Answer the question
In order to leave comments, you need to log in
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;
}
}
}
{
"id": "*************************",
"email": "********@gmail.com",
"verified_email": true,
"picture": "https://lh3.googleusercontent.com/a-/*************************************************"
}
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"
}
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();
}
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();
}
{
"id": "*************************",
"email": "********@gmail.com",
"verified_email": true,
"picture": "https://lh3.googleusercontent.com/a-/*************************************************"
}
Answer the question
In order to leave comments, you need to log in
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;
}
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 ...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question