为什么从Sharepoint下载.xlsx的Python脚本仅在某些URL中失败?
使用Python Office365 REST Python客户端,我编写了以下Python函数从Sharepoint下载Excel电子表格(基于上的答案) 导入系统 从URLPRASE导入URLPRASE 从office365.runtime.auth.authentication\u上下文导入AuthenticationContext 从office365.sharepoint.client\u上下文导入ClientContext 从office365.sharepoint.file导入文件为什么从Sharepoint下载.xlsx的Python脚本仅在某些URL中失败?,python,sharepoint,download,office365api,Python,Sharepoint,Download,Office365api,使用Python Office365 REST Python客户端,我编写了以下Python函数从Sharepoint下载Excel电子表格(基于上的答案) 导入系统 从URLPRASE导入URLPRASE 从office365.runtime.auth.authentication\u上下文导入AuthenticationContext 从office365.sharepoint.client\u上下文导入ClientContext 从office365.sharepoint.file导入文件
xmlErrText=“我认为区别在于URL本身。失败的URL中是否有空格?相同的文件是否始终失败,或者它是否改变了哪些文件成功,哪些文件失败?您是否搜索了错误消息中包含的错误代码(-2130575338,或十六进制的0x81020016)?它似乎与
ClientErrorCodes.ListItemDeleted
错误()。可能该项目已被删除,但仍缓存在您的浏览器中?此错误似乎还有其他原因,但我对SharePoint不太熟悉,因此无法针对此情况筛选相关网页。我只是希望这能让您找到正确的方向:-)我怀疑@wovano的做法是正确的。或许可以尝试检索文件的属性或版本控制信息,看看是否有效?可能文件已被删除、重新添加或从回收站返回,但删除标志尚未重置或仍在缓存中?查看文件的历史记录,它似乎从未被删除。但它已被移动。我无法从其旧位置下载,即使在浏览器中也是如此。B但我可以从浏览器中的新位置下载,但不能使用脚本。我不是Sharepoint专家,我不确定还需要寻找什么。
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
<?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>