我写了一段代码,删除了Google帐户联系人列表中的所有联系人,并上传了新的联系人列表。我正在使用Outh2进行身份验证。一切正常,但访问令牌在1小时后到期。错误代码是401.有人可以帮我解决这个问题吗?
这是我的代码:
string clientId = ConfigurationManager.AppSettings["token"];
string clientSecret = ConfigurationManager.AppSettings["key"]; ;
string[] scopes = new string[] { "https://www.google.com/m8/feeds/" };
try
{
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
, scopes
, "myaccount@gmail.com"
, CancellationToken.None
, new FileDataStore("test")).Result;
OAuth2Parameters parameters = new OAuth2Parameters();
parameters.AccessToken = credential.Token.AccessToken;
parameters.RefreshToken = credential.Token.RefreshToken;
parameters.AccessType = "offline";
UploadContacts(parameters);
}
谢谢!
答案 0 :(得分:0)
我在我的代码中使用了以下credential.Token.IsExpired方法。它在下面的项目中工作正常。
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets
{
ClientId = Google.ClientId, ClientSecret = Google.ClientSecret },
scopes,
settings.GoogleAccount,
CancellationToken.None).Result;
// check token is expired
if (credential.Token.IsExpired(credential.Flow.Clock))
{
if (credential.RefreshTokenAsync(CancellationToken.None).Result)
{
//The access token is now refreshed.
}
else
{
//The access token has expired but we can't refresh it
}
}
else
{
//The access token is OK, continue.
}
}
我希望这会有所帮助。
安德烈亚斯