Google使用.net驱动Offline Access

时间:2015-01-19 11:56:42

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

我需要从我的应用程序访问google驱动器。 功能,我需要的是当特定用户首次验证应用程序时,我需要从API获得一些信息,我可以存储在我们的最后,然后在用户想要访问谷歌驱动器后,他没有签到谷歌驱动器,也不是验证应用程序,但使用用户的存储信息,它会自动验证用户的驱动器访问权限。 我见过许多离线访问的例子,但无法解决我的目的。 与以下网站访问谷歌驱动器相同的功能。 https://www.multcloud.com/

任何人请给我一些方法或一些能满足上述要求的例子。

2 个答案:

答案 0 :(得分:0)

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

登录Google云端硬盘的简单示例。使用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会将everthing存储在服务器上的%AppData%目录中。这对于Web应用程序来说并不理想。我建议您查看DatabaseDataStore.cs并根据自己的需要对其进行修改。

从示例项目中删除了代码,该示例项目与找到的{c}教程系列here

一起发布

答案 1 :(得分:0)

我在将Google云端硬盘与我的应用程序连接时遇到了同样的问题,但现在我找到了一个绝对适合我的解决方案。 Googles Client lib将为您处理所有这些。下载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)在这里,您将获得accessstoken和requesttoken 2)将其保存在数据库中以备将来使用(离线访问) 3)传递参数以访问Google云端硬盘文档

   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访问文件...希望您喜欢它

相关问题