Retrieving Google-PHP-Client User Information?

Firstly, I just want to say what information I need to get from the user.

  • Full name (first / last)
  • Email address (primary account, not @ google-plus.com)
  • Location (country, state, city, address)
  • Youtube username

To get all this information, I went and downloaded / installed the PHP-Client library located here .

Since this is my first time using the API, I looked around and found the following areas:

$client->setScopes(array('https://www.googleapis.com/auth/youtube.readonly', 'https://www.googleapis.com/auth/userinfo.profile', 'https://www.googleapis.com/auth/userinfo.email'));

For some reason, when I run this:, $myData = $GoogleData->userinfo->get('me');I get the following:

Undefined property: Google_Service_Plus::$userinfo in path/test.php on line 61

Not too sure what I'm doing wrong, or even if I have to use Google Plus Api to get this information.

( YouTube) .. Google. ?

-, API Google ( )

. sooo . , , , , .

+4
2

PHP ( 1.0.4- GitHub):

require_once 'lib/Google/Client.php';
require_once 'lib/Google/Service/Plus.php';

$google_client = new \Google_Client;
$google_client->setClientId(GOOGLE_CLIENT_ID);
$google_client->setClientSecret(GOOGLE_CLIENT_SECRET);
$google_client->setRedirectUri(GOOGLE_REDIRECT_URI);
$google_client->setDeveloperKey(GOOGLE_DEVELOPER_KEY);
$google_client->setAccessType = 'offline';

// Either call:
//     $google_client->authenticate($auth_code);
// with the $auth_code returned by the auth page or 
//     $google_client->setAccessToken($existing_token);
// with a previously generated access token.

$plus = new \Google_Service_Plus($google_client);
$person = $plus->people->get('me');

print_r($person);

" https://www.googleapis.com/auth/plus.login" ( , Google Plus).

YouTube, " https://www.googleapis.com/auth/youtube" channels # list, "mine" true. PHP lib - "Google_Service_YouTube".

+8

1.1.4 google-api-php-client

, Google:

 $client = new Google_Client();
 $client->setAuthConfigFile('/path/to/config/file/here');
 $client->setRedirectUri('https://redirect/url/here');
 $client->setAccessType('offline'); //optional
 $client->setScopes(['profile']); //or email
 $auth_url = $client->createAuthUrl();
 header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
 exit();

, redirect_url, , :

 //assuming a successful authentication code is returned
 $authentication_code = 'code-returned-by-google';
 $client = new Google_Client();
 //.... configure $client object
 $client->authenticate($authentication_code);
 $token_data = $client->getAccessToken();

 //get user email address
 $google_oauth =new Google_Service_Oauth2($client);
 $google_account_email = $google_oauth->userinfo->get()->email;
 //$google_ouath->userinfo->get()->familyName;
 //$google_ouath->userinfo->get()->givenName;
 //$google_ouath->userinfo->get()->name;

. YouTube YouTube

+5

Source: https://habr.com/ru/post/1529578/


All Articles