【问题标题】:SharePoint Online access token from Python来自 Python 的 SharePoint Online 访问令牌
【发布时间】:2021-01-12 00:53:45
【问题描述】:

非常感谢任何帮助!我尝试了很多库和这样做的方法,似乎没有任何效果。我对 REST API 和使用它们访问 SharePoint 缺乏经验。我已经尝试了各种使用 Python 请求、office365-REST-Python-Client、HttpNtlmAuth、OAuth2、rauth 等的代码示例。

我们的 SharePoint 管理员为我设置了一个 SharePoint 插件来访问我的网站数据。说API中不是我的用户名和密码,而是使用客户端,先获取访问令牌,然后进行API调用。

令人沮丧的是,SharePoint 管理员在 Postman 中设置了调用,API 请求调用以获取访问令牌并获取我希望在那里正常工作的站点文件和文件夹数据。我什至可以使用她创建的相同参数在 Postman 中创建自己的 API 请求,并且效果很好。但是尝试在我的 Python 应用程序中执行此操作给我带来了各种错误和问题。目前,我只是尝试在 Python 中检索我的访问令牌。

这是我当前的代码和错误。括号内的所有文本和所有大写字母都是非文字/模糊的,原因很明显,因为我在网上发布了这个。错误是:

文件“[MY LOCAL DRIVE LOCATION...]\venv\lib\site-packages\office365\runtime\auth\authentication_context.py”,第 45 行,在 acquire_token_for_app raise ValueError('获取令牌失败:{0}'.format(self.provider.error)) ValueError:获取令牌失败:无

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']))


【问题讨论】:

  • 你能解决这个问题吗?我有完全相同的问题。
  • 你的代码对我有用,而 shareplum 和 sharepy 没有,我猜他们可能使用了不同的身份验证方法或支持我公司不使用的不同 API

标签: python rest postman access-token sharepoint-online


【解决方案1】:

看来你应该把你的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']))

测试结果:

【讨论】:

  • 谢谢,但我也尝试过(将 URL 更改为我要访问的站点),但错误结果与上述相同。我的问题中显示的 URL 是用于使用 Postman 获取令牌的 URL,管理员对其进行了测试。她说那是获得令牌的地方。它在 Postman 中确实有效,只是在我使用这个特定库的 Python 应用程序中无效。
【解决方案2】:
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

【讨论】:

  • 我看不到该代码如何获取令牌,您能完成示例吗?谢谢
【解决方案3】:

  '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

【讨论】:

  • 我看不出该代码如何获取令牌,您能完成示例吗?谢谢。
【解决方案4】:

这仍然是该错误的最高 Google 结果。

这是由两件事引起的:

  • Azure 由于某种原因未能对您的 Sharepoint 加载项进行身份验证。
  • 您正在运行一个版本的 office365-rest-python-client

这一行 (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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    • 2016-07-23
    • 1970-01-01
    • 2011-12-11
    • 1970-01-01
    • 2011-12-10
    • 1970-01-01
    相关资源
    最近更新 更多