3

I'm using new beta Google APIs Client Library for .NET to load tasks lists of more than one user. It's classified as "installed aplication" (according to google dev. console) with more than one authorized user accounts. It's really simple to authenticate one user (using google.apis) but I cannot figure out, how to do the same with refresh tokens, and how to use this tokens to get service object.

Sample code:

var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
            GoogleClientSecrets.Load(GoogleTools.GenerateStreamFromString(GoogleTools.Cred)).Secrets,
new[] { TasksService.Scope.Tasks },
"user", CancellationToken.None, new FileDataStore("Tasks.Auth.Store")).Result;         


// Create the service.
service = new TasksService(new BaseClientService.Initializer()
{
     HttpClientInitializer = credential,
     ApplicationName = "Tasks API Sample",
});

I want to construct object credential using refresh token, but I'm really lost and cannot find any appropriate documentation.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
eCorke
  • 858
  • 1
  • 10
  • 24
  • possible duplicate of [.NET Google api 1.7 beta authenticating with refresh token](http://stackoverflow.com/questions/21007866/net-google-api-1-7-beta-authenticating-with-refresh-token) – Linda Lawton - DaImTo Mar 24 '14 at 09:20

2 Answers2

7

You can check if the current Token is expired and Refresh using the code below:

if (credential.Token.IsExpired(Google.Apis.Util.SystemClock.Default))
{
  var refreshResult = credential.RefreshTokenAsync(CancellationToken.None).Result;
}

After that the credential has the credential.Token.AccessToken and credential.Token.RefreshToken refreshed

mqueirozcorreia
  • 905
  • 14
  • 33
1

I already answer to that one in the following thread: .NET Google api 1.7 beta authenticating with refresh token

Hope it will be helpful for you as well.

Community
  • 1
  • 1
peleyal
  • 3,472
  • 1
  • 14
  • 25