Google drive Offline Access using .net

后端 未结 2 1514
星月不相逢
星月不相逢 2021-01-16 20:58

I need to have to access the google drive from my application. The functionality,i need is when particular user first authenticate the application, i need to have some infor

2条回答
  •  臣服心动
    2021-01-16 21:31

    Googles Client lib will handle all that for you. nuget.org/packages/Google.Apis.Drive.v2 By default its going to use FileDatastore. I recommend making your own implementation of Idatastore and storing the refresh tokens in the database.

    Simple example of login to Google Drive. Using the Google.apis.drive.v2

     String CLIENT_ID = "{...}.apps.googleusercontent.com";
                String CLIENT_SECRET = "GeE-cD7PtraV0LqyoxqPnOpv";
    
                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;
    
                DriveService service = new DriveService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "Drive API Sample",
                });
    

    Filedatastore will store everthing in the %AppData% directory on the server. This really isn't ideal for web applications. I recommend having a look at DatabaseDataStore.cs and altering it for your own uses.

    Code ripped from the sample project that goes along with the Google drive API c# tutorial series found here

提交回复
热议问题