簡體   English   中英

Python Sharepoint 庫 Office365-REST-Python-Client 錯誤:401:網址未經授權:https://mydomain.sharepoint.com/sites/sample/_api/Web

[英]Python Sharepoint library Office365-REST-Python-Client ERROR: 401: Unauthorized for url: https://mydomain.sharepoint.com/sites/sample/_api/Web

我正在嘗試將僅限應用程序的流程用於共享點。

首先,我使用此 URL https://mydomain.sharepoint.com/sites/sample/_layouts/15/appregnew.aspx創建了一個新的應用程序客戶端 ID 和密鑰。

然后我轉到 URL https://mydomain.sharepoint.com/sites/sample/_layouts/15/appinv.aspx使用以下 xml 授予此應用程序的權限。

<AppPermissionRequests AllowAppOnlyPolicy="true"> <AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="FullControl"/> <AppPermissionRequest Scope="http://sharepoint/content/sitecollection/web" Right="FullControl"/> </AppPermissionRequests>

並點擊信任它。

我正在使用的代碼:

from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext

site_url = 'https://mydomain.sharepoint.com/sites/sample/'
app_principal = {
  'client_id': '.....-..',
  'client_secret': '......=',
}

context_auth = AuthenticationContext(url=site_url)
context_auth.acquire_token_for_app(client_id=app_principal['client_id'], 
client_secret=app_principal['client_secret'])

ctx = ClientContext(site_url, context_auth)
web=ctx.web
ctx.load(web)
ctx.execute_query()
if len(result) > 0:
    print("Folder has been found: {0}".format(result[0].properties["Name"]))

但它拋出以下錯誤: Traceback (most recent call last): File "client.py", line 16, in <module> ctx.execute_query() File "C:\\Users\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\office365\\runtime\\client_runtime_context.py", line 140, in execute_query self.pending_request().execute_query() File "C:\\Users\\AppData\\Local\\Programs\\Python\\Python37\\lib\\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://mydomain.sharepoint.com/sites/sample/_api/Web')

需要幫忙。

我按照您的步驟操作,但無法重現您的問題。 在此處輸入圖片說明

如果您的 office365 租戶是最近創建的,您可以嘗試運行以下命令。

Set-SPOTenant -DisableCustomAppAuthentication $false

提示:您需要將 SharePoint Online 托管外殼更新到最新版本。

更新: 在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM