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);
I am using above method to authenticate client.I want to automate my process to run daily, the problem is the RefreshToken used in above code expires after every two days. Below is the code to get 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;
}
Is there any way to persist this RefreshToken as my task cant be automated without this?