推薦的OAuth流爲服務和守護程序的應用程序是客戶機憑證流(在流,有沒有刷新令牌參與;客戶端ID和客戶端密鑰是用於獲取最終到期的訪問令牌,然後您需要使用相同的客戶端ID和密碼獲取新的訪問令牌)。在SharePoint Online中,的情況下,你有這種情況下2種選擇:
的Program.cs
static void Main(string[] args)
{
Uri siteUri = new Uri("https://tenant.sharepoint.com/teams/test");
//Get the realm for the URL
string realm = TokenHelper.GetRealmFromTargetUrl(siteUri);
//Get the access token for the URL.
// Requires this app to be registered with the tenant
string accessToken = TokenHelper.GetAppOnlyAccessToken(
TokenHelper.SharePointPrincipal,
siteUri.Authority, realm).AccessToken;
HttpWebRequest endpointRequest =
(HttpWebRequest)HttpWebRequest.Create(
"https://tenant.sharepoint.com/teams/test/_api/web/lists/GetByTitle('Documents')/items");
endpointRequest.Method = "GET";
endpointRequest.Accept = "application/json;odata=verbose";
endpointRequest.Headers.Add("Authorization", "Bearer " + accessToken);
HttpWebResponse endpointResponse =
(HttpWebResponse)endpointRequest.GetResponse();
}
}
的app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<appSettings>
<add key="ClientId" value="65e674ca-3827-4134-852b-1196ff935e08"/>
<add key="ClientSecret" value="xxxxxxx"/>
</appSettings>
</configuration>
- 的SharePoint Online + Azure中的Active Directory(AAD )整合。細節here。在那個鏈接中你會找到一個示例代碼。第一種方法的區別在於,在這一個中你不使用ACS而是使用AAD。應用程序需要的權限在AAD中定義 - 截至今天,據我所知,您可以在AAD中定義的應用程序權限不像您可以通過ACS定義的應用程序權限 - 即使用ACS,您可以在網站集級別定義應用程序,使用AAD您無法獲得租戶的全權限(即所有網站集)
我開始調查https://developer.microsoft.com/zh-cn/ us/graph /作爲解決方案。 – Robert3452