Google+ API: How can I use RefreshTokens to avoid requesting access every time my app launches?

前端 未结 5 1449
不知归路
不知归路 2020-12-02 08:45

I\'m trying to use the Google+ API to access info for the authenticated user. I\'ve copied some code from one of the samples, which works fine (below), however I\'m having t

5条回答
  •  一个人的身影
    2020-12-02 09:43

    I also had problems with getting "offline" authentication to work (i.e. acquiring authentication with a refresh token), and got HTTP-response 400 Bad request with a code similar to the OP's code. However, I got it to work with the line client.ClientCredentialApplicator = ClientCredentialApplicator.PostParameter(this.clientSecret); in the Authenticate-method. This is essential to get a working code -- I think this line forces the clientSecret to be sent as a POST-parameter to the server (instead of as a HTTP Basic Auth-parameter).

    This solution assumes that you've already got a client ID, a client secret and a refresh-token. Note that you don't need to enter an access-token in the code. (A short-lived access-code is acquired "under the hood" from the Google server when sending the long-lived refresh-token with the line client.RefreshAuthorization(state);. This access-token is stored as part of the auth-variable, from where it is used to authorize the API-calls "under the hood".)

    A code example that works for me with Google API v3 for accessing my Google Calendar:

    class SomeClass
    {
    
        private string clientID         = "XXXXXXXXX.apps.googleusercontent.com";
        private string clientSecret     = "MY_CLIENT_SECRET";
        private string refreshToken     = "MY_REFRESH_TOKEN";
        private string primaryCal       = "MY_GMAIL_ADDRESS";
    
        private void button2_Click_1(object sender, EventArgs e)
        {
            try
            {
                NativeApplicationClient client = new NativeApplicationClient(GoogleAuthenticationServer.Description, this.clientID, this.clientSecret);
                OAuth2Authenticator auth = new OAuth2Authenticator(client, Authenticate);
    
                // Authenticated and ready for API calls...
    
                // EITHER Calendar API calls (tested):
                CalendarService cal = new CalendarService(auth);
                EventsResource.ListRequest listrequest = cal.Events.List(this.primaryCal);
                Google.Apis.Calendar.v3.Data.Events events = listrequest.Fetch();
                // iterate the events and show them here.
    
                // OR Plus API calls (not tested) - copied from OP's code:
                var plus = new PlusService(auth);
                plus.Key = "BLAH";  // don't know what this line does.
                var me = plus.People.Get("me").Fetch();
                Console.WriteLine(me.DisplayName);
    
                // OR some other API calls...
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error while communicating with Google servers. Try again(?). The error was:\r\n" + ex.Message + "\r\n\r\nInner exception:\r\n" + ex.InnerException.Message);
            }
        }
    
        private IAuthorizationState Authenticate(NativeApplicationClient client)
        {
            IAuthorizationState state = new AuthorizationState(new string[] { }) { RefreshToken = this.refreshToken };
    
            // IMPORTANT - does not work without:
            client.ClientCredentialApplicator = ClientCredentialApplicator.PostParameter(this.clientSecret);
    
            client.RefreshAuthorization(state);
            return state;
        }
    }
    

提交回复
热议问题