我已经看到许多questions关于这个问题的答案。有些是针对不同的语言的。对我来说,目前尚不清楚处理此问题的正确方法。
这是我想出的:
public bool Init()
{
UserCredential credential;
ClientSecrets secrets = new ClientSecrets()
{
ClientId = m_ClientID,
ClientSecret = m_ClientSecret
};
if (!m_LogFilePathSet)
return false;
try
{
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
secrets,
m_Scopes,
"user",
CancellationToken.None,
new FileDataStore("MSAToolsSoftware.GMail.Application")).Result;
if (credential.Token.IsExpired(Google.Apis.Util.SystemClock.Default))
{
var refreshResult = credential.RefreshTokenAsync(CancellationToken.None).Result;
}
var initializer = new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = m_ApplicationName
};
m_Service = new GmailService(initializer);
}
catch(Exception ex)
{
SimpleLog.Log(ex);
return false;
}
return true;
}
这是刷新访问令牌(如果需要)的正确方法吗?
谢谢。