Search code examples
.netoauth-2.0gdatagoogle-contacts-api

Google Contacts API: how to reuse authorization code?


I succeeded in getting the contacts when I use the following code:

Dim parameters = New OAuth2Parameters()
parameters.ClientId = "XXX.apps.googleusercontent.com"
parameters.ClientSecret = "XXXX"
parameters.RedirectUri = "urn:ietf:wg:oauth:2.0:oob"
parameters.Scope = "https://www.google.com/m8/feeds"

Dim authorizationUrl As String = OAuthUtil.CreateOAuth2AuthorizationUrl(parameters)
Process.Start(authorizationUrl)
Dim authCode As String = InputBox("Authorisation code", "Authorisation Code", "")
parameters.AccessCode = authCode

OAuthUtil.GetAccessToken(parameters)
Dim accessToken = parameters.AccessToken

Dim settings = New RequestSettings("my app name")
Dim cr  = New ContactsRequest(settings)
Dim contcts = cr.GetContacts() 

My question is:

How can I somehow keep the authorization across sessions, so I don't need to ask the user every few hours to authorize the access to his contact list?


Solution

  • We can use the old Access-Token an the Refresh-Token (that get the first time)

     Dim parameters = New OAuth2Parameters()
     parameters.ClientId = "XXXXX.apps.googleusercontent.com"
     parameters.ClientSecret = "XXXXXX"
     parameters.RedirectUri = "urn:ietf:wg:oauth:2.0:oob"
     parameters.Scope = "https://www.google.com/m8/feeds"
     parameters.ResponseType = "code"
     parameters.AccessToken = "",  /* use the value returned from the old call to GetAccessToken here */
     parameters.RefreshToken = "", /* use the value returned from the old call to GetAccessToken here */
    
    OAuthUtil.RefreshAccessToken(parameters)