Google drive Offline Access using .net

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

提交回复
热议问题