1

I'm using Google API Application for post content to Google Plus using Google API libraries.

As per the Google Developer API, I have done the below steps.

1) Downloaded the google API libraries from https://github.com/google/google-api-php-client. And then installed composer and everything was done.

After that in Google developer console I have done below.

1) Created Project in Developer Console (I'm not using gmail.com email).

2) Enabled Google+ Domain API.

3) API key i have created.

4) OAuth client ID created.

5) Service account keys created and download the json file and put in the *.json in my directory.

6) Enabled less secure apps.

And then My PHP code is

  <?php
require_once 'vendor/autoload.php';
// create the Google client
$client = new Google_Client();
putenv('GOOGLE_APPLICATION_CREDENTIALS=/home/google-plus-api/google-api-php-client/service_account_key.json');
/**
 * Set your method for authentication. Depending on the API, This could be
 * directly with an access token, API key, or (recommended) using
 * Application Default Credentials.
 */
$client->useApplicationDefaultCredentials();
$client->setAuthConfig('/home/google-plus-api/google-api-php-client/service_account_key.json');
$client->setRedirectUri("urn:ietf:wg:oauth:2.0:oob");

$client->addScope(Google_Service_Plus::PLUS_ME);
$authUrl = $client->createAuthUrl();
header("Location: $authUrl");

$plus = new Google_Service_Plus($client);
$plusdomains = new Google_Service_PlusDomains($client);

$postBody = new Google_Service_PlusDomains_Activity();
$postBody['object']['originalContent'] = 'Happy Monday';

$result = $plusdomains->activities->insert('me', $postBody);

$result = $plus->people->get('me');

$response->getBody()->write(var_export($result, true));


?>

Everything is done And when I'm running this code, I'm getting below error.

PHP Notice:  Indirect modification of overloaded element of Google_Service_PlusDomains_Activity has no effect in /home/google-plus-api/google-api-php-client/plus_test_new.php on line 23 PHP Fatal error:  Uncaught Google_Service_Exception: {  "error": {  "errors": [    {
    "domain": "global",
    "reason": "forbidden",
    "message": "Forbidden"    }   ],   "code": 403,   "message": "Forbidden"  } }

 in /home/google-plus-api/google-api-php-client/src/Google/Http/REST.php:118 Stack trace:
#0 /home/google-plus-api/google-api-php-client/src/Google/Http/REST.php(94): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...')
#1 /home/google-plus-api/google-api-php-client/src/Google/Task/Runner.php(176): Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...')
#2 /home/google-plus-api/google-api-php-client/src/Google/Http/REST.php(58): Google_Task_Runner->run()
#3 /home/google-plus-api/google-api-php-client/src/Google/Client.php(788): Google_Http_REST::execute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...', Array)
#4 /home/google-plus-api/google-api-php-client/ in /home/google-plus-api/google-api-php-client/src/Google/Http/REST.php on line 118

Please help me anyone to avoid this error, and make this coding running.

Thanks,

Vignesh Kumar
  • 99
  • 1
  • 4

0 Answers0