使用谷歌的API .NET访问用户信息用户信息、API、NET

2023-09-04 00:27:09 作者:Mc、五年之约

我使用的是谷歌的API preVIEW(1.7.0)批准通过OAuth2用户。我一直在关注的样本MVC code 。这是我执行 FlowMetadata :

 私有静态只读IAuthorization codeFLOW流量= ...; //令牌的实现

公共静态异步任务< Google.Apis.Auth.OAuth2.Web.Authorization codeWebApp.AuthResult> GetCredentials(控制器控制器的CancellationToken的CancellationToken){
    VAR的结果=等待新的授权codeMvcApp(控制器,新Models.Generic.AppFlowMetadata())AuthorizeAsync(的CancellationToken)。
    如果(result.Credential!= NULL)
    {
         //挣扎在这里。如何让我的请求得到了电子邮件地址?
    }
}
 

我现在有一个有效的 UserCredential ,因此访问令牌,但我找不到任何管理API来访问用户信息。我没有找到这个问题,但是这似乎假设我只是做原料请求,而不是使用官方库

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

解决方案

您应该做到以下几点:

.NET Core WebApi EF访问数据新增用户数据

在除了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:

 如果(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:

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:

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, ... 
}