使用谷歌的API .NET访问用户信息 [英] Access User Info using Google APIs for .NET

查看:220
本文介绍了使用谷歌的API .NET访问用户信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是谷歌的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

 私有静态只读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来访问用户信息。我没有找到这个问题,但是这似乎假设我只是做原料请求,而不是使用官方库

我怎样才能获得用户的电子邮件地址?

解决方案

您应该做到以下几点:

  1. 在除了Google.Apis.Auth的NuGet包你应该安装以下页面:<一href="https://www.nuget.org/packages/Google.Apis.Oauth2.v2">https://www.nuget.org/packages/Google.Apis.Oauth2.v2

  2. 添加 Google.Apis.Oauth2.v2.Oauth2Service.Scope.UserinfoProfile Google.Apis.Oauth2.v2.Oauth2Service.Scope.UserinfoEmail 以作用域列表(当你初始化AppFlowMetadata)。

  3. 现在,添加以下code:

 如果(result.Credential!= NULL)
{
    VAR oauthSerivce =新Google.Apis.Oauth2.v2.Oauth2Service(
        新BaseClientService.Initializer()
        {
            HttpClientInitializer =凭证,
            应用程序名称=驱动API样本,
        });

    VAR USERINFO =等待oauthSerivce.Userinfo.Get()ExecuteAsync()。
    //你可以使用userInfo.Email,性别,FamilyName,...
}
 

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:

  1. In addition to Google.Apis.Auth NuGet package you should install the following page: https://www.nuget.org/packages/Google.Apis.Oauth2.v2

  2. 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).

  3. Now, add the following code:

if (result.Credential != null)
{
    var oauthSerivce = new Google.Apis.Oauth2.v2.Oauth2Service(
        new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "Drive API Sample",
        });

    var userInfo = await oauthSerivce.Userinfo.Get().ExecuteAsync();
    // You can use userInfo.Email, Gender, FamilyName, ... 
}

这篇关于使用谷歌的API .NET访问用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆