5

试图找到如何从 PHP 发布到 google plus wall 但即使使用 api explorer 也得到403

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "forbidden",
    "message": "Forbidden"
   }
  ],
  "code": 403,
  "message": "Forbidden"
 }
}

我的 PHP 代码如下所示:

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

遵循官方文档和其他帖子的一些参考资料

4

1 回答 1

3

首先,我们需要了解免费 google 帐户有一个 API,即以@gmail.com结尾的帐户,而 G Suite 帐户有一个 API,即以 @ yourdomain.com 结尾的帐户。根据参考文档和最近的测试,无法在免费 google 帐户(@gmail.com)上使用 API 插入评论。

这仅适用于 G Suite 帐户(@yourdomain.com)。我必须阅读insert 方法的文档,并且我能够通过执行以下操作使其工作:

<?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));

  }
}

?>

总之,如果您尝试在免费的 gmail.com 帐户上执行此操作,您将收到 403 Forbidden 错误。希望将来可以使用此功能,但目前只有与 Google 合作的公司才能访问此特殊 API,例如 Hootsuite。

于 2017-03-12T23:09:51.450 回答