我正在编写一个必须调用Sharepoint-online API的集成。我的集成不是一个webapp,必须在没有用户在场的情况下工作。
据我了解,我需要两个设置步骤: 1.用户必须登录Azure并设置应用程序并获取客户端ID。 2.我必须使用客户端ID和用户名和密码调用服务,然后我将获得访问令牌,刷新令牌和ID令牌
完成两个设置步骤后,我可以使用访问令牌调用服务,但有时会过期,我需要使用刷新令牌来获取新的。
步骤2对我来说似乎很奇怪。为什么没有用户界面,用户可以登录并获取Access Refresh和ID令牌?有人建立了一个公用事业网站,只是这样做,或者我误解了什么?
由于 罗伯特
答案 0 :(得分:2)
服务和守护程序应用的推荐OAuth流程是客户端凭据流(在该流程中,没有涉及刷新令牌; a客户端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>