【问题标题】:OneDrive API - Refer to Sharepoint file to upload or download - invalid audience errorOneDrive API - 参考 Sharepoint 文件上传或下载 - 无效的受众错误
【发布时间】:2016-05-27 04:38:32
【问题描述】:

我想以编程方式与 Office 365 E3 Sharepoint 站点中的文件进行交互。

我正在使用 Azure AD 和 ADAL Python library 来验证对 Sharepoint 站点文件的访问权限。

import adal
import urllib
import requests
import urllib2

## set variables
username = 'curtis@tenant.onmicrosoft.com'
password = 'OFoarry8Oe$'
authorization_url = 'https://login.windows.net/tenant.onmicrosoft.com' # Authority
redirect_uri = 'https://login.microsoftonline.com/login.srf'
client_id = 'dcbf844f-d2c3-42d1-8a7d-0f838f57899a' # Client id

## use ADAL to create token response
token_response = adal.acquire_token_with_username_password(
        authorization_url,
        username,
        password
    )
## endpoints discovery
## https://api.office.com/discovery/v2.0/me/allServices

## create refresh token and save it to use later 
refresh_token = token_response['refreshToken']
refresh_token_file = open('refresh_token.txt', 'w')
refresh_token_file.write(refresh_token)
refresh_token_file.close()

## get saved refresh token and use it to get new token response
refresh_token = open('refresh_token.txt', 'r').read()
token_response = adal.acquire_token_with_refresh_token(authorization_url, str(refresh_token))

## get access_token from token response
access_token = token_response.get('accessToken')
headers = {'Authorization':'BEARER ' + str(access_token)}

认证成功,我可以做到

print access_token

返回一个令牌字符串。

我正在努力使用从 Sharepoint 文件夹下载和上传文件的语法。这是我目前所拥有的:

## download file
file_url = 'https://tenant.sharepoint.com/_api/v1.0/files/root:/myfoldername/myfilename.csv:/content'
r = requests.get(file_url, headers=headers)
print r.text

到目前为止,我还未能成功引用该文件。我收到一个错误:

{"error":"invalid_client","error_description":"Invalid audience Uri 'https:\/\/management.core.windows.net\/'."}

这似乎表明我指的是wrong Site。或者可能是指folder incorrectly

这是我从 Sharepoint 站点获取的我要下载的文件的 URL(来自其在 Sharepoint 中的属性):

https://tenant.sharepoint.com/Shared%20Documents/myfoldername/myfilename.csv

Sharepoint 站点中文件的 url 是否有助于定义 file_url 语法应该是什么?如果不是,我还能如何确定file_url 应该是什么?

【问题讨论】:

    标签: python-2.7 sharepoint office365 adal onedrive


    【解决方案1】:

    根据代码,您已通过 Azure AD 进行身份验证,但调用了 SharePoint REST API。 SharePoint REST 是不同的身份验证流程。你可以参考here

    在您的场景中,我们可以使用 Microsoft Graph API 从 Office 365 中的 SharePoint 网站下载内容。下面是下载默认网站上文件内容的示例:

    GET: https://graph.microsoft.com/v1.0/me/drive/root:/test.txt:/content
    
    authorization: bearer {token}
    

    有关 Microsoft Graph API 的更多详细信息,请参阅以下链接:

    https://graph.microsoft.io/en-us/docs/api-reference/v1.0/api/item_downloadcontent

    https://graph.microsoft.io/en-us/docs/authorization/app_authorization

    【讨论】:

    • 好的,这就是问题所在。假设如果用户已通过身份验证(我的用户是帐户管理员),则将授予所有权限。但事实并非如此。我可能会切换到 Graph API,它似乎有更好的文档记录。
    【解决方案2】:

    飞雪让我走上了正轨。我的代码正在请求 ADAL 没有预料到的资源。

    原来adal.acquire_token_with_username_password 中的__init__.pydefault values(见class _DefaultValues 中的代码底部)用于client_idresource

    ADAL 的默认资源是https://management.core.windows.net/,这是它期望我的file_url 资源拥有的。 invalid audiencehttps://tenant.sharepoint.com

    所以我更改了 ADAL 的默认值:

    `client_id` = my Azure AD app's client_id 
    `resource` = `https://tenant.sharepoint.com/`  
    

    ADAL 的acquire_token_with_username_password(见下文)将client_idresource 设置为None。我没有尝试,但猜测可以编辑这些以删除 =None 并在我的代码中设置而不是 class _DefaultValues

    def acquire_token_with_username_password(
        authority,
        username,
        password,
        client_id=None,
        resource=None,
        validate_authority=True
    ):
    

    并且还对我的file_url(文件名的网址)进行了细微(但必需)更改为:

    file_url = 'https://pokrant.sharepoint.com/_api/v2.0/drive/root:/analytics/output_analytics.csv:/content'
    

    现在,当我运行代码时,我会在控制台中打印出 csv 文件内容。

    【讨论】:

      猜你喜欢
      • 2015-06-20
      • 2017-01-03
      • 1970-01-01
      • 2016-06-05
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多