var REFRESH_TOKEN = System.Configuration.ConfigurationSettings.AppSettings["AdxToken"].ToString();
ClientSecrets secrets = new ClientSecrets()
{
ClientId = clienID,
ClientSecret = clientSecret
};
var token = new TokenResponse { RefreshToken = REFRESH_TOKEN };
var credentials = new UserCredential(new GoogleAuthorizationCodeFlow(
new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = secrets
}),
"user",
token);
我使用上面的方法来验证客户端。我希望自动化我的流程每天运行,问题是上面代码中使用的RefreshToken每两天过期一次。 以下是获取RefreshToken
的代码 private OAuth2Authenticator<WebServerClient> CreateAuthenticator()
{
// Register the authenticator.
var provider = new WebServerClient(GoogleAuthenticationServer.Description);
provider.ClientIdentifier = ClientCredentials.ClientID;
provider.ClientSecret = ClientCredentials.ClientSecret;
var authenticator = new OAuth2Authenticator<WebServerClient>(provider, GetAuthentication);
//{ NoCaching = true }; // TODO(mlinder): Uncomment when CL is merged.
return authenticator;
}
private IAuthorizationState GetAuthentication(WebServerClient client)
{
// If this user is already authenticated, then just return the auth state.
IAuthorizationState state = AuthState;
if (state != null)
{
return state;
}
// Check if an authorization request already is in progress.
state = client.ProcessUserAuthorization(new HttpRequestInfo(HttpContext.Current.Request));
if (state != null && (!string.IsNullOrEmpty(state.AccessToken) || !string.IsNullOrEmpty(state.RefreshToken)))
{
// Store and return the credentials.
HttpContext.Current.Session["AUTH_STATE"] = _state = state;
return state;
}
OutgoingWebResponse response = client.PrepareRequestUserAuthorization(new[] { "https://www.googleapis.com/auth/adexchange.seller", "https://www.googleapis.com/auth/adexchange.seller.readonly" });
response.Headers["Location"] += "&access_type=offline&approval_prompt=force";
response.Send(); // Will throw a ThreadAbortException to prevent sending another response.
return null;
}
有没有办法坚持这个RefreshToken,因为我的任务不能在没有这个的情况下实现自动化?