Google Contacts API:如何重用授权码?

时间:2015-06-29 10:03:07

标签: .net oauth-2.0 gdata google-contacts

当我使用以下代码时,我成功获取了联系人:

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() 

我的问题是:

我怎么能以某种方式保持跨会话的授权,所以我不需要每隔几个小时就要求用户授权访问他的联系人列表?

1 个答案:

答案 0 :(得分:0)

我们可以使用旧的Access-Token和Refresh-Token(第一次获得)

 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)