session - Get post from Google+ without browser's authentication in google's account via PHP -
i'm want google+ posts via php, google requires login via browser. it's possible provide account's authentication via php, allowing me posts without login every time want 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 generate // client id, client secret, , register 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 .... // access token may have been updated lazily. $_session['token'] = $client->getaccesstoken(); } else { // part 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>"; } ?>
yep, call , use simple api access key - can retrieve public information, have supply long numeric id user you're retrieving posts rather using string 'me'. take @ latest version of library well: https://github.com/google/google-api-php-client well. need setup client doing, api key project has 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");
Comments
Post a Comment