【问题标题】:How to create credential object with already having access code and token for Google Drive API如何使用已经拥有 Google Drive API 的访问代码和令牌创建凭证对象
【发布时间】:2015-11-21 01:13:05
【问题描述】:

我想为工作表和谷歌驱动器 api 使用 相同的 OAuth 代码和令牌,而不需要为工作表和驱动器重定向到第 2 页。

以下是使用 oauth 2 生成访问码和令牌的代码

                string SCOPE = "https://spreadsheets.google.com/feeds https://docs.google.com/feeds";
                OAuth2Parameters parameters = new OAuth2Parameters();
                parameters.ClientId = CLIENT_ID;
                parameters.ClientSecret = CLIENT_SECRET;
                parameters.RedirectUri = REDIRECT_URI;
                parameters.Scope = SCOPE;
                string authorizationUrl = OAuthUtil.CreateOAuth2AuthorizationUrl(parameters); 
                OAuthUtil.GetAccessToken(parameters);
                string accessToken = parameters.AccessToken;

与 asp.net mvc 一样,它是通过 google sheet API 的重定向生成的。

我也想将 google drive API 用于相同的令牌。

我如何为 google Drive API 创建凭据对象以便使用它。跟随代码打开另一个窗口并将代码发送到该窗口。

var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets
      {
         ClientId = clientId,
         ClientSecret = clientSecret
      }, scopes, "skhan", CancellationToken.None, new FileDataStore("Drive.Auth.Store")).Result;

【问题讨论】:

    标签: c# asp.net-mvc oauth google-drive-api google-api-dotnet-client


    【解决方案1】:

    您可以使用下面的代码,假设您已经拥有访问/刷新令牌:

    var flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
        {
            ClientSecrets = new ClientSecrets
                {
                    ClientId = ClientId,
                    ClientSecret = ClientSecret
                },
                Scopes = new[] { DriveService.Scope.Drive }
        });
    
    var credential = new UserCredential(_flow, UserIdentifier, new TokenResponse
        {
            AccessToken = AccessToken,
            RefreshToken = RefreshToken
        });
    var service = new DriveService(new BaseClientService.Initializer
        {
            ApplicationName = "MyApp",
            HttpClientInitializer = credential,
            DefaultExponentialBackOffPolicy = ExponentialBackOffPolicy.Exception | ExponentialBackOffPolicy.UnsuccessfulResponse503
        });
    

    【讨论】:

      【解决方案2】:

      您可以使用此处提到的方法:.NET Google api 1.7 beta authenticating with refresh token,只需在令牌响应中设置您的访问权限和引用令牌,就可以开始了。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-28
      • 2012-10-12
      相关资源
      最近更新 更多