gpt4 book ai didi

javascript - 从 Google 帐户中提取电子邮件地址

转载 作者:行者123 更新时间:2023-11-29 21:02:38 24 4
gpt4 key购买 nike

我在一个网站上工作,客户要求提供允许使用 Google 和 Facebook 帐户注册/登录的选项。如何从用户的 Google 个人资料中提取电子邮件地址以存储在数据库中?

这是我的代码。问题是我没有完全获取用户配置文件。相反,我只收到用户名。

try
{
WebClient client = new WebClient();
var urlProfile = "https://www.googleapis.com/oauth2/v1/userinfo?access_token="
+ access_token;

string outputData = client.DownloadString(urlProfile);

GoogleUserOutputData serStatus =
JsonConvert.DeserializeObject<GoogleUserOutputData>(outputData);

if (serStatus != null)
{
return serStatus;
// You will get the user information here.
}
}
catch (Exception ex)
{
//catching the exception
}

return null;

最佳答案

这是一种在 JavaScript 中接收数据(电子邮件等)的方法。最后,它会显示带有数据的警报。 (您可以将此数据存储在数据库中。)这是一个带有 Google 按钮的完整工作示例。

<html>

<head>
<title>Demo: Getting an email address using the Google+ Sign-in button</title>
<!-- Include the API client and Google+ client. -->
<script src = "https://plus.google.com/js/client:platform.js" async defer></script>
</head>

<body>
<!-- Container with the Sign-In button. -->
<div id="gConnect" class="button">
<button class="g-signin"
data-scope="email"
data-clientid="Your_Client_ID"
data-callback="onSignInCallback"
data-theme="dark"
data-cookiepolicy="single_host_origin">
</button>
<!-- Textarea for outputting data -->
<div id="response" class="hide">
<textarea id="responseContainer" style="width:100%; height:150px"></textarea>
</div>
</div>
</body>

<script>
/**
* Handler for the signin callback triggered after the user selects an account.
*/
function onSignInCallback(resp) {

gapi.client.load('plus', 'v1', apiClientLoaded);
}

/**
* Sets up an API call after the Google API client loads.
*/
function apiClientLoaded() {

gapi.client.plus.people.get({userId: 'me'}).execute(handleEmailResponse);
}

/**
* Response callback for when the API client receives a response.
*
* @param resp The API response object with the user email and profile information.
*/
function handleEmailResponse(resp) {
var primaryEmail;
var name;
var gender;

for (var i=0; i < resp.emails.length; i++) {
if (resp.emails[i].type === 'account')
primaryEmail = resp.emails[i].value;
if (resp.displayName != null)
name = resp.displayName;
gender = resp.gender;
}
document.getElementById('responseContainer').value = 'Primary email: ' +
primaryEmail + '\n\nFull Response:\n' + JSON.stringify(resp);
ShowAlert("Email: "+primaryEmail +" "+"Name: "+ resp.displayName +" "+"Gender: "+gender);
}

</script>

</html>

有关更多信息和详细信息,您可以(应该)阅读此链接: Getting people and profile information

关于javascript - 从 Google 帐户中提取电子邮件地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45756440/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com