我试图用SharePoint包编写一个脚本来访问公司SharePoint上的文件。本教程声明
首先,您需要创建一个
SharePointSite
对象。我们假设您使用的是basic;如果不使用,则需要自己创建一个适当的urllib2
打开器。
然而,经过几次尝试,我得出的结论是基本的auth是不够的。在研究如何使其工作的过程中,我遇到了这篇文章,它很好地概述了身份验证的一般方案。我正在挣扎的是用Python实现这一点。
我已经成功地劫持了SharePoint模块中的基本内容。为此,我在链接的文章中获取了XML消息,并使用它替换了SharePoint模块生成的XML。在做了一些其他更改之后,我现在收到一个令牌,如链接文章的步骤2所述。
现在,在步骤3中,我需要用POST将这个令牌发送到SharePoint。下面是它应该是什么样子的示例:
POST http://yourdomain.sharepoint.com/_forms/default.aspx?wa=wsignin1.0 HTTP/1.1
Host: yourdomain.sharepoint.com
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)
Content-Length: [calculate]
t=EwBgAk6hB....abbreviated
我目前使用下面的代码来生成我的帖子。在其他几个问题的指导下,我省略了content-length
头,因为应该自动计算它。我不知道该把令牌放在哪里,所以我就把它塞进了data
。
headers = {
'Host': 'mydomain.sharepoint.com',
'Connection': 'keep-alive',
'User-Agent': 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)'
}
data = {'t':'{}'.format(token[2:])}
data = urlencode(data)
postURL = "https://mydomain.sharepoint.com/_forms/default.aspx?wa=wsignin1.0"
req = Request(postURL, data, headers)
response = urlopen(req)
但是,这会产生以下错误消息:
urllib2.HTTPError: HTTP Error 302: The HTTP server returned a redirect error that would lead to an infinite loop.
The last 30x error message was:
Found
如何生成将正确返回所需身份验证cookie的帖子?
发布于 2016-02-08 17:01:00
根据基于声明的SharePoint联机远程身份验证和SharePoint在线认证的文章:
联邦身份验证(FedAuth) cookie用于SharePoint Online中的每个顶级站点,如根站点、MySite、管理站点和公共站点。根联邦身份验证(rtFA) cookie用于所有SharePoint联机。当用户访问一个新的顶级站点或另一个公司的页面时,rtFA cookie用于在没有提示的情况下对它们进行无声的身份验证。
总之,要获得身份验证cookie,需要将请求发送到以下端点:
url: https://tenant.sharepoint.com/_forms/default.aspx?wa=wsignin1.0
method: POST
data: security token
一旦验证了请求,响应将在header中包含身份验证cookie (FedAuth
和rtFa
),如本文所述。
SharePoint在线REST客户端
作为概念的证明,已经发布了SharePoint在线REST客户端,它展示了如何:
实现详细信息:
acquireAuthenticationCookie
函数。示例
此示例演示如何读取Web客户端对象属性:
from client.AuthenticationContext import AuthenticationContext
from client.ClientRequest import ClientRequest
url = "https://contoso.sharepoint.com/"
username = "jdoe@contoso.onmicrosoft.com"
password = "password"
ctxAuth = AuthenticationContext(url)
if ctxAuth.acquireTokenForUser(username, password):
request = ClientRequest(url,ctxAuth)
requestUrl = "/_api/web/" #Web resource endpoint
data = request.executeQuery(requestUrl=requestUrl)
webTitle = data['d']['Title']
print "Web title: {0}".format(webTitle)
else:
print ctxAuth.getLastErrorMessage()
在示例存储库的GitHub文件夹下可以找到更多的示例
https://stackoverflow.com/questions/35187724
复制相似问题