Creating Google Drive DriveService with existing a

2019-07-07 03:41发布

I am using ASP.NET Web API and Google.Apis.Drive.v2 Client Library for .NET to upload files to users Drive.

All examples of using the Drive Client Library for .NET require a authentication flow. But how should I create the DriveService when I already know the access token?

2条回答
我命由我不由天
2楼-- · 2019-07-07 04:16

Normally when you access Google Drive using oauth2 you would do something like this

//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
                                          ,Environment.UserName
                                          ,CancellationToken.None
                                          ,new FileDataStore("Daimto.GoogleDrive.Auth.Store")
                                          ).Result;

This creates a UserCredential that you can then use to create a DriveService

DriveService service = new DriveService(new BaseClientService.Initializer()
 {
 HttpClientInitializer = credential,
 ApplicationName = "Drive API Sample",
 });

The problem you are going to have is that FileDataStore stores the credential information on the hard drive. in the %AppData% directory.

What you want to do is create your own implementation of IDataStore and use that instead. The code to do this is extensive. The closest example I have at the moment is one that will load the refresh token from the database. I haven't had time to create a tutorial to go along with this but the project is siting on GitHub. Google-Dotnet-Samples/Authentication you are welcome to play around with it. I would check out the database one you should be able to alter it to let you submit a refreshtoken to it.

查看更多
混吃等死
3楼-- · 2019-07-07 04:42

Despite the fact that have been 2 years since the question has been asked, today I've encountered the same situation and my solution is:

var valid_token = "Pass_the_valid_token_here";
var token = new Google.Apis.Auth.OAuth2.Responses.TokenResponse()
{
    AccessToken = valid_token,
    ExpiresInSeconds = 3600,
    Issued = DateTime.Now
};

var fakeflow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
    ClientSecrets = new ClientSecrets
    {
        ClientId = "fakeClientId",
        ClientSecret = "fakeClientSecret"
    }
});

UserCredential credential = new UserCredential(fakeflow, "fakeUserId", token);
var serviceInitializer = new BaseClientService.Initializer()
{
    //ApplicationName = "Storage Sample",
    HttpClientInitializer = credential
};

DriveService service = new DriveService(serviceInitializer);
查看更多
登录 后发表回答