问题描述
我使用的是谷歌的API preVIEW(1.7.0)批准通过OAuth2用户。我一直在关注的<一个href="https://$c$c.google.com/p/google-api-dotnet-client/wiki/OAuth2#Web_Applications_%28using_ASP.NET_MVC%29"相对=nofollow>样本MVC code 。这是我执行 FlowMetadata code>:
私有静态只读IAuthorization codeFLOW流量= ...; //令牌的实现
公共静态异步任务&LT; Google.Apis.Auth.OAuth2.Web.Authorization codeWebApp.AuthResult&GT; GetCredentials(控制器控制器的CancellationToken的CancellationToken){
VAR的结果=等待新的授权codeMvcApp(控制器,新Models.Generic.AppFlowMetadata())AuthorizeAsync(的CancellationToken)。
如果(result.Credential!= NULL)
{
//挣扎在这里。如何让我的请求得到了电子邮件地址?
}
}
我现在有一个有效的 UserCredential
,因此访问令牌,但我找不到任何管理API来访问用户信息。我没有找到这个问题,但是这似乎假设我只是做原料请求,而不是使用官方库
我怎样才能获得用户的电子邮件地址?
您应该做到以下几点:
-
在除了Google.Apis.Auth的NuGet包你应该安装以下页面:https://www.nuget.org/packages/Google.Apis.Oauth2.v2
-
添加 Google.Apis.Oauth2.v2.Oauth2Service.Scope.UserinfoProfile 也 Google.Apis.Oauth2.v2.Oauth2Service.Scope.UserinfoEmail 以作用域列表(当你初始化AppFlowMetadata)。
-
现在,添加以下code:
I'm using the Google APIs Preview (1.7.0) to authorize a user via OAuth2. I've been following the sample MVC code. This is my implementation of FlowMetadata
:
private static readonly IAuthorizationCodeFlow flow = ...; // Implementation of tokens
public static async Task<Google.Apis.Auth.OAuth2.Web.AuthorizationCodeWebApp.AuthResult> GetCredentials(Controller controller, CancellationToken cancellationToken) {
var result = await new AuthorizationCodeMvcApp(controller, new Models.Generic.AppFlowMetadata()).AuthorizeAsync(cancellationToken);
if (result.Credential != null)
{
// Struggling here. How do I make a request to get the e-mail address?
}
}
I now have a valid UserCredential
and therefore Access Token, but I cannot find any managed APIs for accessing the user info. I did find this question, but this appears to assume I am just making raw requests, rather than using the official library.
How can I get the user's e-mail address?
You should do the following:
In addition to Google.Apis.Auth NuGet package you should install the following page: https://www.nuget.org/packages/Google.Apis.Oauth2.v2
Add Google.Apis.Oauth2.v2.Oauth2Service.Scope.UserinfoProfile and also Google.Apis.Oauth2.v2.Oauth2Service.Scope.UserinfoEmail to the scopes list (When you initialize the AppFlowMetadata).
Now, add the following code:
这篇关于使用谷歌的API .NET访问用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!