从谷歌获取帖子+;没有浏览器';谷歌的身份验证';通过PHP访问s帐户

从谷歌获取帖子+;没有浏览器';谷歌的身份验证';通过PHP访问s帐户,php,session,authentication,google-plus,posts,Php,Session,Authentication,Google Plus,Posts,我想通过PHP获得google+帖子,但google要求我通过浏览器登录。 是否可以通过PHP提供帐户的身份验证,允许我在每次想要获取帖子时无需登录即可获取帖子 代码是: <?php require_once 'src/Google_Client.php'; require_once 'src/contrib/Google_PlusService.php'; session_start(); $client = new Google_Client(); $client->set

我想通过PHP获得google+帖子,但google要求我通过浏览器登录。 是否可以通过PHP提供帐户的身份验证,允许我在每次想要获取帖子时无需登录即可获取帖子

代码是:

<?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>";
}
?>

是的,只需按您的意愿调用并使用简单的API访问键即可-您可以检索公共信息,但您必须为您检索帖子的用户提供长数字ID,而不是使用字符串“我”。同时也可以查看该库的最新版本。您需要使用启用Google+的项目中的API密钥来设置客户端

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