<i id='yPbQV'><tr id='yPbQV'><dt id='yPbQV'><q id='yPbQV'><span id='yPbQV'><b id='yPbQV'><form id='yPbQV'><ins id='yPbQV'></ins><ul id='yPbQV'></ul><sub id='yPbQV'></sub></form><legend id='yPbQV'></legend><bdo id='yPbQV'><pre id='yPbQV'><center id='yPbQV'></center></pre></bdo></b><th id='yPbQV'></th></span></q></dt></tr></i><div id='yPbQV'><tfoot id='yPbQV'></tfoot><dl id='yPbQV'><fieldset id='yPbQV'></fieldset></dl></div>
    <bdo id='yPbQV'></bdo><ul id='yPbQV'></ul>

      <small id='yPbQV'></small><noframes id='yPbQV'>

      <tfoot id='yPbQV'></tfoot>

      <legend id='yPbQV'><style id='yPbQV'><dir id='yPbQV'><q id='yPbQV'></q></dir></style></legend>
      1. 如何通过 .NET 中的 Google Drive SDK 使用刷新令牌生

        时间:2023-08-28

            <i id='qCFyX'><tr id='qCFyX'><dt id='qCFyX'><q id='qCFyX'><span id='qCFyX'><b id='qCFyX'><form id='qCFyX'><ins id='qCFyX'></ins><ul id='qCFyX'></ul><sub id='qCFyX'></sub></form><legend id='qCFyX'></legend><bdo id='qCFyX'><pre id='qCFyX'><center id='qCFyX'></center></pre></bdo></b><th id='qCFyX'></th></span></q></dt></tr></i><div id='qCFyX'><tfoot id='qCFyX'></tfoot><dl id='qCFyX'><fieldset id='qCFyX'></fieldset></dl></div>
            <tfoot id='qCFyX'></tfoot>

            <small id='qCFyX'></small><noframes id='qCFyX'>

              • <bdo id='qCFyX'></bdo><ul id='qCFyX'></ul>
                <legend id='qCFyX'><style id='qCFyX'><dir id='qCFyX'><q id='qCFyX'></q></dir></style></legend>
                    <tbody id='qCFyX'></tbody>
                • 本文介绍了如何通过 .NET 中的 Google Drive SDK 使用刷新令牌生成访问令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个使用 Google Drive 访问用户文件的 .NET 应用程序.我能够获得授权码,并且我已经能够通过 AccessToken 和 RefreshToken 交换授权码.问题是我无法刷新访问令牌,并且它会在一小时后过期.

                  I have a .NET application that is using Google Drive to access the user's file. I am able to get the authorization code, and I have been able to exchange the authorization code by the AccessToken and the RefreshToken. The issue is that I cannot refresh the access token, and it expires after an hour.

                  类似于这个问题:如何通过 google drive API 使用刷新令牌生成访问令牌? 除了我在 .NET 中工作(使用 Google.API... DLL).

                  Similar to this question: How to generate access token using refresh token through google drive API? except that I am working in .NET (using the Google.APIs... DLLs).

                  我知道这一点:https://developers.google.com/accounts/docs/OAuth2InstalledApp#refresh 但是,我期望 IAuthorizationState 或 OAuth2Authenticator 对象中提供某种方法来允许我刷新访问令牌.

                  I am aware of this: https://developers.google.com/accounts/docs/OAuth2InstalledApp#refresh however, I am expecting some sort of method available in the IAuthorizationState or OAuth2Authenticator object to allow me refresh the access token.

                  请指教.谢谢.

                  请注意,使用此代码我可以获得访问令牌.只是我希望这段代码在 Google API 中.

                  Please note that using this code I am able to get the Access Token. It is just that I am expecting this code to be inside the Google API.

                      public class OAuth2AccessTokenReponse
                      {
                          public string access_token;
                          public int expires_in;
                          public string token_type; 
                      }
                      public static string refreshAccessToken()
                      {
                          using (System.Net.WebClient client = new System.Net.WebClient())
                          {
                              byte[] response = client.UploadValues("https://accounts.google.com/o/oauth2/token", new System.Collections.Specialized.NameValueCollection(){
                                  {"client_id", ClientID},
                                  {"client_secret", ClientSecret},
                                  {"refresh_token", "XXXXX"},
                                  {"grant_type", "refresh_token"}
                              });
                              string sresponse = System.Text.Encoding.Default.GetString(response);
                              OAuth2AccessTokenReponse o = (OAuth2AccessTokenReponse) Newtonsoft.Json.JsonConvert.DeserializeObject(sresponse, typeof(OAuth2AccessTokenReponse));
                              return o.access_token;        
                          }
                      }
                  

                  推荐答案

                  我研究了一个更合适的示例:GoogleApisSample 的 Tasks.WinForms.NoteMgr... 并找到了解决方案.

                  I studied a more suitable sample: the Tasks.WinForms.NoteMgr of the GoogleApisSample... and with it I found the solution.

                  解决方案在下面的代码中.它的关键部分是调用 arg.RefreshToken(state);

                  The solution is in the code below. The key part of it is calling arg.RefreshToken(state);

                  谢谢.

                      public static Authentication.IAuthenticator UseSavedAuthorization()
                      {          
                  
                          var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description);
                          provider.ClientIdentifier = ClientID;
                          provider.ClientSecret = ClientSecret;
                  
                          OAuth2Authenticator<NativeApplicationClient> auth = new OAuth2Authenticator<NativeApplicationClient>(provider, getState);
                  
                          auth.LoadAccessToken();
                  
                          return auth;             
                      }
                  
                  
                  public static IAuthorizationState getState(NativeApplicationClient arg)
                      {
                          IAuthorizationState state = new AuthorizationState(new[] { TasksService.Scopes.Tasks.GetStringValue(), 
                                  DriveService.Scopes.DriveFile.GetStringValue() , DriveService.Scopes.Drive.GetStringValue()
                          });
                          state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
                  
                          state.RefreshToken = "<refresh token previously saved>";        
                          arg.RefreshToken(state); 
                  
                          return state; 
                      }`
                  

                  这篇关于如何通过 .NET 中的 Google Drive SDK 使用刷新令牌生成访问令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Google 云端硬盘 - 无法通过经过身份验证的 (OAut 下一篇:Google Drive API - 从服务帐户转移所有权

                  相关文章

                  最新文章

                    <legend id='yXqRP'><style id='yXqRP'><dir id='yXqRP'><q id='yXqRP'></q></dir></style></legend>
                    • <bdo id='yXqRP'></bdo><ul id='yXqRP'></ul>
                  1. <i id='yXqRP'><tr id='yXqRP'><dt id='yXqRP'><q id='yXqRP'><span id='yXqRP'><b id='yXqRP'><form id='yXqRP'><ins id='yXqRP'></ins><ul id='yXqRP'></ul><sub id='yXqRP'></sub></form><legend id='yXqRP'></legend><bdo id='yXqRP'><pre id='yXqRP'><center id='yXqRP'></center></pre></bdo></b><th id='yXqRP'></th></span></q></dt></tr></i><div id='yXqRP'><tfoot id='yXqRP'></tfoot><dl id='yXqRP'><fieldset id='yXqRP'></fieldset></dl></div>

                    <small id='yXqRP'></small><noframes id='yXqRP'>

                    1. <tfoot id='yXqRP'></tfoot>