34

Link: https://sites.google.com/site/oauthgoog/Home/emaildisplayscope

From the link above I add the email scope

https://www.googleapis.com/auth/plus.me
https://www.googleapis.com/auth/userinfo.email

But I dont understand the following

Once you have a valid OAuth token, you can use it to make API calls to the Email Display API endpoint: https://www.googleapis.com/userinfo/email If the token is not valid, a 401 error will be returned. If the token is valid, then the user's Email address will be returned. The API will also return a boolean value to indicate whether Google has verified that the user owns that Email address. However most installed applications will ignore that value.

How to make a call to the Email Display API endpoint? Using https://www.googleapis.com/userinfo/email

Kara
  • 6,115
  • 16
  • 50
  • 57
Marian Petrov
  • 625
  • 2
  • 9
  • 21

4 Answers4

54

Set your scopes to:

And use the endpoint:

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

Usage:

get https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=youraccess_token

You will get JSON:

{ "id": "xx", 
  "name": "xx", 
  "given_name": "xx", 
  "family_name": "xx", 
  "link": "xx", 
  "picture": "xx", 
  "gender": "xx", 
  "locale": "xx" 
}
bruntime
  • 371
  • 2
  • 13
Joe M
  • 2,527
  • 1
  • 25
  • 25
  • It returns { "data": { "email": "xxx@gmail.com", "isVerified": true } } And i cant get the email only.. – Marian Petrov Jul 23 '12 at 04:20
  • if your using JQuery try jQuery.parseJSON( ).email – Joe M Jul 23 '12 at 04:28
  • 2
    When I try this, I'm getting only user id (:( – Mo. Oct 08 '12 at 00:18
  • 1
    Links are expired. paste new documentation links – kongaraju Feb 27 '13 at 04:35
  • 1
    If you mean the https://www.googleapis.com/auth/XXX links - those are not documentation. They are the scopes and enpoint that needs to be set to your GoogleOAuthParameters object. (e.g. oauthParameters.setScope(); ) – Joe M Feb 27 '13 at 06:49
  • you will get only the email if you only ask for the email in the scopes, depending on the scope you have you can also get the circles, friends, name, etc – Dvid Silva Nov 23 '13 at 19:44
  • Can I get all the emails from the login user account. – Anu Oct 18 '14 at 11:33
34

The scopes have changed for Google+ Sign-In.

Set your scopes to:

https://www.googleapis.com/auth/plus.login
https://www.googleapis.com/auth/userinfo.email

The JavaScript calls look like this:

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);
  })
});

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

Edit: Note that you do not need scopes for plus.me or userinfo.profile.

Chris Cartland
  • 3,058
  • 2
  • 17
  • 14
17

Now we use GoogleAPI with Google+

As on December 2013, here is the most updated website;

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

Then for SignIn for Web

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

Choosing a sign-in flow

->Client-side flow

->Initiate the sign-in flow with JavaScript (I believe this is the latest technology)

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

Initiating the Google+ Sign-In flow with JavaScript

You can start the Google+ Sign-In flow by using the gapi.auth.signIn() method. This method gives you a lot of flexibility to decide how and when to prompt the user to authorize your app and sign-in.

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

gapi.auth.signIn(parameters)

Initiates the client-side Google+ Sign-In OAuth 2.0 flow. Similar to gapi.auth.authorize() except that this method supports the advanced Google+ Sign-In features, including over-the-air installs of Android apps. This method is a JavaScript alternative to using the Google+ Sign-In button widget.

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

  • Try it Page to trigger the sign-in flow with gapi.auth.signIn()

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

You will try this and for your own, follow

Step 1: Create a client ID and client secret

Ignore the following step,

Actually, you need clientID only and replace the one in the source code of Try it above.

Add scope 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'
};

Add

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

Here is the full working and concise code based on the above:

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

No HTML output, but console. so open browser's Developers Console tools to view the result.

1

I did this in angularjs, in the Ionic framework, and it works., try this.

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);
            });
    }

});