posting to google plus through api

夙愿已清 提交于 2019-12-17 17:00:17

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/42742869/posting-to-google-plus-through-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!