I
I
Igor Vasiliev2019-09-20 18:18:31
Google
Igor Vasiliev, 2019-09-20 18:18:31

Why doesn't the Google Client API give the username?

Hello.
-
I tried everything that was shown in the documentation, it's kind of a nightmare.
Here are the sources https://github.com/googleapis/google-api-php-client for installing Google Client
Here is my working code:

<?php
namespace xxx\xxx;

use Yii;
use Google_Client;
use Google_Service_Drive;
use Google_Service_Oauth2;
use Google_Service_Plus;
use Google_Service_Exception; 



class Google 
{
    
    public $Client_Id; // Yii::$app->google->Client_Id;
    
    public $Secret; // Yii::$app->google->Secret;
    
    public $RedirectUri; // Yii::$app->google->RedirectUri;

    
    public function getClientGoogle() 
    {
        $client = new Google_Client();
        $client->setClientId(Yii::$app->google->Client_Id);
        $client->setClientSecret(Yii::$app->google->Secret);
        $client->setRedirectUri(Yii::$app->google->RedirectUri);
        $client->setScopes("email");
        $loginUrl = $client->createAuthUrl();
        return $loginUrl;
    }

    public function GetUserProfileInfo($access_token) 
    {	
        $url = 'https://www.googleapis.com/userinfo/v2/me?fields=email,family_name,gender,given_name,hd,id,link,locale,name,picture,verified_email';	
        $ch = curl_init();		
        curl_setopt($ch, CURLOPT_URL, $url);		
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token));
        $data = json_decode(curl_exec($ch), true);
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);		
        if($http_code != 200) 
                throw new \Exception('Error : Failed to get user information');

        return $data;
    }
    
    public function getTokenGoogle() 
    {
        $gclient = new Google_Client();
        $gclient->setClientId(Yii::$app->google->Client_Id);
        $gclient->setClientSecret(Yii::$app->google->Secret);
        $gclient->setRedirectUri(Yii::$app->google->RedirectUri);
        $gclient->setScopes(array('https://www.googleapis.com/auth/userinfo.email','https://www.googleapis.com/auth/userinfo.profile', 'https://www.googleapis.com/auth/plus.me'));

        $get = Yii::$app->request->get();
        $result = $get['code'];
        if($result)
        {
                $token = $gclient->fetchAccessTokenWithAuthCode($result);
                $result = json_decode(json_encode($token));  
                return 'https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token='.$result->access_token;
        }        
    }
}

I get:
{
  "id": "*************************",
  "email": "********@gmail.com",
  "verified_email": true,
  "picture": "https://lh3.googleusercontent.com/a-/*************************************************"
}

How to get full data like in Google Console. ???
GET https://www.googleapis.com/userinfo/v2/me?fields=email%2Cfamily_name%2Cgender%2Cgiven_name%2Chd%2Cid%2Clink%2Clocale%2Cname%2Cpicture%2Cverified_email&key={YOUR_API_KEY}
 
 
Response
 
200
 
- Show headers -
  
{
 "id": "******************",
 "email": "***************@gmail.com",
 "verified_email": true,
 "name": "************",
 "given_name": "******************",
 "picture": "https://lh3.googleusercontent.com/a-/*****************************************",
 "locale": "ru"
}

Tried with and without "me" https://any-api.com/googleapis_com/oauth2/docs/use...
try {
                $token = $gclient->fetchAccessTokenWithAuthCode($result);
                $gclient->setAccessToken($token);
                $oAuth = new \Google_Service_Oauth2($gclient);
                $data = $oAuth->userinfo_v2_me->get();
                print_r($data);
            } catch (\Exception $e) { // Google_Service_Exception 
                echo $e->getMessage();
            }

and
try {
                $token = $gclient->fetchAccessTokenWithAuthCode($result);
                $gclient->setAccessToken($token);
                $oAuth = new \Google_Service_Oauth2($gclient);
                $data = $oAuth->userinfo->get();
                print_r($data);
            } catch (\Exception $e) { // Google_Service_Exception 
                echo $e->getMessage();
            }

I get:
{
  "id": "*************************",
  "email": "********@gmail.com",
  "verified_email": true,
  "picture": "https://lh3.googleusercontent.com/a-/*************************************************"
}

Does anyone know how to get the user's first and last name, or just the first name???

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Igor Vasiliev, 2019-09-23
@Isolution666

The error was in the "ospreys", and this is what you need!

public function getClientGoogle() 
    {
        $client = new Google_Client();
        $client->setClientId(Yii::$app->google->Client_Id);
        $client->setClientSecret(Yii::$app->google->Secret);
        $client->setRedirectUri(Yii::$app->google->RedirectUri);
        $client->setScopes(array('https://www.googleapis.com/auth/userinfo.email','https://www.googleapis.com/auth/userinfo.profile', 'https://www.googleapis.com/auth/plus.me'));

        $loginUrl = $client->createAuthUrl();
        return $loginUrl;
    }

G
grinat, 2019-09-20
@grinat

Well, if the urls are identical, then there are not enough scopes. Either people decided not to show them to public applications, because. according to the doc:
https://www.googleapis.com/auth/userinfo.profile See your personal info, including any personal info you've made publicly available
via https://developers.google.com/identity/protocols/g ...

D
Dimonchik, 2019-09-20
@dimonchik2013

little code
and, generally speaking, is it possible to get them?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question