使用访问令牌Google Ionic获取电子邮件地址



我想使用gmail登录在离子框架中访问用户的电子邮件地址和个人资料url。

遵循OAuth代码有助于登录用户并返回访问令牌

 $http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
 var requestToken = "";
 var accessToken = "";
 var clientId = "example";
 var clientSecret = "xxxx";
 $scope.LoginwithGoogle = function() {
   var ref = window.open('https://accounts.google.com/o/oauth2/auth?client_id=' + clientId + '&redirect_uri=http://localhost/callback&scope=https://www.googleapis.com/auth/urlshortener&approval_prompt=force&response_type=code&access_type=offline', '_blank', 'location=no');
   ref.addEventListener('loadstart', function(event) {
    if ((event.url).startsWith("http://localhost/callback")) {
     requestToken = (event.url).split("code=")[1];
     $http({
       method: "post",
       url: "https://accounts.google.com/o/oauth2/token",
       data: "client_id=" + clientId + "&client_secret=" + clientSecret + "&redirect_uri=http://localhost/callback" + "&grant_type=authorization_code" + "&code=" + requestToken
      })
      .success(function(data) {
       var accessToken = data.access_token;
      })
      .error(function(data, status) {
       alert("ERROR: " + data);
      });
     ref.close();
    }
   });

如何使用此访问令牌获取用户电子邮件地址和个人资料图片?

有关Google登录成功的成功,请使用以下代码从Access令牌获取用户公众详细信息。

$http.get("https://www.googleapis.com/plus/v1/people/me", {
                params: {
                    access_token: result.access_token
                }
            })
            .then(function(res) {
                console.log(res);
            }, function(error) {
                alert(JSON.stringify(error));
            });

最新更新