Google drive Offline Access using .net

后端 未结 2 1512
星月不相逢
星月不相逢 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:30

    I had Same Problem with connecting Google Drive with my Application, but now I found one solution which absolutely Working fine for me. Googles Client lib will handle all that for you.Download nuget Package Google.GData.Client and Google.GData.Documents.

    following my code

     parameters = new OAuth2Parameters()
                    {
                        ClientId = "CLIENT_ID",
                        ClientSecret = "CLIENT_SECRET",
                        RedirectUri = currentURL,//"http://localhost:6600/Home.html",
                        Scope = "https://docs.google.com/feeds/ ",
                        State = "documents",
                        AccessType = "offline",  // offline means it creats a refreshtoken 
                        TokenExpiry = DateTime.Now.AddYears(1)
                    };
                    string url = Google.GData.Client.OAuthUtil.CreateOAuth2AuthorizationUrl(parameters);
    

    1) here user should login with his account so redirect your page here 2) when you came back to your page (Oauth Redirect Page) 3) then take code and state from url(QueryString) 4) send to server (page load event) 5) write following code page_load event(first get query string in code variable)

     OAuth2Parameters parameters = new OAuth2Parameters()
                {
                    ClientId = "274488228041-1aaq8a069h3c7lsjstsl394725tumdlo.apps.googleusercontent.com",
                    ClientSecret = "Ew1EMwe4EB8oLHvKFfDZxQhp",
                    RedirectUri = currentURL,//"http://localhost:6600/Home.html"
                    Scope = "https://docs.google.com/feeds/ ",
                    State = "documents",
                    AccessType = "offline",   // offline means it creats a refreshtoken 
                    TokenExpiry = DateTime.Now.AddYears(1)
                };
                parameters.AccessCode = code;
    
                Google.GData.Client.OAuthUtil.GetAccessToken(parameters);
    

    1) here you will get accesstoken and requesttoken 2) save it in Database for future purpose(offline access) 3) pass parameters to access Google Drive Documents

       GOAuth2RequestFactory requestFactory = new GOAuth2RequestFactory(null, "Infactum", parameters);
                DocumentsService service = new DocumentsService("Infactum");
                service.RequestFactory = requestFactory;
                //requestFactory.CustomHeaders["Authorization"] = "Bearer " + parameters.AccessToken;
                DocumentsListQuery query = new DocumentsListQuery();
                query.NumberToRetrieve = 2000;
    
                // Make a request to the API and get all documents.
                DocumentsFeed feed = service.Query(query);
    

    here, You will get all types of 2000 files in feed object you can access files using feed.entries... hope you like it

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题