繁体   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