【问题标题】:Google drive Offline Access using .netGoogle 使用 .net 驱动离线访问
【发布时间】:2015-01-19 11:56:41
【问题描述】:

我需要从我的应用程序访问谷歌驱动器。 我需要的功能是当特定用户首次对应用程序进行身份验证时,我需要从 API 中获取一些信息,我可以将其存储在我们的最后,然后每当用户想要访问谷歌驱动器时,他不必签名谷歌驱动器,也不是对应用程序进行身份验证,而是使用存储的用户信息,它会自动验证用户的驱动器访问权限。 我见过很多离线访问的例子,但无法解决我的目的。 与以下站点上的 google 驱动器访问具有相同的功能。 https://www.multcloud.com/

任何人请给我做的方式或一些可以满足上述要求的例子。

【问题讨论】:

  • API 文档是怎么说的?你试过什么?

标签: .net authentication google-drive-api offline


【解决方案1】:

Google 客户端库将为您处理所有这些。 nuget.org/packages/Google.Apis.Drive.v2 默认情况下它会使用 FileDatastore。我建议您自己实现 Idatastore 并将刷新令牌存储在数据库中。

登录 Google Drive 的简单示例。使用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 会将所有内容存储在服务器上的 %AppData% 目录中。这对于 Web 应用程序来说确实不理想。我建议您查看DatabaseDataStore.cs 并将其更改为您自己的用途。

从与 Google drive API c# 教程系列一起找到的示例项目中提取的代码here

【讨论】:

    【解决方案2】:

    我在将 Google Drive 与我的应用程序连接时遇到了同样的问题,但现在我找到了一个绝对适合我的解决方案。 Google 客户端库将为您处理所有这些。下载 nuget 包 Google.GData.Client 和 Google.GData.Documents。

    按照我的代码

     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) 此处用户应使用他的帐户登录,以便将您的页面重定向到此处 2)当你回到你的页面时(Oauth Redirect Page) 3) 然后从 url(QueryString) 获取代码和状态 4)发送到服务器(页面加载事件) 5)编写以下代码page_load事件(首先在代码变量中获取查询字符串)

     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) 在这里你会得到 accesstoken 和 requesttoken 2)将其保存在数据库中以备将来使用(离线访问) 3) 传递参数以访问 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);
    

    在这里,您将在 feed 对象中获得所有类型的 2000 个文件,您可以使用 feed.entries 访问文件...希望您喜欢它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-28
      • 2017-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-29
      相关资源
      最近更新 更多