Question

First, i just want to say what information i am needing to get from the user.

  • Full Name (First/Last)
  • Email Address (Main Account, not @google-plus.com)
  • Location (Country, State, city, address)
  • Youtube Username

To get all of this information, i went ahead and download/installed the PHP-Client Library located here.

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

$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 this:

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

Not too sure what i am doing wrong, or even if i should be using the Google Plus Api to get this information.

I need the Main Account email (Contains all youtube channels) and the users address, etc. That they signed up in their Google Account. How do i get this information and what am i doing wrong in my above example?

I have actually created a chat room dedicated to all things Google API (Go here)

This also brings me to another point. Why is the documentation sooo outdated and not taken care of. Most of the examples that i see are from two years ago, but i am using something that has been updated a couple months ago.

Was it helpful?

Solution

Using the new version of the PHP library (currently 1.0.4-beta on 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);

The scope should be "https://www.googleapis.com/auth/plus.login" (I only tested with the "profile" scope, because I don't have a Google Plus profile).

To get the YouTube channels, you'd have to add the scope "https://www.googleapis.com/auth/youtube" and use the channels#list method, with the 'mine' parameter set to true. The class in the PHP lib is 'Google_Service_YouTube'.

OTHER TIPS

I hit a similar issue and solved it using version 1.1.4 of google-api-php-client

Assuming the following code is used to redirect a user to the Google authentication page:

 $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();

Assuming a valid authentication code is returned to the redirect_url, the following will generate a token from the authentication code as well as provide basic profile information:

 //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;

However, location is not returned. New YouTube accounts don't have YouTube specific usernames

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top