2016-02-05 73 views
-1

我想使用REST API在c#中檢索所有在線SharePoint列表。 對於這一點,我使用下面的代碼如何使用C#中的REST API獲取列表項目

HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create("https://o365.sharepoint.com/sites/test-site/_api/web/lists"); 
endpointRequest.Method = "GET"; 
endpointRequest.Accept = "application/json;odata=verbose"; 
endpointRequest.ContentLength = 0; 

但我得到以下錯誤

403:禁止訪問被拒絕。在此位置打開文件之前。您必須先瀏覽網站並選擇自動登錄選項。

所以我搜索過,發現這個,我們必須通過AuthenticationToken,這是寫在這個環節

https://msdn.microsoft.com/en-us/library/jj164022.aspx

endpointRequest.Headers.Add("Authorization", "Bearer " + accessToken); 

但我如何得到的accessToken?

+0

您必須從認證服務器獲得AcceessToken(您需要有效的憑據才能這樣做) – bit

+0

我有有效憑證可以這樣做,但我如何實現Accesstoken。如果您可以提供示例代碼,那將非常有幫助。 – User5590

+0

請參閱:https://www.visualstudio.com/en-us/integrate/get-started/auth/oauth或 http://code.pearson.com/pearson-learningstudio/apis/authentication/authentication-sample -code/sample-code-oauth-2-c_x – bit

回答

0

that same page

[...]它還假定您有存儲在的accessToken變量有效的OAuth訪問令牌。如果您從加載項Web內部進行此調用,則不需要訪問令牌,就像在SharePoint託管的加載項中一樣。請注意,您無法從瀏覽器客戶端上運行的代碼獲取訪問令牌。您必須從服務器上運行的代碼獲取訪問令牌。 Context Token OAuth flow for SharePoint Add-insAuthentication Code OAuth flow for SharePoint Add-ins說明如何獲取訪問令牌。

+0

感謝您的回覆。我從中調用REST API的應用程序不在SharePoint中。所以我需要編寫一個代碼來從認證服務器獲取accessToken。如果你能提供樣品,那將會非常有用。 – User5590

相關問題