2016-07-26 40 views
1

我尝试从Google OAuth documentation创建一个示例,但无法获得有效的凭证。 在OAuth回调控制器中,我重写了IndexAsync方法。但事件我有一个授权码。我的花饰标记每次都是假的。并结果我有空result.CredentialGoogle oauth .net如何获得有效的凭证?

public override async Task<ActionResult> IndexAsync(AuthorizationCodeResponseUrl authorizationCode, CancellationToken taskCancellationToken) 
    { 
     var result = await new AuthorizationCodeMvcApp(this, new AppFlowMetadata()). 
      AuthorizeAsync(taskCancellationToken); 

    --> **result.Credential == null** 

     var service = new GmailService(new BaseClientService.Initializer 
     { 
      HttpClientInitializer = result.Credential, 
      ApplicationName = "ASP.NET MVC Sample" 
     }); 
     UsersResource.LabelsResource.ListRequest request = service.Users.Labels.List("me"); 

     IList<Label> labels = request.Execute().Labels; 

     var a = request; 
     return View(); 
    } 

我做错了什么?

+0

你有没有偶然发现这个有用的[快速入门](https://developers.google.com/gmail/api/quickstart/dotnet)? – Tholle

+0

是的,但是这个控制台应用程序的例子。我使用asp.net mvc和OAuth。 –

回答

0

正如您所说,您正在使用OAuth 2.0。使用OAuth 2.0访问Google API有一个基本模式。它遵循以下4个步骤:

  • 从Google Developers Console获取OAuth 2.0凭据。
  • 从Google授权服务器获取访问令牌。
  • 将访问令牌发送到API。
  • 如有必要,刷新访问令牌。

欲了解更多详情,您可以在这里按照教程:https://developers.google.com/identity/protocols/OAuth2InstalledApp#handlingtheresponse

您要访问的谷歌开发者控制台获取的OAuth 2.0凭据如已知的谷歌和客户端ID和客户端密钥您的应用程序

除了@Tholle的答案,请通过Official Google Document。只需完成页面其余部分中描述的步骤,大约五分钟后,您将拥有一个向Gmail API发出请求的简单.NET控制台应用程序。

+0

嘿,这是一个非常糟糕的答案。 Guy在Google的例子中遇到了一个问题,你给他提供了一些通用信息和无关链接(如果是MVC应用程序,“安装的应用程序”教程无用 - 而且他使用了官方文档) – Bartosz

相关问题