Cod*_*gue 10 .net c# asp.net-mvc google-api-dotnet-client
我正在使用Google API Preview(1.7.0)通过OAuth2授权用户.我一直在关注示例MVC代码.这是我的实施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?
}
}
Run Code Online (Sandbox Code Playgroud)
我现在有一个有效的UserCredential访问令牌,但我找不到任何托管API来访问用户信息.我确实找到了这个问题,但这似乎假设我只是提出原始请求,而不是使用官方库.
如何获取用户的电子邮件地址?
pel*_*yal 20
您应该执行以下操作:
除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时).
现在,添加以下代码:
Run Code Online (Sandbox Code Playgroud)if (result.Credential != null) { var oauthSerivce = new Google.Apis.Oauth2.v2.Oauth2Service( new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "OAuth 2.0 Sample", }); var userInfo = await oauthSerivce.Userinfo.Get().ExecuteAsync(); // You can use userInfo.Email, Gender, FamilyName, ... }
| 归档时间: |
|
| 查看次数: |
11124 次 |
| 最近记录: |