1

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

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

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

之后在谷歌开发者控制台中我做了下面。

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

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

谢谢,

4

0 回答 0