如何从谷歌加oauth获取用户电子邮件

Mar*_*rov 33 google-plus

链接:https://sites.google.com/site/oauthgoog/Home/emaildisplayscope

从上面的链接我添加电子邮件范围

https://www.googleapis.com/auth/plus.me
https://www.googleapis.com/auth/userinfo.email
Run Code Online (Sandbox Code Playgroud)

但我不明白以下几点

获得有效的OAuth令牌后,您可以使用它来对电子邮件显示API端点进行API调用:https: //www.googleapis.com/userinfo/email 如果令牌无效,将返回401错误.如果令牌有效,则将返回用户的电子邮件地址.API还将返回一个布尔值,以指示Google是否已验证用户是否拥有该电子邮件地址.但是,大多数安装的应用程序将忽略该值

如何调用Email Display API端点?使用https://www.googleapis.com/userinfo/email

Jos*_*tha 52

将范围设置为:

并使用端点:

https://www.googleapis.com/oauth2/v1/userinfo?alt=json

用法:

get https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=youraccess_token
Run Code Online (Sandbox Code Playgroud)

你会得到JSON:

{ "id": "xx", 
  "name": "xx", 
  "given_name": "xx", 
  "family_name": "xx", 
  "link": "xx", 
  "picture": "xx", 
  "gender": "xx", 
  "locale": "xx" 
}
Run Code Online (Sandbox Code Playgroud)

  • 当我尝试这个,我只得到用户ID(:( (2认同)

Chr*_*and 34

Google+登录的范围已更改.

将范围设置为:

https://www.googleapis.com/auth/plus.login
https://www.googleapis.com/auth/userinfo.email
Run Code Online (Sandbox Code Playgroud)

JavaScript调用如下所示:

gapi.client.load('oauth2', 'v2', function() {
  gapi.client.oauth2.userinfo.get().execute(function(resp) {
    // Shows user email
    console.log(resp.email);
  })
});

gapi.client.load('plus', 'v1', function() {
  gapi.client.plus.people.get( {'userId' : 'me'} ).execute(function(resp) {
    // Shows profile information
    console.log(resp);
  })
});
Run Code Online (Sandbox Code Playgroud)

更多信息https://developers.google.com/+.

编辑:请注意,您不需要plus.me或userinfo.profile的范围.

  • 范围应以空格分隔. (9认同)

小智 17

现在我们将GoogleAPI与Google+配合使用

截至2013年12月,这是最新的网站;

https://developers.google.com/+/

然后是SignIn for Web

https://developers.google.com/+/web/signin/

选择登录流程

- >客户端流程

- > 使用JavaScript启动登录流程(我相信这是最新技术)

https://developers.google.com/+/web/signin/javascript-flow

使用JavaScript启动Google+登录流程

您可以使用gapi.auth.signIn() 方法启动Google+登录流程.此方法为您提供了很大的灵活性,可以决定如何以及何时提示用户授权您的应用并登录.

https://developers.google.com/+/web/api/javascript#gapiauthsigninparameters

gapi.auth.signIn(参数)

启动客户端Google+登录OAuth 2.0流程.与gapi.auth.authorize()类似,不同之处在于此方法支持高级Google+登录功能,包括Android应用程序的无线安装.此方法是使用Google+登录按钮小部件的JavaScript替代方法.

https://developers.google.com/+/web/signin/javascript-flow

  • 尝试使用gapi.auth.signIn()触发登录流程页面

https://google-developers.appspot.com/+/demos/signin_demo_render(SourceCode)

您将尝试这个并为您自己,请关注

第1步:创建客户端ID和客户端密钥

忽略以下步骤,

实际上,您只需要clientID并替换上面的Try it源代码中的一个.

添加范围 https://www.googleapis.com/auth/userinfo.email

var options = {
  'callback': loginFinished,
  'approvalprompt': 'force',
  'clientid': 'YOURID.apps.googleusercontent.com',
  'scope': 'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email',
  'requestvisibleactions': 'http://schemas.google.com/CommentActivity http://schemas.google.com/ReviewActivity',
  'cookiepolicy': 'single_host_origin'
};
Run Code Online (Sandbox Code Playgroud)

gapi.client.load('oauth2', 'v2', function()
  {
    gapi.client.oauth2.userinfo.get()
      .execute(function(resp)
      {
        // Shows user email
        console.log(resp.email);
      });
  });
Run Code Online (Sandbox Code Playgroud)

以下是基于以上内容的完整工作和简明代码:

<html>
<head>
  <title>Google+ Sign-in button demo: rendering with JavaScript</title>
  <style type="text/css">
  html, body { margin: 0; padding:0;}
  #signin-button {
   padding: 5px;
  }
  #oauth2-results pre { margin: 0; padding:0; width: 600px;}
  .hide { display: none;}
  .show { display: block;}
  </style>

  <script src="https://apis.google.com/js/client:platform.js" type="text/javascript"></script>
  <script type="text/javascript">
var loginFinished = function(authResult)
{
  if (authResult)
  {
    console.log(authResult);
  }

  gapi.client.load('oauth2', 'v2', function()
  {
    gapi.client.oauth2.userinfo.get()
      .execute(function(resp)
      {
        // Shows user email
        console.log(resp.email);
      });
  });

};

var options = {
  'callback': loginFinished,
  'approvalprompt': 'force',
  'clientid': 'YOURID.apps.googleusercontent.com',
  'scope': 'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email',
  'requestvisibleactions': 'http://schemas.google.com/CommentActivity http://schemas.google.com/ReviewActivity',
  'cookiepolicy': 'single_host_origin'
};

var renderBtn = function()
{
  gapi.signin.render('renderMe', options);
}
  </script>

</head>
<body onload ="renderBtn()">
   <div id="renderMe"></div>  
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

没有HTML输出,但控制台.所以打开浏览器的Developers Console工具来查看结果.