2014-11-15 79 views
1

我想使用Google Drive API以及简单的WEB API 2 - Project。 不知何故GoogleWebAuthorizationBroker.cs丢失。Google Drive APi with clean WEB API 2

我用什么: 的Visual Studio 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控制器:

    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谷歌说,浏览器中出现以下错误:

  1. 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. 

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

+0

我检查过文件,它说,当你创建谷歌证书时,你必须指定'redirect url',并且重定向url与localhost:port不匹配。 –

+0

我知道,我已经将这个重定向-URI设置为我的项目URL。 如果我调试类如上所述缺少,这就是为什么错误显示我认为。 –

+0

你能告诉我,google凭证设置中的重定向网址是什么? –

回答

0

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

只有一个奇怪的事情: 如果我调试我的代码,它仍然说GoogleWebAuthorizationBroker.cs丢失。没有调试,我可以做我想做的一切。

非常感谢您的帮助。