【问题标题】:posting to google plus through api通过 api 发布到 google plus
【发布时间】:2017-08-02 05:50:40
【问题描述】:

试图找到如何从 PHP 发布到 google plus wall 但即使使用 api explorer at 也得到了 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;

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

【问题讨论】:

    标签: php google-api google-api-php-client google-apis-explorer


    【解决方案1】:

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

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

    <?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。

    【讨论】:

    • 我使用我的应用凭据执行您的代码并得到相同的响应 { "error": { "errors": [ { "domain": "global", "reason": "forbidden", " message": "Forbidden" } ], "code": 403, "message": "Forbidden" } }
    • 撤消对应用程序的访问权限,然后重试。另外,您使用的是 G Suite 帐户吗? @bitgandtter
    • 我撤销它并返回相同的消息。出于某种原因,我无法分享。我正在使用我的个人帐户
    • @bitgandtter 如果您使用的是个人 @gmail.com 帐户,那么我想这是不可能的。根据this documentation,活动资源没有插入方法。我还没有尝试过,但我会尝试,然后我会确认这一点。
    • 感谢@morfinismo,如果由于某种原因你设法让它工作,请告诉我。非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多