0

I was able to get a RefreshToken by following the instruction given in this link : How do I authorise a background web app without user intervention? (canonical ?).

Now I need user credential for getting driveservice object. I have searched a lot but cant find a exact solution.

I can get access token using refresh token, ClientId and ClientSecret following this link :Using OAuth 2.0 for Web Server Applications - offline.

But after that how can I get user credential using that AccessToken? Or any direct way for getting credential using refresh token. Any example or suggestion.

Thanks!

Community
  • 1
  • 1
hasnayn
  • 356
  • 4
  • 22

1 Answers1

1

You might want to try using the Client Lib for this instead of that. All you need to do is run the following code once. The refresh token will be created by FileDataStore and stored in your %appData% directory. The next time it runs it wont prompt you for authentication because it will have it already. You could also create your own implementation of iDataStore to store the refresh token where ever you like. I like current directory LocalFileDatastore.cs.

//Scopes for use with the Google Drive API
string[] scopes = new string[] { DriveService.Scope.Drive,
                                 DriveService.Scope.DriveFile};
// here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
UserCredential credential = 
            GoogleWebAuthorizationBroker
                          .AuthorizeAsync(new ClientSecrets { ClientId = CLIENT_ID
                                                            , ClientSecret = CLIENT_SECRET }
                                          ,scopes
                                          ,"SingleUser"
                                          ,CancellationToken.None
                                          ,new FileDataStore("Daimto.GoogleDrive.Auth.Store")
                                          ).Result;

Code ripped from Google Drive API C# tutorial.

Brian Vallelunga
  • 9,869
  • 15
  • 60
  • 87
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • I dont want to use %appData% or any other Storage. Is it possible getting UserCredential using **Refresh token**, **ClientId** and **ClientSecret** ? – hasnayn Nov 25 '14 at 11:40
  • You can create your own implementation of IDatastore that does it how ever you like. Or you can by pass using the Client Lib and do it all manually http://www.daimto.com/google-3-legged-oauth2-flow/ – Linda Lawton - DaImTo Nov 25 '14 at 11:41
  • any c# example for by pass process ? – hasnayn Nov 25 '14 at 11:49
  • If you check https://github.com/LindaLawton/Google-Dotnet-Samples/blob/master/Authentication/Diamto.Google.Authentication/Diamto.Google.Authentication/DatabaseDataStore.cs you will see how to create one that stores it in the database you should be able to edit that for sending a refreshtoken. Its to big to post here. – Linda Lawton - DaImTo Nov 25 '14 at 11:51