C#-People API不返回电子邮件地址 [英] c# - People API does not return email address
问题描述
我正在尝试获取登录用户的电子邮件地址和名称.我可以看到用户名,但电子邮件地址显示为空.这是我编写的用于检索这两个值的代码:
I am trying to get the email address and name of the logged-in user. I can see the name of the user but the email address shows null. Here is the code I have written to retrieve both the values:
using Google.Apis.Auth.OAuth2;
using Google.Apis.People.v1;
using System.Net;
using System.Threading;
using Google.Apis.Services;
using Google.Apis.People.v1.Data;
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = "jfms2vr52dm9eghchabasa5.apps.googleusercontent.com",
ClientSecret = "xxxxxxxxxxxx"
},
new[] { "profile", "https://www.googleapis.com/auth/contacts.readonly", "https://www.googleapis.com/auth/user.birthday.read", "https://www.googleapis.com/auth/userinfo.email" },
"me",
CancellationToken.None).Result;
// Create the service.
var service = new PeopleService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "M_Test1",
});
PeopleResource.GetRequest peopleRequest = service.People.Get("people/me");
peopleRequest.RequestMaskIncludeField = "person.names,person.emailAddresses,person.birthdays";
Person profile = peopleRequest.Execute();
推荐答案
您似乎已更改了范围.为了查看电子邮件地址,用户必须已授予您访问电子邮件范围的权限.登录用户的证书存放在%appdata%中,名称为我,这是您在下面的代码中为其指定的名称.
It looks like you have changed your scopes. In order to see the email address the user must have granted you access to the email scope. The credentails for the user who is logged in are stored in %appdata% under the name me which is the name you gave it in the code below.
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = "jfms2vr52dm9eghchabasa5.apps.googleusercontent.com",
ClientSecret = "xxxxxxxxxxxx"
},
new[] { "profile", "https://www.googleapis.com/auth/contacts.readonly", "https://www.googleapis.com/auth/user.birthday.read", "https://www.googleapis.com/auth/userinfo.email" },
"me", // change this to reset login
CancellationToken.None).Result;
这只是表示用户的字符串.您可以将其更改为其他任何内容,它会提示您重新登录.或者,您也可以删除%appdata%
This is just a string denoting the user. You can change it to anything else and it will prompt you to login again. Or you can just delete the file in %appdata%
这篇关于C#-People API不返回电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!