0
       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?

Tweety01
  • 176
  • 2
  • 17
  • RefreshToken should NOT expire after two days. It's the AccessToken that expires after one hour. Take a look in the doc - https://developers.google.com/accounts/docs/OAuth2, it says " If your application requests enough refresh tokens to go over one of the limits, older refresh tokens stop working.", maybe this is your problem? – peleyal Dec 31 '14 at 15:55
  • Notice also that when you get access and refresh token, it's the library who refreshes your access token when it expires, take a look in https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#credentials, "UserCredential and AuthorizationCodeFlow take care of automatically "refreshing" the token,..." – peleyal Dec 31 '14 at 15:56
  • Token refresh is done automatically by the Google API .NET Client library. If you are doing it in your code, remove it and I bet your problem goes away. – Mike Meinz Dec 31 '14 at 23:50

0 Answers0