通过API发布到谷歌加 [英] posting to google plus through api

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

问题描述

试图找到如何从PHP发布到谷歌再加上墙,但即使使用API​​浏览器也得到403 at 得到了

  {
error:{
错误:[
{
domain:global,
reason:禁止,
消息:禁止
}
],
code:403,
message:Forbidden
}
}



我的PHP代码如下所示:

  $ client = new \Google_Client(); 
$ client-> setApplicationName(Speerit);
$ client-> setClientId($ appId);
$ client-> setClientSecret($ appSecret);
$ client-> setAccessType(offline); //离线访问
$ client-> setIncludeGrantedScopes(true); //增量认证
$ client-> setAccessToken(
json_encode(
array(
'access_token'=> $ accessToken,
'expires_in'=> 3600,
'token_type'=>'持有者',


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

//为此用户ID创建网址
$ url = sprintf('https://www.googleapis.com/plusDomains/v1/people/me/activities');

//创建你的HTTP请求对象
$ headers = ['content-type'=> 应用程序/ JSON];
$ body = [
object=> [
originalContent=> Happy Monday!#caseofthemondays,
],
access=> [
items=> [
[type=> domain],
],
domainRestricted=>真,
],
];
$ request = new Request('POST',$ url,$ headers,json_encode($ body));

//发出HTTP请求
$ response = $ client-> send($ request);

//是否有效?
echo $ response-> getStatusCode()。PHP_EOL;
echo $ response-> getReasonPhrase()。PHP_EOL;
echo $ response-> getBody()。PHP_EOL;

以下是官方文档和其他帖子的一些参考资料:

解决方案

首先,我们需要了解有一个免费的Google帐户API,意味着以 @ gmail.com 结尾的帐户,并且有一个针对G套件帐户,意味着帐户结束 @ yourdomain.com 。根据 参考文档 和最近的测试,不可能在免费的Google帐户中使用API​​插入注释(@ gmail.com)



这是仅适用于G Suite帐户*(@ yourdomain.com)*。我必须阅读 插入方法的文档,我可以通过执行以下操作来实现它:

 <?php session_start(); 

require_oncevendor / autoload.php; //包含库

//定义使api调用所需的作用域
$ scopes = array(
https://www.googleapis.com/auth/plus。 stream.write,
https://www.googleapis.com/auth/plus.me
);

//创建客户端对象
$ 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($ b $'access'=> array(
'items'=> array(
'type '=>'domain'
),
'domainRestricted'=> true
),
'verb'=>'post',
'object '=>数组(
'originalContent'=>使用Google API PHP客户端库发布!
),

);

$ newActivity = $ service-> activities-> insert(me,$ activity);


var_dump($ newActivity);

$ b} else {

if(!isset($ _ GET [code])){

$ authUrl = $客户端 - > createAuthUrl();
header('Location:'。filter_var($ authUrl,FILTER_SANITIZE_URL));
$ b} 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禁止的错误。希望在未来,这将是可用的,但目前,只有与Google合作的公司才能访问此特殊API,例如Hootsuite。


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

解决方案

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.

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

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