【发布时间】:2014-11-15 17:12:30
【问题描述】:
我想使用 Google Drive API 和一个简单的 WEB API 2 - 项目。 不知何故,GoogleWebAuthorizationBroker.cs 丢失了。
我用什么: 视觉工作室 2013 更新 4 带有 WEB API 的空模板
我的步骤:
- 创建包含WEB API的空项目
- 构建项目
- 通过 Nuget Packager 更新包
- 安装包 Google.Apis.Drive.v2(使用本指南:https://developers.google.com/drive/web/quickstart/quickstart-cs)
-
将上述链接中的代码复制并粘贴到一个干净的 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.1 错误:缺少 GoogleWebAuthorizationBroker.cs
6.2 Google 在浏览器中显示以下错误:
这是一个错误。 错误: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