【问题标题】:Google Drive APi with clean WEB API 2具有干净 WEB API 2 的 Google Drive APi
【发布时间】:2014-11-15 17:12:30
【问题描述】:

我想使用 Google Drive API 和一个简单的 WEB API 2 - 项目。 不知何故,GoogleWebAuthorizationBroker.cs 丢失了。

我用什么: 视觉工作室 2013 更新 4 带有 WEB API 的空模板

我的步骤:

  1. 创建包含WEB API的空项目
  2. 构建项目
  3. 通过 Nuget Packager 更新包
  4. 安装包 Google.Apis.Drive.v2(使用本指南:https://developers.google.com/drive/web/quickstart/quickstart-cs
  5. 将上述链接中的代码复制并粘贴到一个干净的 api-controller 中:

    public IEnumerable<string> Get()
    {
        UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
            new ClientSecrets
            {
                ClientId = "228492645857-5599mgcfnhrr74a7er1do1chpam4rnbt.apps.googleusercontent.com",
                ClientSecret = "onoyJQaUazQK4VsKUjD63sDu",
            },
            new[] { DriveService.Scope.Drive },
            "user",
            CancellationToken.None).Result;
    
        // Create the service.
        var service = new DriveService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "Drive API Sample",
        });
    
        File body = new File();
        body.Title = "My document";
        body.Description = "A test document";
        body.MimeType = "text/plain";
    
        byte[] byteArray = System.IO.File.ReadAllBytes(@"C:\Projects\VS\DataAnime\DataAnime\document.txt");
        System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
    
        FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "text/plain");
        request.Upload();
    
        File file = request.ResponseBody;
    
        return new string[] { file.Id, "value2" };
    }
    
  6. 建筑

    6.1 错误:缺少 GoogleWebAuthorizationBroker.cs

    6.2 Google 在浏览器中显示以下错误:

  1. 这是一个错误。 错误:redirect_uri_mismatch

    应用程序:项目默认服务帐户

    您可以发送电子邮件至此应用程序的开发人员:xxxx@gmail.com

    请求中的重定向 URI:http://example.com:63281/authorize/ 与注册的不匹配 重定向 URI。

http://example.com:63281/authorize/ was neither the url i am using for my project nor the url i registered in my developer console (this errorshowing-port is changeing everytime i run this project.

有人知道这是为什么吗? 没有其他来源帮助解决这个奇怪的问题。

【问题讨论】:

  • 我检查了文档,上面写着,当你创建谷歌凭据时,你必须在那里指定redirect url,并且重定向 url 与 localhost:port 不匹配。
  • 我知道,我已经将此重定向 URL 设置为我的项目 URL。如果我调试上面提到的类丢失了,这就是我认为出现错误的原因。
  • 你能告诉我,谷歌凭据设置中的重定向网址是什么?
  • 在开发者工具中,重定向 url 是 example.com:xxxx/signin-google 而我的项目是 example.com:xxxx。老实说,我不知道 /signin-google 来自哪里。然而,错误表明它的 example.com:xxxx/authorize。所以我有点困惑。
  • 我想将/authorize添加到主网址,但正如您所说,谷歌添加了其他内容,不知道为什么,我仍然会尝试找到解决方案。

标签: asp.net asp.net-web-api google-drive-api


【解决方案1】:

我通过在https://console.developers.google.com 上为原生软件而不是网络客户端项目创建一个新项目来解决这个问题,即使我使用的是网络客户端。

只有一件奇怪的事情: 如果我调试我的代码,它仍然说缺少 GoogleWebAuthorizationBroker.cs。 无需调试,我可以做任何我想做的事情。

非常感谢您的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-21
    • 1970-01-01
    • 2013-02-10
    • 1970-01-01
    相关资源
    最近更新 更多