通过api发布到谷歌加

时间:2017-03-12 01:57:54

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

试图找到如何从PHP发布到谷歌加墙,但即使使用api explorer at也得到了

{
 "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;

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

1 个答案:

答案 0 :(得分:3)

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

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

  | 
  |     function saveDashboard() {
  |              ^

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