【发布时间】: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