简体   繁体   中英

How can I get user's email address using Google+ API in Zend Framework1?

I have integrated the Google API in my example to log in my website through Google+ for fetching the details of users email, first name and lastname from his/her profile. When i use the below function

    public function getProfile()
   {
    $endpoint = 'https://www.googleapis.com/oauth2/v1/userinfo';
    return (array) json_decode($this->_getData('profile', $endpoint));
   }

I am getting the output as

    Array ( [firstname] => xxxx [appid] => 11XXXXXXXXXXXXXXXXX92 [email] => [lastname] => YYYY [location] => [username] => XXXX YYYY )

Where as the email is empty.

How to get the email id? What uri must be written in $endpoint here to get the email id along with the other data?

You don't show what scopes you are requesting, but it seems likely that you are not asking for the https://www.googleapis.com/auth/userinfo.email scope.

However, the userinfo scopes and endpoints have been deprecated and will be removed in September 2014 .

Going forward, you should use the email scope with one of the profile scopes (such as profile , https://www.googleapis.com/auth/plus.me , or https://www.googleapis.com/auth/plus.login ) to get access to the user's email, and then use one of the Google+ API endpoints such as people.get with a userid of "me".

@prisoner is spot on, if you request the email scope, you will be able to get the user's email address in plus.people.get API calls. I'll show you how to do this in JavaScript:

Add the Google APIs with the following code before your closing body tag, it's ugly, but a standard for loading external libraries asynchronously:

<script type="text/javascript">
  (function() {
    var po = document.createElement('script');
    po.type = 'text/javascript'; po.async = true;
    po.src = 'https://plus.google.com/js/client:plusone.js';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(po, s);
  })();
</script>

Add the signin 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>

Handle the signin response:

function onSignInCallback(){

...

Load the Google+ API client and handle the auth response:

  gapi.client.load('plus','v1', function(){
    if (authResult['access_token']) {
      gapi.client.plus.people.get({'userId':'me'}).execute(
        function(resp){
          alert(resp.emails[0].value)
        });          
    } else if (authResult['error']) {
      // There was an error, which means the user is not signed in.
      // As an example, you can handle by writing to the console:
      console.log('There was an error: ' + authResult['error']);
    }
    console.log('authResult', authResult);
  });
}

Demo is available here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM