Refresh token for Google Storage Client2019 Community Moderator ElectionGlass quick start for .NET and refresh tokenCreating Customer in Quickbooks Online from C#OAuth Bearer Access Token sliding expirationNot able to get new refresh token from azure AD with grant type refresh_token called with old refresh tokenOAuth 2.0 How to invalidate granted auth tokens using password grantEbay REST API - application token for listing products and updating inventoryWebAPI Non-interactive authenticating to Azure AD using username & passwordC# set access_type = offline for AuthorizeAsync?Why caching access token is consider bad in oauth2?How to deal with access tokens on apache beam

If Invisibility ends because the original caster casts a non-concentration spell, does Invisibility also end on other targets of the original casting?

Single word request: Harming the benefactor

Do Bugbears' arms literally get longer when it's their turn?

When were linguistics departments first established

Want to switch to tankless, but can I use my existing wiring?

Welcoming 2019 Pi day: How to draw the letter π?

What does おとこえしや mean?

Plywood subfloor won't screw down in a trailer home

What has been your most complicated TikZ drawing?

Do I need to leave some extra space available on the disk which my database log files reside, for log backup operations to successfully occur?

"However" used in a conditional clause?

What is the difference between "shut" and "close"?

Should we release the security issues we found in our product as CVE or we can just update those on weekly release notes?

Why do Australian milk farmers need to protest supermarkets' milk price?

Are there situations where a child is permitted to refer to their parent by their first name?

Excess Zinc in garden soil

Should QA ask requirements to developers?

Is a lawful good "antagonist" effective?

Making a sword in the stone, in a medieval world without magic

Counter-example to the existence of left Bousfield localization of combinatorial model category

validation vs test vs training accuracy, which one to compare for claiming overfit?

Decoding assembly instructions in a Game Boy disassembler

Good allowance savings plan?

What exactly is the purpose of connection links straped between the rocket and the launch pad



Refresh token for Google Storage Client



2019 Community Moderator ElectionGlass quick start for .NET and refresh tokenCreating Customer in Quickbooks Online from C#OAuth Bearer Access Token sliding expirationNot able to get new refresh token from azure AD with grant type refresh_token called with old refresh tokenOAuth 2.0 How to invalidate granted auth tokens using password grantEbay REST API - application token for listing products and updating inventoryWebAPI Non-interactive authenticating to Azure AD using username & passwordC# set access_type = offline for AuthorizeAsync?Why caching access token is consider bad in oauth2?How to deal with access tokens on apache beam










2















I must be stupid I think. Using OAuth.



Have the following piece of code



// Authenticate with Google 
using (MemoryStream stream =
new MemoryStream(GetSecrets()))

string credPath = "token.json";
this.userCredential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);



Works fine and sets this.userCredential as expected



Now I want to create a StorageClient



But StorageClient cannot be created directly from UserCredential.



The only way I can see is to create from the AccessToken



GoogleCredential googleCredential = GoogleCredential.FromAccessToken(this.userCredential.Token.AccessToken); 
return StorageClient.Create(googleCredential);


Problem is this token expires after an hour.



However, in C# I cannot for the life of me find a way to pass in the refresh token (which I have in the UserCredential) and have it refresh itself as needed



It seems possible in Java though, using the .Builder



Does anyone know if this is possible ?










share|improve this question









New contributor




Nick Ambrose is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    2















    I must be stupid I think. Using OAuth.



    Have the following piece of code



    // Authenticate with Google 
    using (MemoryStream stream =
    new MemoryStream(GetSecrets()))

    string credPath = "token.json";
    this.userCredential = GoogleWebAuthorizationBroker.AuthorizeAsync(
    GoogleClientSecrets.Load(stream).Secrets,
    scopes,
    "user",
    CancellationToken.None,
    new FileDataStore(credPath, true)).Result;
    Console.WriteLine("Credential file saved to: " + credPath);



    Works fine and sets this.userCredential as expected



    Now I want to create a StorageClient



    But StorageClient cannot be created directly from UserCredential.



    The only way I can see is to create from the AccessToken



    GoogleCredential googleCredential = GoogleCredential.FromAccessToken(this.userCredential.Token.AccessToken); 
    return StorageClient.Create(googleCredential);


    Problem is this token expires after an hour.



    However, in C# I cannot for the life of me find a way to pass in the refresh token (which I have in the UserCredential) and have it refresh itself as needed



    It seems possible in Java though, using the .Builder



    Does anyone know if this is possible ?










    share|improve this question









    New contributor




    Nick Ambrose is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      2












      2








      2


      1






      I must be stupid I think. Using OAuth.



      Have the following piece of code



      // Authenticate with Google 
      using (MemoryStream stream =
      new MemoryStream(GetSecrets()))

      string credPath = "token.json";
      this.userCredential = GoogleWebAuthorizationBroker.AuthorizeAsync(
      GoogleClientSecrets.Load(stream).Secrets,
      scopes,
      "user",
      CancellationToken.None,
      new FileDataStore(credPath, true)).Result;
      Console.WriteLine("Credential file saved to: " + credPath);



      Works fine and sets this.userCredential as expected



      Now I want to create a StorageClient



      But StorageClient cannot be created directly from UserCredential.



      The only way I can see is to create from the AccessToken



      GoogleCredential googleCredential = GoogleCredential.FromAccessToken(this.userCredential.Token.AccessToken); 
      return StorageClient.Create(googleCredential);


      Problem is this token expires after an hour.



      However, in C# I cannot for the life of me find a way to pass in the refresh token (which I have in the UserCredential) and have it refresh itself as needed



      It seems possible in Java though, using the .Builder



      Does anyone know if this is possible ?










      share|improve this question









      New contributor




      Nick Ambrose is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      I must be stupid I think. Using OAuth.



      Have the following piece of code



      // Authenticate with Google 
      using (MemoryStream stream =
      new MemoryStream(GetSecrets()))

      string credPath = "token.json";
      this.userCredential = GoogleWebAuthorizationBroker.AuthorizeAsync(
      GoogleClientSecrets.Load(stream).Secrets,
      scopes,
      "user",
      CancellationToken.None,
      new FileDataStore(credPath, true)).Result;
      Console.WriteLine("Credential file saved to: " + credPath);



      Works fine and sets this.userCredential as expected



      Now I want to create a StorageClient



      But StorageClient cannot be created directly from UserCredential.



      The only way I can see is to create from the AccessToken



      GoogleCredential googleCredential = GoogleCredential.FromAccessToken(this.userCredential.Token.AccessToken); 
      return StorageClient.Create(googleCredential);


      Problem is this token expires after an hour.



      However, in C# I cannot for the life of me find a way to pass in the refresh token (which I have in the UserCredential) and have it refresh itself as needed



      It seems possible in Java though, using the .Builder



      Does anyone know if this is possible ?







      c# .net google-cloud-platform






      share|improve this question









      New contributor




      Nick Ambrose is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Nick Ambrose is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited Mar 7 at 11:01









      Sagar P. Ghagare

      5292721




      5292721






      New contributor




      Nick Ambrose is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Mar 7 at 9:19









      Nick AmbroseNick Ambrose

      132




      132




      New contributor




      Nick Ambrose is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Nick Ambrose is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Nick Ambrose is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes


















          3














          The simplest way of achieving this is to create the StorageService yourself, and then pass that to the StorageClientImpl constructor:



          var service = new StorageService(new BaseClientService.Initializer

          HttpClientInitializer = userCredential,
          ApplicationName = StorageClientImpl.ApplicationName,
          );

          StorageClient client = new StorageClientImpl(service);


          We might consider adding an overload of Create that takes an IConfigurableHttpClientInitializer instead of a GoogleCredential, in the future.






          share|improve this answer























          • If this works, thank you so much!!!! Seems wrong to mess with an Impl -- hope that won't break going forward. But its way better than continually creating new clients with manually refreshed AccessTokens !

            – Nick Ambrose
            Mar 7 at 10:23












          • @NickAmbrose: No, the "Impl" class is part of the public API - we won't be breaking it. The separation between StorageClient and StorageClientImpl is primarily so that can write test doubles of StorageClient.

            – Jon Skeet
            Mar 7 at 10:39






          • 3





            Jon -- I cannot thank you enough, this solved my problem perfectly! I really appreciate you taking the time to respond.

            – Nick Ambrose
            Mar 7 at 14:33










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );






          Nick Ambrose is a new contributor. Be nice, and check out our Code of Conduct.









          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55040095%2frefresh-token-for-google-storage-client%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          The simplest way of achieving this is to create the StorageService yourself, and then pass that to the StorageClientImpl constructor:



          var service = new StorageService(new BaseClientService.Initializer

          HttpClientInitializer = userCredential,
          ApplicationName = StorageClientImpl.ApplicationName,
          );

          StorageClient client = new StorageClientImpl(service);


          We might consider adding an overload of Create that takes an IConfigurableHttpClientInitializer instead of a GoogleCredential, in the future.






          share|improve this answer























          • If this works, thank you so much!!!! Seems wrong to mess with an Impl -- hope that won't break going forward. But its way better than continually creating new clients with manually refreshed AccessTokens !

            – Nick Ambrose
            Mar 7 at 10:23












          • @NickAmbrose: No, the "Impl" class is part of the public API - we won't be breaking it. The separation between StorageClient and StorageClientImpl is primarily so that can write test doubles of StorageClient.

            – Jon Skeet
            Mar 7 at 10:39






          • 3





            Jon -- I cannot thank you enough, this solved my problem perfectly! I really appreciate you taking the time to respond.

            – Nick Ambrose
            Mar 7 at 14:33















          3














          The simplest way of achieving this is to create the StorageService yourself, and then pass that to the StorageClientImpl constructor:



          var service = new StorageService(new BaseClientService.Initializer

          HttpClientInitializer = userCredential,
          ApplicationName = StorageClientImpl.ApplicationName,
          );

          StorageClient client = new StorageClientImpl(service);


          We might consider adding an overload of Create that takes an IConfigurableHttpClientInitializer instead of a GoogleCredential, in the future.






          share|improve this answer























          • If this works, thank you so much!!!! Seems wrong to mess with an Impl -- hope that won't break going forward. But its way better than continually creating new clients with manually refreshed AccessTokens !

            – Nick Ambrose
            Mar 7 at 10:23












          • @NickAmbrose: No, the "Impl" class is part of the public API - we won't be breaking it. The separation between StorageClient and StorageClientImpl is primarily so that can write test doubles of StorageClient.

            – Jon Skeet
            Mar 7 at 10:39






          • 3





            Jon -- I cannot thank you enough, this solved my problem perfectly! I really appreciate you taking the time to respond.

            – Nick Ambrose
            Mar 7 at 14:33













          3












          3








          3







          The simplest way of achieving this is to create the StorageService yourself, and then pass that to the StorageClientImpl constructor:



          var service = new StorageService(new BaseClientService.Initializer

          HttpClientInitializer = userCredential,
          ApplicationName = StorageClientImpl.ApplicationName,
          );

          StorageClient client = new StorageClientImpl(service);


          We might consider adding an overload of Create that takes an IConfigurableHttpClientInitializer instead of a GoogleCredential, in the future.






          share|improve this answer













          The simplest way of achieving this is to create the StorageService yourself, and then pass that to the StorageClientImpl constructor:



          var service = new StorageService(new BaseClientService.Initializer

          HttpClientInitializer = userCredential,
          ApplicationName = StorageClientImpl.ApplicationName,
          );

          StorageClient client = new StorageClientImpl(service);


          We might consider adding an overload of Create that takes an IConfigurableHttpClientInitializer instead of a GoogleCredential, in the future.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 7 at 9:23









          Jon SkeetJon Skeet

          1092k69279688457




          1092k69279688457












          • If this works, thank you so much!!!! Seems wrong to mess with an Impl -- hope that won't break going forward. But its way better than continually creating new clients with manually refreshed AccessTokens !

            – Nick Ambrose
            Mar 7 at 10:23












          • @NickAmbrose: No, the "Impl" class is part of the public API - we won't be breaking it. The separation between StorageClient and StorageClientImpl is primarily so that can write test doubles of StorageClient.

            – Jon Skeet
            Mar 7 at 10:39






          • 3





            Jon -- I cannot thank you enough, this solved my problem perfectly! I really appreciate you taking the time to respond.

            – Nick Ambrose
            Mar 7 at 14:33

















          • If this works, thank you so much!!!! Seems wrong to mess with an Impl -- hope that won't break going forward. But its way better than continually creating new clients with manually refreshed AccessTokens !

            – Nick Ambrose
            Mar 7 at 10:23












          • @NickAmbrose: No, the "Impl" class is part of the public API - we won't be breaking it. The separation between StorageClient and StorageClientImpl is primarily so that can write test doubles of StorageClient.

            – Jon Skeet
            Mar 7 at 10:39






          • 3





            Jon -- I cannot thank you enough, this solved my problem perfectly! I really appreciate you taking the time to respond.

            – Nick Ambrose
            Mar 7 at 14:33
















          If this works, thank you so much!!!! Seems wrong to mess with an Impl -- hope that won't break going forward. But its way better than continually creating new clients with manually refreshed AccessTokens !

          – Nick Ambrose
          Mar 7 at 10:23






          If this works, thank you so much!!!! Seems wrong to mess with an Impl -- hope that won't break going forward. But its way better than continually creating new clients with manually refreshed AccessTokens !

          – Nick Ambrose
          Mar 7 at 10:23














          @NickAmbrose: No, the "Impl" class is part of the public API - we won't be breaking it. The separation between StorageClient and StorageClientImpl is primarily so that can write test doubles of StorageClient.

          – Jon Skeet
          Mar 7 at 10:39





          @NickAmbrose: No, the "Impl" class is part of the public API - we won't be breaking it. The separation between StorageClient and StorageClientImpl is primarily so that can write test doubles of StorageClient.

          – Jon Skeet
          Mar 7 at 10:39




          3




          3





          Jon -- I cannot thank you enough, this solved my problem perfectly! I really appreciate you taking the time to respond.

          – Nick Ambrose
          Mar 7 at 14:33





          Jon -- I cannot thank you enough, this solved my problem perfectly! I really appreciate you taking the time to respond.

          – Nick Ambrose
          Mar 7 at 14:33












          Nick Ambrose is a new contributor. Be nice, and check out our Code of Conduct.









          draft saved

          draft discarded


















          Nick Ambrose is a new contributor. Be nice, and check out our Code of Conduct.












          Nick Ambrose is a new contributor. Be nice, and check out our Code of Conduct.











          Nick Ambrose is a new contributor. Be nice, and check out our Code of Conduct.














          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55040095%2frefresh-token-for-google-storage-client%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

          2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

          Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme