簡體   English   中英

來自 Python 的 SharePoint Online 訪問令牌

[英]SharePoint Online access token from Python

任何幫助深表感謝! 我已經嘗試了很多庫和這樣做的方法,但似乎沒有任何效果。 我對 REST API 和使用它們訪問 SharePoint 缺乏經驗。 我嘗試了各種使用 Python 請求的代碼示例,office365-REST-Python-Client、HttpNtlmAuth、OAuth2、rauth 等。

我們的 SharePoint 管理員為我設置了一個 SharePoint 插件來訪問我的網站數據。 說它不會是我在 API 中的用戶名和密碼,而是使用客戶端,首先獲取訪問令牌,然后進行 API 調用。

令人沮喪的是 SharePoint 管理員在 Postman 中設置了調用,API 請求調用以獲取訪問令牌並獲取我希望在那里正常工作的站點文件和文件夾數據。 我什至可以使用她創建的相同參數在 Postman 中創建我自己的 API 請求,並且效果很好。 但是嘗試在我的 Python 應用程序中執行此操作給我帶來了各種錯誤和問題。 現在,我只是想在 Python 中檢索我的訪問令牌。

這是我當前的代碼和錯誤。 由於明顯的原因,括號內的所有文本和所有大寫字母都是非文字/模糊的,因為我是在網上發布的。 錯誤是:

文件“[我的本地驅動器位置...]\\venv\\lib\\site-packages\\office365\\runtime\\auth\\authentication_context.py”,第 45 行,acquire_token_for_app 引發 ValueError('獲取令牌失敗:{0}'.format (self.provider.error)) ValueError: Acquire token failed: None

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

class SharepointService:

        site_url = 'https://accounts.accesscontrol.windows.net/[TENANT ID the SP ADMIN GAVE ME]/tokens/OAuth/2?content'
        client_id = '[THE CLIENT ID THE SP ADMIN GAVE ME]'
        client_secret = '[THE CLIENT SECRET THE SP ADMIN GAVE ME]'

        app_principal = {'client_id': client_id, 'client_secret': client_secret}

        context_auth = AuthenticationContext(url=site_url)

        token = context_auth.acquire_token_for_app(client_id=app_principal['client_id'], client_secret=app_principal['client_secret'])
        print(token)

        ctx = ClientContext(site_url, context_auth)

        web = ctx.web
        ctx.load(web)
        ctx.execute_query()
        print("Web site title: {0}".format(web.properties['Title']))


看來您應該將您的 site_url 更改為您要訪問的站點。

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

site_url = 'https://contoso.sharepoint.com/sites/dev'
app_principal = {
  'client_id': 'e53634cb-xxxx-4e3a-8557-026a9393e215',
  'client_secret': 'SKjZHU4Ubr2BTouPsiXXtz9YeHU/yBww/xXxanq1I2k=',
}

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()
print("Web site title: {0}".format(web.properties['Title']))

測試結果: 在此處輸入圖片說明

path('api-auth/',include ('rest_framework.urls',namespace='rest_framework')), path('api/', include('djoser.urls')), ####urls.py
from rest_framework.decorators import api_view
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import AllowAny
##vies.py
class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = '__all__'
###serializers.py

 'DEFAULT_AUTHENTICATION_CLASSES': [ 'rest_framework_simplejwt.authentication.JWTAuthentication', 'rest_framework_jwt.authentication.JSONWebTokenAuthentication', 'rest_framework.authentication.SessionAuthentication', 'rest_framework.authentication.BasicAuthentication', ], 'DEFAULT_PERMISSION_CLASSES': ( 'rest_framework.permissions.IsAuthenticated', 'rest_framework.permissions.AllowAny', "rest_framework.permissions.DjangoModelPermissions", ##settings.py

這仍然是此錯誤的最高 Google 結果。

這是由兩件事引起的:

  • 由於某種原因,Azure 無法對您的 Sharepoint 加載項進行身份驗證。
  • 您正在運行一個版本的 office365-rest-python-client < 2.3.0 - 在此版本之前,失敗是無聲的

這一行 (raise_for_status) 現在打印 azure auth 失敗: https : //github.com/vgrem/Office365-REST-Python-Client/blob/2.3.0/office365/runtime/auth/providers/acs_token_provider.py#L69

在我的情況下,身份驗證失敗是由於密鑰在一年后到期。

您可以這樣刷新它們: https : //medium.com/@cecildt/renewing-sharepoint-online-provider-add-ins-client-secret-ba2828a49e7

暫無
暫無

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

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