我想使用Google Drive API以及簡單的WEB API 2 - Project。 不知何故GoogleWebAuthorizationBroker.cs丟失。Google Drive APi with clean WEB API 2
我用什麼: 的Visual Studio 2013更新4 空的模板與WEB API
我的步驟:
- 創建空的項目包括Web API
- 建設項目
- 更新通過Nuget Packager安裝包
- 安裝包Google.Apis.Drive.v2(使用本指南:https://developers.google.com/drive/web/quickstart/quickstart-cs)
複製並從上面的鏈接的代碼粘貼到一個乾淨的API控制器:
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谷歌說,瀏覽器中出現以下錯誤:
That’s an error. Error: redirect_uri_mismatch
Application: Project Default Service Account
You can email the developer of this application at: [email protected]
The redirect URI in the request: http://example.com:63281/authorize/ did not match a registered redirect 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不匹配。 –
我知道,我已經將這個重定向-URI設置爲我的項目URL。 如果我調試類如上所述缺少,這就是爲什麼錯誤顯示我認爲。 –
你能告訴我,google憑證設置中的重定向網址是什麼? –