18

I followed all these steps.

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

I have client id and client secret.

I got access token now, how can I get user profile and email with access token? And how to check whether user logged in or not?

FelipeAls
  • 21,711
  • 8
  • 54
  • 74
kongaraju
  • 9,344
  • 11
  • 55
  • 78

4 Answers4

29

Using OAuth2, you can request permissions through the scope parameter. (Documentation.) I imagine the scopes you want are https://www.googleapis.com/auth/userinfo.email and https://www.googleapis.com/auth/userinfo.profile.

Then, it's a simple matter to get the profile info once you've obtained your access token. (I assume you've been able to redeem the returned authorization code for an access token?) Just make a get request to https://www.googleapis.com/oauth2/v1/userinfo?access_token={accessToken}, which returns a JSON array of profile data, including email:

{
    "id": "00000000000000",
    "email": "fred.example@gmail.com",
    "verified_email": true,
    "name": "Fred Example",
    "given_name": "Fred",
    "family_name": "Example",
    "picture": "https://lh5.googleusercontent.com/-2Sv-4bBMLLA/AAAAAAAAAAI/AAAAAAAAABo/bEG4kI2mG0I/photo.jpg",
    "gender": "male",
    "locale": "en-US"
}

No guarantees, but try this:

$url = "https://www.googleapis.com/oauth2/v1/userinfo";
$request = apiClient::$io->makeRequest($client->sign(new apiHttpRequest($url, 'GET')));

if ((int)$request->getResponseHttpCode() == 200) {
    $response = $request->getResponseBody();
    $decodedResponse = json_decode($response, true);
    //process user info
  } else {
    $response = $request->getResponseBody();
    $decodedResponse = json_decode($response, true);
    if ($decodedResponse != $response && $decodedResponse != null && $decodedResponse['error']) {
      $response = $decodedResponse['error'];
    }
  }
}
Vineet1982
  • 7,730
  • 4
  • 32
  • 67
  • 1
    If you have the access token then it means you are logged in simply – Vineet1982 Feb 27 '13 at 05:28
  • I mean when I will get access token, I can get access token when user click on login or when page loads with login button – kongaraju Feb 27 '13 at 06:48
  • Hey, its working absolutely perfect. But I read in [Migration Blog](https://developers.google.com/+/api/auth-migration#timetable) that Google will stop service for userinfo.email and userinfo.profile from Sept 2014. So this wont work after that. The alternate they have provided is to use people.me endpoint. How that can be used actually ? – Kartik Domadiya Apr 09 '14 at 07:56
  • @kartik i really dont know about migration thanks for info. I have to through docs and do thing will let u know soon about that. In first attempt it seems they are closing oauth openid and shiftting to g+. – Vineet1982 Apr 09 '14 at 08:43
  • @Vineet1982 Now I'm not getting the email in the response. Is the api updated ? – I'm nidhin May 11 '15 at 06:52
  • @I'mnidhin oauth 1.0 support has been disabled and thus you have to update it to oauth 2.0 – Vineet1982 May 12 '15 at 09:22
  • Just tried it. the api https://www.googleapis.com/oauth2/v1/userinfo?access_token={accessToken} doesn't return email, even the response contains id, name and so on. – Stony Sep 17 '15 at 02:37
  • https://www.googleapis.com/oauth2/v2/userinfo?access_token= will also works – Sivaprasad derangula Jan 20 '17 at 06:50
  • @Sivaprasadderangula Does this API scope come under sensitive scopes?(which needs app verification) – Murali K Sep 05 '20 at 19:49
7

try this

$accessToken = 'access token';
$userDetails = file_get_contents('https://www.googleapis.com/oauth2/v1/userinfo?access_token=' . $accessToken);
$userData = json_decode($userDetails);

if (!empty($userData)) {

  $googleUserId = '';
  $googleEmail = '';
  $googleVerified = '';
  $googleName = '';
  $googleUserName = '';

  if (isset($userData->id)) {
    $googleUserId = $userData->id;
  }
  if (isset($userData->email)) {
    $googleEmail = $userData->email;
    $googleEmailParts = explode("@", $googleEmail);
    $googleUserName = $googleEmailParts[0];
  }
  if (isset($userData->verified_email)) {
    $googleVerified = $userData->verified_email;
  }
  if (isset($userData->name)) {
    $googleName = $userData->name;
  }
} else {

  echo "Not logged In";
}
Dino Babu
  • 5,814
  • 3
  • 24
  • 33
1

You just add this line into your scope Open your Application.cfc and then add this code

     <cfset request.oauthSettings = 
           {scope = "https://www.googleapis.com/auth/userinfo.email+https://www.googleapis.com/auth/userinfo.profile",
                                    client_id = "Your-id",
                                    client_secret = "your-secret",
                                    redirect_uri = "redirect-page",
                                    state = "optional"} />

Now you can get email from function that you can call like this

    <cfscript>              
        public function getProfile(accesstoken) {

            var h = new com.adobe.coldfusion.http();
            h.setURL("https://www.googleapis.com/oauth2/v1/userinfo");
            h.setMethod("get");
            h.addParam(type="header",name="Authorization",value="OAuth #accesstoken#");
            h.addParam(type="header",name="GData-Version",value="3");
            h.setResolveURL(true);
            var result = h.send().getPrefix();
            return deserializeJSON(result.filecontent.toString());
        }       
    </cfscript>

            <cfoutput>
            <cfset show = getProfile(session.ga_accessToken)>
            <cfdump var="#show#">
           </cfoutput>

Hope this can Help many of people to solve this . :)

Azam Alvi
  • 6,918
  • 8
  • 62
  • 89
1
$access_token = 'your access token';
$headers = array('Content-Type: Application/json');
$endpoint = "https://www.googleapis.com/oauth2/v1/userinfo?access_token=".$access_token;


$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_URL, $endPoint);
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, $header);

curl_setopt($soap_do, CURLOPT_FAILONERROR, true);
$result = curl_exec($soap_do);