Question

I'm want to get google+ posts via PHP, but google requires my login via browser. It's possible provide the account's authentication via PHP, allowing me to get the posts without login every time I want to get the posts?

The code is:

<?php 
require_once 'src/Google_Client.php';
require_once 'src/contrib/Google_PlusService.php';

session_start();

$client = new Google_Client();
$client->setApplicationName("Google+ PHP Starter Application");


// Visit https://code.google.com/apis/console?api=plus to generate your
// client id, client secret, and to register your redirect uri.
 $client->setClientId('clientid');
 $client->setClientSecret('clientsecret');
 $client->setRedirectUri('http://localhost/OLA/google+/simple.php/b.html');
 $client->setDeveloperKey('apikey');
$plus = new Google_PlusService($client);

if (isset($_GET['logout'])) {
  unset($_SESSION['token']);
}

if (isset($_GET['code'])) { // login stuff <-------
  if (strval($_SESSION['state']) !== strval($_GET['state'])) {
    die("The session state did not match.");
  }
  $client->authenticate();
  $_SESSION['token'] = $client->getAccessToken();
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}

if ($client->getAccessToken()) {
  //$me = $plus->people->get('me');
  //print "Your Profile: <pre>" . print_r($me, true) . "</pre>";

  $params = array('maxResults' => 100);
  $activities = $plus->activities->listActivities('me', 'public', $params);
  //print "Your Activities: <pre>" . print_r($activities, true) . "</pre>";

  $params = array(
    'orderBy' => 'recent',
    'maxResults' => '20'//20
  );

    $q = "tag";


  $results = $plus->activities->search($q, $params);
  //code ....

  // The access token may have been updated lazily.
  $_SESSION['token'] = $client->getAccessToken();
} else {
  // This is the part that logs me in via browser <------
  $state = mt_rand();
  $client->setState($state);
  $_SESSION['state'] = $state;

  $authUrl = $client->createAuthUrl();
  print "<a class='login' href='$authUrl'>Connect Me!</a>";
}
?>
Was it helpful?

Solution

Yep, just call as you would and use the simple API access key - you can retrieve public information, but you will have to supply to long numeric ID for the user you're retrieving posts for rather than using the string 'me'. Take a look at the latest version of the library as well: https://github.com/google/google-api-php-client as well. You need to setup your client as you were doing, with an API key from a project which has the Google+ enabled on https://developers.google.com/console

$client = new Google_Client();
$client->setApplicationName("Post Fetcher");
$client->setDeveloperKey('PUT_YOUR_API_KEY_HERE_OR_IT_WONT_WORK');
$plus = new Google_Service_Plus($client);
$activities = $this->plus->activities
                   ->listActivities("118051310819094153327", "public");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top