任何帮助都是非常感谢的!我尝试过这么多的图书馆和方法,似乎什么都没有用。我对REST和使用REST访问SharePoint缺乏经验。我已经试用了各种使用Python请求、Office365-RET-Python-Client、HttpNtlmAuth、OAuth2、rauth等的代码示例。
我们的SharePoint管理员为我设置了一个SharePoint AddIn来访问我的站点数据。表示它不是API中的用户名和密码,而是使用客户端,首先获得访问令牌,然后进行API调用。
令人沮丧的是,SharePoint管理员在Postman中设置了调用,API请求调用以获得访问令牌,并获得我希望在那里工作的站点文件和文件夹数据。我甚至可以使用她创建的相同参数在Postman中创建我自己的API请求,而且效果很好。但是在我的Python应用程序中尝试这样做给我带来了各种各样的错误和问题。现在,我只是尝试用Python检索我的访问令牌。
这是我当前的代码和错误。由于明显的原因,括号内的所有文本和所有大写都是非文字/模糊的,因为我在网上发布了这篇文章。错误是:
文件“我的本地驱动器LOCATION...\venv\lib\site-packages\office365\runtime\auth\authentication_context.py",第45行,在acquire_token_for_app引发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']))
发布于 2020-09-28 05:56:50
似乎您应该将您的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']))
测试结果:
发布于 2020-09-28 06:06:55
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
发布于 2020-09-28 06:10:27
'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
https://stackoverflow.com/questions/64068317
复制相似问题