使用Python Office365-REST-Python-Client,我编写了以下Python函数,用于从Sharepoint下载Excel电子表格(基于How to read SharePoint Online (Office365) Excel files in Python with Work or School Account?的答案)
import sys
from urlparse import urlparse
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.file import File
xmlErrText = "<?xml version=\"1.0\" encoding=\"utf-8\"?><m:error"
def download(sourceURL, destPath, username, password):
print "Download URL: {}".format(sourceURL)
urlParts = urlparse(sourceURL)
baseURL = urlParts.scheme + "://" + urlParts.netloc
relativeURL = urlParts.path
if len(urlParts.query):
relativeURL = relativeURL + "?" + urlParts.query
ctx_auth = AuthenticationContext(baseURL)
if ctx_auth.acquire_token_for_user(username, password):
try:
ctx = ClientContext(baseURL, ctx_auth)
web = ctx.web
ctx.load(web)
ctx.execute_query()
except:
print "Failed to execute Sharepoint query (possibly bad username/password?)"
return False
print "Logged into Sharepoint: {0}".format(web.properties['Title'])
response = File.open_binary(ctx, relativeURL)
if response.content.startswith(xmlErrText):
print "ERROR response document received. Possibly permissions or wrong URL? Document content follows:\n\n{}\n".format(response.content)
return False
else:
with open(destPath, 'wb') as f:
f.write(response.content)
print "Downloaded to: {}".format(destPath)
else:
print ctx_auth.get_last_error()
return False
return True
此功能对某些URL正常,但对另一些URL则失败,失败时将打印以下“文件不存在”文档内容(为便于阅读,添加了换行符和空格):
<?xml version="1.0" encoding="utf-8"?>
<m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<m:code>
-2130575338, Microsoft.SharePoint.SPException
</m:code>
<m:message xml:lang="en-US">
The file /sites/path/to/document.xlsx does not exist.
</m:message>
</m:error>
我知道用户名和密码正确。确实,更改密码会导致完全不同的错误。
我发现当文档不存在或没有足够的权限访问该文档时,就会发生此错误。
但是,我可以使用相同的用户名/密码在Web浏览器中下载具有相同URL的文档。
请注意,对于同一Sharepoint存储库中的某些.xlsx URL,该相同功能始终可以正常运行,但对于同一SharePoint存储库中的某些其他.xlsx URL,则始终无法运行。
我唯一的猜测是,还有一些需要我管理的更细粒度的权限。但是我完全不知道它们是否存在。
有人可以帮助我解决导致失败的原因并弄清楚如何使其能够处理我已经可以在网络浏览器中下载的所有必需文件吗?
以下注释的其他注释
%20
编码。