Trying to find how to post to google plus wall from PHP but got 403 even using the api explorer at got the
{
"error": {
"errors": [
{
"domain": "global",
"reason": "forbidden",
"message": "Forbidden"
}
],
"code": 403,
"message": "Forbidden"
}
}
My PHP code looks like:
$client = new \Google_Client();
$client->setApplicationName("Speerit");
$client->setClientId($appId);
$client->setClientSecret($appSecret);
$client->setAccessType("offline"); // offline access
$client->setIncludeGrantedScopes(true); // incremental auth
$client->setAccessToken(
json_encode(
array(
'access_token' => $accessToken,
'expires_in' => 3600,
'token_type' => 'Bearer',
)
)
);
$client->setScopes(
array(
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/plus.me",
"https://www.googleapis.com/auth/plus.stream.write",
)
);
$client = $client->authorize();
// create the URL for this user ID
$url = sprintf('https://www.googleapis.com/plusDomains/v1/people/me/activities');
// create your HTTP request object
$headers = ['content-type' => 'application/json'];
$body = [
"object" => [
"originalContent" => "Happy Monday! #caseofthemondays",
],
"access" => [
"items" => [
["type" => "domain"],
],
"domainRestricted" => true,
],
];
$request = new Request('POST', $url, $headers, json_encode($body));
// make the HTTP request
$response = $client->send($request);
// did it work??
echo $response->getStatusCode().PHP_EOL;
echo $response->getReasonPhrase().PHP_EOL;
echo $response->getBody().PHP_EOL;
Following official documentation and some references from other posts
First, we need to understand that there is an API for free google accounts, meaning accounts ending @gmail.com and there is an API for G Suite accounts, meaning account ending @yourdomain.com. Based on the reference documentation and recent testings, it is not possible to insert a comment using the API on free google accounts (@gmail.com).
This is only possible with G Suite accounts (@yourdomain.com). I had to read the documentation for the insert method, and I was able to make it work by doing the following:
<?php session_start();
require_once "vendor/autoload.php"; //include library
//define scopes required to make the api call
$scopes = array(
"https://www.googleapis.com/auth/plus.stream.write",
"https://www.googleapis.com/auth/plus.me"
);
// Create client object
$client = new Google_Client();
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/index.php');
$client->setAuthConfig("client_secret.json");
$client->addScope($scopes);
if( isset($_SESSION["access_token"]) ) {
$client->setAccessToken($_SESSION["access_token"]);
$service = new Google_Service_PlusDomains($client);
$activity = new Google_Service_PlusDomains_Activity(
array(
'access' => array(
'items' => array(
'type' => 'domain'
),
'domainRestricted' => true
),
'verb' => 'post',
'object' => array(
'originalContent' => "Post using Google API PHP Client Library!"
),
)
);
$newActivity = $service->activities->insert("me", $activity);
var_dump($newActivity);
} else {
if( !isset($_GET["code"]) ){
$authUrl = $client->createAuthUrl();
header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL));
} else {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/index.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
}
?>
In summary, if you try to do this on a free gmail.com account, you will get the 403 Forbidden error. Hopefully in the future this will be available but for right now, only companies that have partnered with Google has access to this special api, such as Hootsuite.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With