簡體   English   中英

SharePoint Online中的遠程身份驗證

[英]Remote Authentication in SharePoint Online

我正在嘗試使用SharePoint包編寫腳本來訪問我公司的SharePoint上的文件。 教程說明

首先,您需要創建一個SharePointSite對象。 我們假設您正在使用基本身份驗證; 如果你不是,你需要自己創建一個合適的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

我目前使用以下代碼生成我的POST。 在其他一些問題的指導下,我省略了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的POST?

根據SharePoint Online中的遠程身份驗證使用基於聲明的身份驗證SharePoint Online身份驗證文章:

聯合身份驗證(FedAuth)cookie適用於SharePoint Online中的每個頂級站點,例如根站點,MySite,管理站點和公共站點。 所有SharePoint Online都使用根聯合身份驗證(rtFA)cookie。 當用戶訪問新的頂級站點或其他公司的頁面時,rtFA cookie用於在沒有提示的情況下以靜默方式對其進行身份驗證。

總而言之,要獲取身份驗證cookie,需要將請求發送到以下端點:

url: https://tenant.sharepoint.com/_forms/default.aspx?wa=wsignin1.0  
method: POST
data: security token

一旦請求得到驗證,響應將包含HTTP標頭中的身份驗證cookie( FedAuthrtFa ),如您提到的文章中所述。

適用於Python的SharePoint Online REST客戶端

作為概念驗證,已發布用於PythonSharePoint Online REST客戶端,其中顯示了如何:

  • 在SharePoint Online中執行遠程身份驗證
  • 使用REST API對Web資源(如Web,列表或列表項)執行基本CRUD操作

實施細節

例子

該示例顯示了如何讀取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存儲庫的examples文件夾下找到更多示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM