我正在vs2017中创建一个用于启用Google身份验证的Web应用程序。我有一个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);
}
这显示我谷歌登录屏幕,我能够登录。
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]域[global]
提示:字段以逗号分隔删除您的空间。
为了生日,你应该使用生日
peopleRequest.RequestMaskIncludeField = "person.emailAddresses,person.birthdays";
如果您要查找的数据不存在,请确保让用户填写qazxsw poi
领域
确保在代码中添加了适当的范围
例
https://www.googleapis.com/auth/userinfo.email
测试一下
如果您运行以下表格,您将看到数据是否存在new[] { "profile", "https://www.googleapis.com/auth/contacts.readonly", "https://www.googleapis.com/auth/user.birthday.read", "https://www.googleapis.com/auth/userinfo.email" },
当我运行它时,我可以看到我的生日和我的电子邮件地址。