首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Google api授权引发浏览器

Google api授权引发浏览器
EN

Stack Overflow用户
提问于 2014-12-17 13:40:14
回答 1查看 134关注 0票数 1

我正在写一个小的桌面应用程序,将上传文件到谷歌驱动器。所以当我登录到我的谷歌帐户时,一切都很好,但是当我没有登录时,程序会在这个页面上启动浏览器"https://accounts.google.com/ServiceLogin“。我使用的是以下代码:

代码语言:javascript
复制
        ClientSecrets secret = new ClientSecrets();
        secret.ClientId = "my_client_id";
        secret.ClientSecret = "my_client_secret";
        UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(secret, new[] { DriveService.Scope.Drive }, "user", CancellationToken.None).Result;

        var service = new DriveService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "TestUpload" });

        File body = new File();
        body.Title = "Title123";
        body.Description = "Decription123";
        body.MimeType = "image/png";

        byte[] arr = System.IO.File.ReadAllBytes(fileName);
        System.IO.MemoryStream stream = new System.IO.MemoryStream(arr);

        FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "image/png");
        request.Upload();

那么,我如何才能在不引发浏览器的情况下以编程方式授权呢?

EN

Stack Overflow用户

发布于 2014-12-17 15:39:30

您的大部分问题是您没有保存身份验证的事实。您正在请求访问权限,但没有保存它。在下面的示例中,fileDataStore以这种方式将身份验证信息存储在您pc上%AppData%中的一个文件中,您下次运行该程序时将不需要身份验证,也不会要求您当时登录到谷歌。

代码语言:javascript
复制
//Scopes for use with the Google Drive API
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",
 });

 public static File uploadFile(DriveService _service, string _uploadFile, string _parent) {

            if (System.IO.File.Exists(_uploadFile))
            {
                File body = new File();
                body.Title = System.IO.Path.GetFileName(_uploadFile);
                body.Description = "File uploaded by Diamto Drive Sample";
                body.MimeType = GetMimeType(_uploadFile);
                body.Parents = new List<ParentReference>() { new ParentReference() { Id = _parent } };

                // File's content.
                byte[] byteArray = System.IO.File.ReadAllBytes(_uploadFile);
                System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
                try
                {
                    FilesResource.InsertMediaUpload request = _service.Files.Insert(body, stream, GetMimeType(_uploadFile));
                    request.Upload();
                    return request.ResponseBody;
                }
                catch (Exception e)
                {
                    Console.WriteLine("An error occurred: " + e.Message);
                    return null;
                }
            }
            else {
                Console.WriteLine("File does not exist: " + _uploadFile);
                return null;
            }           

        }

这段代码是从Google Drive C# upload教程中摘录的,如果你想了解更多关于它做什么以及它是如何工作的信息,你可能想要检查一下。在GitHub上的Google-Dotnet-Samples项目中也有一个可用的样例项目。本教程基于该示例项目。

票数 2
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27519019

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档