Google plus 如何从google plus oauth获取用户电子邮件

Google plus 如何从google plus oauth获取用户电子邮件,google-plus,Google Plus,链接: 从上面的链接中,我添加了电子邮件范围 https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/userinfo.email 但是我不明白下面的内容 拥有有效的OAuth令牌后,可以使用它对电子邮件显示API端点进行API调用: 如果令牌无效,将返回401错误。如果令牌有效,则将返回用户的电子邮件地址。API还将返回一个布尔值,以指示Google是否已验证该用户拥有该电子邮件地址。但是,大多数已安装的

链接:

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

https://www.googleapis.com/auth/plus.me
https://www.googleapis.com/auth/userinfo.email
但是我不明白下面的内容

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

如何调用电子邮件显示API端点?使用

将范围设置为:

https://www.googleapis.com/auth/plus.login
https://www.googleapis.com/auth/userinfo.email
并使用端点:

用法:

get https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=youraccess_token
您将获得JSON:

{ "id": "xx", 
  "name": "xx", 
  "given_name": "xx", 
  "family_name": "xx", 
  "link": "xx", 
  "picture": "xx", 
  "gender": "xx", 
  "locale": "xx" 
}

Google+登录的范围已更改

将范围设置为:

https://www.googleapis.com/auth/plus.login
https://www.googleapis.com/auth/userinfo.email
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);
  })
});
更多信息


编辑:请注意,plus.me或userinfo.profile不需要作用域。

现在我们在Google中使用GoogleAPI+

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

然后是网络登录

在流中选择登录

->客户端流

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

使用JavaScript启动Google+登录流

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

gapi.auth.sign(参数)

启动客户端Google+登录OAuth 2.0流。类似 gapi.auth.authorize()除了此方法支持高级 Google+登录功能,包括Android的空中安装 应用程序。此方法是使用Google的JavaScript替代方法+ 登录按钮小部件

  • 尝试使用gapi.auth.sign()页面触发登录流
(源代码)

你将尝试这个,为了你自己,跟随

步骤1:创建客户端ID和客户端机密

忽略以下步骤

实际上,您只需要clientID,并替换上面的源代码中的clientID

添加范围

以下是基于上述内容的完整、简明的代码:

<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>

Google+登录按钮演示:使用JavaScript呈现
html,正文{边距:0;填充:0;}
#登录按钮{
填充物:5px;
}
#oauth2结果预{边距:0;填充:0;宽度:600px;}
.hide{display:none;}
.show{display:block;}
var loginFinished=函数(authResult)
{
如果(authResult)
{
console.log(authResult);
}
load('oauth2','v2',function()
{
gapi.client.oauth2.userinfo.get()
.执行(功能(resp)
{
//显示用户电子邮件
控制台日志(分别为电子邮件);
});
});
};
变量选项={
“回调”:登录已完成,
“approvalprompt”:“force”,
'clientid':'YOURID.apps.googleusercontent.com',
'范围':'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email',
“请求访问操作”:http://schemas.google.com/CommentActivity http://schemas.google.com/ReviewActivity',
'cookiepolicy':'single_host_origin'
};
var renderBtn=函数()
{
gapi.sign.render('renderMe',选项);
}

没有HTML输出,只有控制台。所以,打开浏览器的开发者控制台工具来查看结果。我在angularjs中,在Ionic框架中做了这件事,它可以工作,试试这个

controller("OauthExample", function($scope, $cordovaOauth, $http) {

    $scope.googleLogin = function() {
        $cordovaOauth.google("YOUR CLIENTID", ["https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile"]).then(function(result) {
            window.localStorage.setItem("access_token", result.access_token);
            $scope.token=JSON.stringify(result);

        }, function(error) {
            console.log(error);
        });
    }


        $scope.getProfileInfo = function() {
            console.log(window.localStorage.getItem('access_token'));
        $http.defaults.headers.common.Authorization = "Bearer " + window.localStorage.getItem("access_token");
        $http.get("https://www.googleapis.com/oauth2/v1/userinfo?alt=json")
            .success(function(data) {
                console.log(data);
                console.log(data.email);
            })
            .error(function(error) {
                console.log(error);
            });
    }

});

它返回{“数据”:{“电子邮件”:”xxx@gmail.com“,“isVerified”:true}}}并且我无法仅获取电子邮件..如果您使用JQuery,请尝试JQuery.parseJSON().email当我尝试此操作时,我仅获取用户id(:(链接已过期。粘贴新文档链接如果您指的是链接,则这些链接不是文档。它们是需要设置到GoogleOAuthParameters对象的作用域和端点。(例如,oauthParameters.setScope();)作用域应以空格分隔。
controller("OauthExample", function($scope, $cordovaOauth, $http) {

    $scope.googleLogin = function() {
        $cordovaOauth.google("YOUR CLIENTID", ["https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile"]).then(function(result) {
            window.localStorage.setItem("access_token", result.access_token);
            $scope.token=JSON.stringify(result);

        }, function(error) {
            console.log(error);
        });
    }


        $scope.getProfileInfo = function() {
            console.log(window.localStorage.getItem('access_token'));
        $http.defaults.headers.common.Authorization = "Bearer " + window.localStorage.getItem("access_token");
        $http.get("https://www.googleapis.com/oauth2/v1/userinfo?alt=json")
            .success(function(data) {
                console.log(data);
                console.log(data.email);
            })
            .error(function(error) {
                console.log(error);
            });
    }

});