我正在vs2017中创建一个Web应用程序以启用Google身份验证。我有一个Login.aspx页,在该页上单击按钮,将以下代码称为:
function onSignIn(googleUser) {
var profile = googleUser.getBasicProfile();
console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead.
// The ID token you need to pass to your backend:
var id_token = googleUser.getAuthResponse().id_token;
console.log("ID Token: " + id_token);
var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://localhost:53028/1.aspx');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send('idtoken=' + id_token);
}
这显示了我的google登录屏幕,并且我可以登录。
1.aspx.cs页面上的代码:
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 = "1ghchabasa5.apps.googleusercontent.com",
ClientSecret = "AG0LvA1wZA11D"
},
new[] { "profile", "https://www.googleapis.com/auth/contacts.readonly" },
"me",
CancellationToken.None).Result;
// Create the service.
var service = new PeopleService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Ms_Test",
});
PeopleResource.GetRequest peopleRequest = service.People.Get("people/me");
peopleRequest.RequestMaskIncludeField = "person.names, person.emailAddresses";
Person profile = peopleRequest.Execute(); // Error on this line
我想获取登录用户的电子邮件地址和生日。
我得到的错误是:
Google.GoogleApiException:'Google.Apis.Requests.RequestError
无效的requestMask.includeFields路径:“ person.email_addresses”。 [400]
错误[
消息[无效的requestMask.includeFields路径:“ person.email_addresses”。]位置[-]原因[badRequest]域[全局]
答案 0 :(得分:0)
提示:字段之间用逗号分隔,请删除您的空间。
要获得生日,您应该使用生日
peopleRequest.RequestMaskIncludeField = "person.emailAddresses,person.birthdays";
如果您要查找的数据不存在,请确保让用户填写Personal info
范围
确保在代码中添加了适当的作用域
示例
new[] { "profile", "https://www.googleapis.com/auth/contacts.readonly", "https://www.googleapis.com/auth/user.birthday.read", "https://www.googleapis.com/auth/userinfo.email" },
测试
如果您运行以下表格,则将查看数据是否存在Test me。运行时,我可以看到我的生日和电子邮件地址。