通过 api 发布到 google plus [英] posting to google plus through api

查看:17
本文介绍了通过 api 发布到 google plus的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图找到如何从 PHP 发布到 google plus 墙,但即使使用 api 资源管理器也得到 403 at 得到了

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"
 }
}

我的 PHP 代码如下:

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

推荐答案

首先,我们需要了解有一个免费的 google 帐户的 API,意思是帐户结尾是 @gmail.com 并且有用于 G Suite 帐户的 API,意思是帐户结尾为 @yourdomain.com. 基于 参考文档 和最近的测试,无法在免费谷歌帐户上使用 API 插入评论(@gmail.com).

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).

这仅适用于 G Suite 帐户(@yourdomain.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));

  }
}

?>

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

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.

这篇关于通过 api 发布到 google plus的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆