401 Client Error: Unauthorized
See original GitHub issueI have checked all the issues, especially #207
I have registered the app: https://[tenant].sharepoint.com/_layouts/15/appinv.aspx
The app identifier has been successfully created.
Client Id: ID
Client Secret: TOKEN=
Title: PFS
App Domain: www.localhost.com
Redirect URI: https://www.localhost.com
I have given permissions: https://[tenant].sharepoint.com/_layouts/15/appinv.aspx
XML:
<AppPermissionRequests AllowAppOnlyPolicy="true">
<AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="FullControl" />
</AppPermissionRequests>
But this code:
from office365.runtime.auth.client_credential import ClientCredential
from office365.sharepoint.client_context import ClientContext
site_url = "https://[tenant].sharepoint.com/"
client_id = "ID"
client_secret = "TOKEN="
client_credentials = ClientCredential(client_id, client_secret)
ctx = ClientContext(site_url).with_credentials(client_credentials)
web = ctx.web
ctx.load(web)
ctx.execute_query()
print(web.properties['Url'])
Produces this error:
Traceback (most recent call last):
File ".../lib/python3.6/site-packages/office365/runtime/client_request.py", line 75, in execute_query
response.raise_for_status()
File ".../lib/python3.6/site-packages/requests/models.py", line 943, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://[tenant].sharepoint.com/_api/Web
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "sharepoint.py", line 96, in <module>
ctx.execute_query()
File ".../lib/python3.6/site-packages/office365/runtime/client_runtime_context.py", line 138, in execute_query
self.pending_request().execute_query()
File ".../lib/python3.6/site-packages/office365/runtime/client_request.py", line 79, in execute_query
raise ClientRequestException(*e.args, response=e.response)
office365.runtime.client_request_exception.ClientRequestException: (None, None, '401 Client Error: Unauthorized for url: https://[tenant].sharepoint.com/_api/Web')
If i use ctx = ClientContext(site_url).with_user_credentials(username, password) everything works fine.
Where do I go from here?
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
How to Fix a 401 Unauthorized Error? - GeeksforGeeks
The 401 Unauthorized Error is an HTTP status code error that represented the request sent by the client to the server that lacks...
Read more >What is ā401 Error Unauthorized Access" and How to Fix it?
The HTTP code ā401 Unauthorized Accessā is a client-side error. It indicates that the website's server sends a āWWW-Authenticateā header response back to...
Read more >401 Unauthorized Error: What It Is and How to Fix It
The 401 Unauthorized Error is an HTTP response status code indicating that the client could not authenticate a request.
Read more >401 Client Error: Unauthorized for url - python - Stack Overflow
It means you are sending wrong credentials please verify your credentials. ā kvk30. Jan 29, 2018 at 9:57. 1.
Read more >How to Quickly Fix the 401 Unauthorized Error (5 Methods)
The 401 (Unauthorized) status code indicates that the request has not been applied because it lacks valid authentication credentials for theĀ ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
New week, new motivation š
I found this thread https://stackoverflow.com/questions/64025660/python-sharepoint-library-office365-rest-python-client-error-401-unauthorized
Install cmdlet if you donāt have it:
Connect and run the command
At first, I got the same error, but after a few minutes (5-10) the authorization worked without any other changes.
Note that my Office365 setup is only a few weeks old.
@xibriz good to know you get it fixed and thank you for sharing the details, I believe someone else will find this useful.
Indeed since:
setting
DisableCustomAppAuthenticationto$falseshould do the trick.Regarding
positive, according to Add-in secret expiration:
Although there is one trick available, for SharePoint Online app principal could be registered via Azure AD instead of
AppRegNew.aspxpage, this way app lifetime duration could be controlled on a more flexible level.Refer, for example, this post for more details.