C# WebTokenRequest和OneDriveClient
我试图通过使用实现Onedrive客户端登录 使用此方法,我最终使用此代码获得一个令牌C# WebTokenRequest和OneDriveClient,c#,onedrive,C#,Onedrive,我试图通过使用实现Onedrive客户端登录 使用此方法,我最终使用此代码获得一个令牌 private static async Task<string> RequestTokenAndSaveAccount(WebAccountProvider Provider, String Scope, String ClientID) { try { WebTokenRequest webTokenRequest = new W
private static async Task<string> RequestTokenAndSaveAccount(WebAccountProvider Provider, String Scope, String ClientID)
{
try
{
WebTokenRequest webTokenRequest = new WebTokenRequest(Provider, "wl.signin onedrive.appfolder onedrive.readwrite", ClientID);
WebTokenRequestResult webTokenRequestResult = await WebAuthenticationCoreManager.RequestTokenAsync(webTokenRequest);
if (webTokenRequestResult.ResponseStatus == WebTokenRequestStatus.Success)
{
App.settings.onedriveStoredAccountKey = webTokenRequestResult.ResponseData[0].WebAccount.Id;
return webTokenRequestResult.ResponseData[0].Token;
}
return "";
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
return "";
}
}
其中accessToken取自RequestTokenAndSaveAccount
方法
OneDrive.SDK 2.x
对于这种情况,@dabox给出的答案是正确的解决方案。
OneDriveClient
只需要一个IAAuthenticationProvider
,这是一个非常简单的界面。您可以创建自己的并实现authenticateRequestsAsync
,以便它调用您的RequestTokenAndSaveAccount
,然后将承载令牌添加到请求中。附加到Brad的答案中,您可以创建一个新的AuthenticationProivder,它在包Microsoft.Graph.Core
中实现IAAuthenticationProivder接口。在包Microsoft.Graph.Core
中还有一个DelegateAuthenticationProvider
,它为您提供了一个代理接口。例如:
OneDriveClient oneDriveClient = new OneDriveClient(
new DelegateAuthenticationProvider(
async (requestMessage) =>
{
string accessToken = await GetAccessTokenSomeWhereAsync();
// Append the access token to the request.
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
}));
return oneDriveClient ;
基于Microsoft Graph的asp.net示例进行了修改:Hi,这是无效的,因为OneDriveClient与GraphServiceClient非常不同,后者是文档中引用的对象。无论如何,非常感谢您的帮助。编辑,这对OneDrive.SDK 1.x的实现无效。对于OneDrive.sdk2.x,它工作得非常好@Brad答案对OneDrive.SDK 1.x有效。我将对我的问题进行编辑,以说明如何处理这两个版本。非常感谢。
OneDriveClient oneDriveClient = new OneDriveClient(
new DelegateAuthenticationProvider(
async (requestMessage) =>
{
string accessToken = await GetAccessTokenSomeWhereAsync();
// Append the access token to the request.
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
}));
return oneDriveClient ;