Office365-REST-Python-客户端访问令牌问题 [英] Office365-REST-Python-Client Access Token issue

查看:326
本文介绍了Office365-REST-Python-客户端访问令牌问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了许多使用Office365-REST-Python-Client的示例,但是没有一个示例可以正确获取访问令牌.我已经在Azure门户下注册了一个应用程序,使用应用程序权限"向其授予了API权限,创建了一个秘密,并在我的设置字典中使用了client_secret和client_id来在以下代码中使用.

I've found many examples of using the Office365-REST-Python-Client however, none of them are correctly obtaining the access token. I've registered an app under the Azure Portal, granted it API permissions using 'Application permissions', created a secret and used the client_secret and client_id in my settings dictionary to use in the below code.

def read_folder_and_files(context, list_title):
    """Read a folder example"""
    list_obj = context.web.lists.get_by_title(list_title)
    folder = list_obj.root_folder
    context.load(folder)
    context.execute_query()
    print("List url: {0}".format(folder.properties["ServerRelativeUrl"]))

    files = folder.files
    context.load(files)
    context.execute_query()
    for cur_file in files:
        print("File name: {0}".format(cur_file.properties["Name"]))

    folders = context.web.folders
    context.load(folders)
    context.execute_query()
    for folder in folders:
        print("Folder name: {0}".format(folder.properties["Name"]))


if __name__ == '__main__':
    ctx_auth = AuthenticationContext(url=settings['url'])
    if ctx_auth.acquire_token_for_app(client_id=settings['client_credentials']['client_id'],
                                      client_secret=settings['client_credentials']['client_secret']):
        ctx = ClientContext(settings['url'], ctx_auth)
        read_folder_and_files(ctx, "Documents")
        # read_folder_and_files_alt(ctx, "Documents")
        # upload_file_into_library(target_library, name, content)
        # download_file(ctx)
    else:
        print(ctx_auth.get_last_error())

运行上面的代码时,出现以下错误:

When I run the above code I get the following error:

  File "/usr/local/lib/python3.7/site-packages/office365/runtime/auth/acs_token_provider.py", line 76, in get_authorization_header
    return 'Bearer {0}'.format(self.access_token["access_token"])
KeyError: 'access_token'

我的最终目标是使用python数据管道中的元数据将文件上传到Sharepoint文档库. Sharepoint不在本地托管,并且包含在我们的365许可证中.

My end goal is to upload files to a Sharepoint Document Libary with metadata from a python data pipeline. Sharepoint is not hosted locally and is included in our 365 licences.

亲切的问候

推荐答案

因此,当您没有访问令牌时,似乎会发生此错误.

So it looks like this error can happen when you're not getting an access token.

我通过放弃Azure门户中的客户端和密钥来解决此问题,而是在SharePoint网站中的以下URL下生成了它们:

I fixed this by ditching the client and secret in my Azure Portal and instead generated them in the SharePoint site under the following URL:

URL: https://[tenant] .sharepoint.com/_layouts/15/appregnew.aspx

要找出在[承租人]空格中应该使用的内容,请查看您的SharePoint URL,并选择"https://"和".sharepoint.com"之间的文本.这是假设您的SharePoint由Microsoft托管.

To find out what you should use in the space of [tenant] look at your SharePoint URL and pick out the text between 'https://' and '.sharepoint.com'. This is assuming your SharePoint is hosted by Microsoft.

单击生成"按钮,使用相关的标题,除非您更了解,否则只需为应用程序域"和重定向URL"输入localhost. (我的项目只是一个简单的上传脚本).取得客户ID和机密的副本.

Click the generate buttons, use a relevant Title and unless you know better just enter localhost for the App Domain and Redirect URL. (My project is just a simple upload script). Take a copy of the Client ID and Secret.

如果您希望您的应用拥有完全访问权限,请导航至:

If you want your App to have full access then navigate to:

https://[tenant] -admin.sharepoint.com/_layouts/15/appinv.aspx

还有另一个链接' https://[tenant] .sharepoint. com/_layouts/15/appinv.aspx ",但这不会让您申请完全控制权限.

There is another link 'https://[tenant].sharepoint.com/_layouts/15/appinv.aspx' but this won't let you apply for full control permissions.

将客户端ID粘贴到App ID中,使用相同的字段名称或将表单链接在一起的乐趣在哪里?单击查找,然后使用下面的XML授予完全控制权.

Paste in the client id into the App id, where would be the fun in using the same field name, or linking the form together? Click lookup and use the below XML to grant full control.

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

单击创建,然后在下一页上单击信任.您需要以具有完全管理员权限的网站所有者身份登录才能授予此控制权.

Click create and on the next page click trust. You will need to be logged in as a site owner with full admin permissions to grant this control.

我在这里找到了所有这些信息,如果需要的话,将进行更详细的介绍: https://www.anupams .net/app-only-policy-with-tenant-level-permissions-in-sharepoint-online/

I found all this info here which goes into more detail if you need it: https://www.anupams.net/app-only-policy-with-tenant-level-permissions-in-sharepoint-online/

奖金信息: 我们的Sharepoint有两个站点",因此传递的基本URL为" https://[tenant] .sharepoint.com '默认情况下将我带到错误的站点,这意味着我要查找的文档库不存在.要在您的设置字典中使用Office365-REST-Python-Client修复此问题,请确保URL设置具有您网站的填充路径,如下所示:

Bonus Info: Our Sharepoint has two 'sites' so passing the base URL of 'https://[tenant].sharepoint.com' took me to the wrong site by default and meant that the document libraries I was looking didn't exist. To fix this using Office365-REST-Python-Client in your settings dictionary make sure the URL setting has the fill path to your site like this:

https://[tenant] -admin.sharepoint.com/sites/[站点]

希望这会有所帮助,此信息使我花费了很多时间!

Hope this helps, this info cost me far to much time!

这篇关于Office365-REST-Python-客户端访问令牌问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆