OneDrive API - 请参阅要上传或下载的Sharepoint文件 - 无效的受众群体错误

时间:2016-05-26 04:50:39

标签: python-2.7 sharepoint office365 adal onedrive

我想以编程方式与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网站的文件的网址是否有助于定义file_url语法应该是什么?如果不是,我怎么能确定file_url应该是什么?

2 个答案:

答案 0 :(得分:0)

根据代码,您使用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

答案 1 :(得分:0)

费雪让我走上了正确的道路。我的代码正在请求ADAL没有预料到的资源。

adal.acquire_token_with_username_password __init__.py class _DefaultValues client_id resource https://management.core.windows.net/ file_url invalid audience https://tenant.sharepoint.com `client_id` = my Azure AD app's client_id `resource` = `https://tenant.sharepoint.com/`

ADAL的默认资源是acquire_token_with_username_password,这是我期望client_id资源所拥有的资源。 resourceNone

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

=None

ADAL' 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' 。我没有尝试,但猜测可以修改这些内容以删除h4并在我的代码中设置,而不是.departmentname

div

还对我的display(url to filename)进行了小的(但必须的)更改为:

public class Person
{
   public int Id {get; set}
   public int Username {get; set;}
   //...other props
}

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