Google OAuth2: When and how to use refresh token

前端 未结 4 914
面向向阳花
面向向阳花 2021-01-07 01:46

I have an installed c# app with code working that gets the authorization code and exchanges it for an access token. I am storing off the refresh token. I know at some poin

相关标签:
4条回答
  • 2021-01-07 02:21

    Spent the last two days figuring out how to use and renew the access token using the refresh token. My answer is posted in another thread here:

    How Google API V 3.0 .Net library and Google OAuth2 Handling refresh token

    0 讨论(0)
  • 2021-01-07 02:23

    When to use the refresh token:

    From what I understand you use the refresh token when you do not wish to authenticate your app each time it boots. This is extremely useful for debugging during application development (as manual authentication can get annoying after a while).

    How to use the refresh token:

    In the most basic sense:

    public static GOAuth2RequestFactory RefreshAuthenticate(){
        OAuth2Parameters parameters = new OAuth2Parameters(){
            RefreshToken = "<YourRefreshToken>",
            AccessToken = "<AnyOfYourPreviousAccessTokens>",
            ClientId = "<YourClientID>",
            ClientSecret = "<YourClientSecret>",
            Scope = "https://spreadsheets.google.com/feeds https://docs.google.com/feeds",
            AccessType = "offline",
            TokenType = "refresh"
        };
        string authUrl = OAuthUtil.CreateOAuth2AuthorizationUrl(parameters);
        return new GOAuth2RequestFactory(null, "<YourApplicationName>", parameters);
    }
    

    You would use this method in other code with a service, perhaps like this

    GOAuth2RequestFactory requestFactory = RefreshAuthenticate();
    SpreadsheetsService service = new SpreadsheetsService("<YourApplicationName>");
    service.RequestFactory = requestFactory;
    

    Hope this helps!

    0 讨论(0)
  • 2021-01-07 02:25

    If someone still have Problems with refreshing the AccessToken, maybe this can help you finding a solution:

                Google.GData.Client.RequestSettings settings = new RequestSettings("<AppName>");
                Google.GData.Client.OAuth2Parameters parameters = new OAuth2Parameters()
                {
                    ClientId = "<YourClientId>",
                    ClientSecret = "<YourClientSecret>",
                    AccessToken = "<OldAccessToken>", //really necessary?
    
                    RedirectUri = "urn:ietf:wg:oauth:2.0:oob",
                    RefreshToken = "<YourRefreshToken>",
                    AccessType = "offline",
                    TokenType = "refresh",
                    Scope = "https://www.google.com/m8/feeds/" //Change to needed scopes, I used this for ContactAPI
                };
                try
                {
                    Google.GData.Client.OAuthUtil.RefreshAccessToken(parameters);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
    
    0 讨论(0)
  • 2021-01-07 02:36

    An access token will expire after 1 hour - after that time you will begin to receive "401 Invalid Credentials" errors when you make calls against a Google API.

    I'm not familiar with the .NET Google API Client library - the Java and Python libraries will automatically make a request for a new access token when this occurs, depending on how you are creating the DriveService object. I would expect the .NET library to have similar semantics.

    0 讨论(0)
提交回复
热议问题