Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WebTokenRequest和OneDriveClient_C#_Onedrive - Fatal编程技术网

C# WebTokenRequest和OneDriveClient

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

我试图通过使用实现Onedrive客户端登录

使用此方法,我最终使用此代码获得一个令牌

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 ;