Google Plus API - Google Plus发布使用PHP插入活动

时间:2017-11-15 15:11:24

标签: php google-plus google-api-php-client google-webmaster-tools google-api-webmasters

我正在使用Google API应用程序使用Google API库向Google Plus发布内容。

根据Google Developer API,我已完成以下步骤。

1)从https://github.com/google/google-api-php-client下载了Google API库。然后安装了作曲家,一切都完成了。

之后在Google开发者控制台中我完成了以下操作。

1)在开发者控制台中创建了项目(我没有使用gmail.com电子邮件)。

2)启用了Google+域名API。

3)我创建的API密钥。

4)创建了OAuth客户端ID。

5)创建服务帐户密钥并下载json文件并将* .json放入我的目录中。

6)启用安全性较低的应用。

然后我的PHP代码

  <?php
require_once 'vendor/autoload.php';
// create the Google client
$client = new Google_Client();
putenv('GOOGLE_APPLICATION_CREDENTIALS=/home/google-plus-api/google-api-php-client/service_account_key.json');
/**
 * Set your method for authentication. Depending on the API, This could be
 * directly with an access token, API key, or (recommended) using
 * Application Default Credentials.
 */
$client->useApplicationDefaultCredentials();
$client->setAuthConfig('/home/google-plus-api/google-api-php-client/service_account_key.json');
$client->setRedirectUri("urn:ietf:wg:oauth:2.0:oob");

$client->addScope(Google_Service_Plus::PLUS_ME);
$authUrl = $client->createAuthUrl();
header("Location: $authUrl");

$plus = new Google_Service_Plus($client);
$plusdomains = new Google_Service_PlusDomains($client);

$postBody = new Google_Service_PlusDomains_Activity();
$postBody['object']['originalContent'] = 'Happy Monday';

$result = $plusdomains->activities->insert('me', $postBody);

$result = $plus->people->get('me');

$response->getBody()->write(var_export($result, true));


?>

一切都已完成当我运行此代码时,我遇到了错误。

PHP Notice:  Indirect modification of overloaded element of Google_Service_PlusDomains_Activity has no effect in /home/google-plus-api/google-api-php-client/plus_test_new.php on line 23 PHP Fatal error:  Uncaught Google_Service_Exception: {  "error": {  "errors": [    {
    "domain": "global",
    "reason": "forbidden",
    "message": "Forbidden"    }   ],   "code": 403,   "message": "Forbidden"  } }

 in /home/google-plus-api/google-api-php-client/src/Google/Http/REST.php:118 Stack trace:
#0 /home/google-plus-api/google-api-php-client/src/Google/Http/REST.php(94): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...')
#1 /home/google-plus-api/google-api-php-client/src/Google/Task/Runner.php(176): Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...')
#2 /home/google-plus-api/google-api-php-client/src/Google/Http/REST.php(58): Google_Task_Runner->run()
#3 /home/google-plus-api/google-api-php-client/src/Google/Client.php(788): Google_Http_REST::execute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...', Array)
#4 /home/google-plus-api/google-api-php-client/ in /home/google-plus-api/google-api-php-client/src/Google/Http/REST.php on line 118

请帮助我任何人避免此错误,并使此编码运行。

谢谢,

0 个答案:

没有答案