Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting OAuth2 refresh token

I'm trying to use Google's Calendar API to demo out an OAuth2 integration that we'll need to do with another third party. I'm using the DotNetOpenAuth library, and I've been able to get the initial redirect to Google for the Allow / Deny prompt and get the authorization code back.

I now need to get the access token and refresh token, but I only seem to get an access token back, refresh token is null.

This is my controller action method where Google redirects back to after the user Accepts or Denies:

public ActionResult ProcessResponse(string state, string code, string error)
{
  var oAuthClient =
    new WebServerClient(
      new AuthorizationServerDescription
      {
        TokenEndpoint = new Uri("https://accounts.google.com/o/oauth2/token"),
        AuthorizationEndpoint = new Uri("https://accounts.google.com/o/oauth2/auth"),
        ProtocolVersion = ProtocolVersion.V20
      },
      _applicationId, 
      _secret)
      {
        AuthorizationTracker = new TokenManager()
      };


  var authState = oAuthClient.ProcessUserAuthorization();

  var accessToken = authState.AccessToken;
  var refreshToken = authState.RefreshToken;

  return View(new[] { accessToken, refreshToken });
}

Any ideas?

EDIT:

To get the authorization code, I setup the oAuthClient identically to what I did above, and use this method:

oAuthClient.RequestUserAuthorization(new[] { "https://www.googleapis.com/auth/calendar" }, returnUrl);
like image 239
Andy Avatar asked Jun 27 '12 17:06

Andy


1 Answers

I had a similar problem, and solved mine by hand-coding the HttpRequest and HttpResponse handling. See code at: https://stackoverflow.com/a/11361759/29156

like image 131
Jeff Fritz Avatar answered Sep 16 '22 18:09

Jeff Fritz