PHP致命错误:无效的JSON-Google Sheets API服务帐户OAuth错误-Wordpress插件

问题描述 投票:0回答:1

环境详细信息

  • OS:MacOS 10.14.6
  • PHP版本:7.2
  • 包名称和版本:google/apiclient": "^2.0

复制步骤

  1. 我已经用Wordpress创建了一个自定义端点,以处理将要写入Google表格的表单数据提交。我通过justinrainbow/json-schema验证JSON方案,然后启动Google客户端。

  2. 我通过PHP数组从WPDB导入服务帐户的客户机密,并将其传递给Google_Client。现在,我只是对值进行硬编码以确保它们正常工作。

  3. 此代码包装在函数中。如果我有一个没有框架的标准php项目,并且自动加载了Google,则脚本运行良好,读取了现有的工作表,然后在下一个可用行中添加了新行。

但是,当我包含要从wp-json api上的注册端点调用的函数时,在命名空间类中会出错:

<br />
<b>Fatal error</b>: Uncaught Exception: Invalid JSON response in
/wordpress/wp-content/plugins/cbnmovies-google-sheets/vendor/google/auth/src/OAuth2.php:553
Stack trace:
#0 /wordpress/wp-content/plugins/cbnmovies-google-sheets/vendor/google/auth/src/OAuth2.php(503):
Google\Auth\OAuth2-&gt;parseTokenResponse(Object(GuzzleHttp\Psr7\Response))
#1
/wordpress/wp-content/plugins/cbnmovies-google-sheets/vendor/google/auth/src/Credentials/ServiceAccountCredentials.php(123):
Google\Auth\OAuth2-&gt;fetchAuthToken(Object(Google\Auth\HttpHandler\Guzzle6HttpHandler))
#2 /wordpress/wp-content/plugins/cbnmovies-google-sheets/vendor/google/auth/src/FetchAuthTokenCache.php(84):
Google\Auth\Credentials\ServiceAccountCredentials-&gt;fetchAuthToken(Object(Google\Auth\HttpHandler\Guzzle6HttpHandler))
#3 /wordpress/wp-content/plugins/cbnmovies-google-sheets/vendor/google/auth/src/Middleware/AuthTokenMiddleware.php(115):
Google\Auth\FetchAuthTokenCache-&gt;fetchAuthToken(Object(Google\Auth\HttpHandler\Guzzle6HttpHandler))
#4 /wordpress/wp-content/plugins/cbnmovie in
<b>/wordpress/wp-content/plugins/cbnmovies-google-sheets/vendor/google/auth/src/OAuth2.php</b> on line <b>553</b><br />

代码示例

[NAMESPACE INFO(使用自动加载功能,如果您想知道的话)

<?php
/**
 * @package CBNMovies-Google-Sheets
 */

namespace Inc\API;
use Inc\Base\BaseController;
use Inc\API\SheetsApiController;
use JsonSchema\Validator;
use JsonSchema\Constraints\Constraint;
use Google_Client;
use Google_Service_Sheets;
use Google_Service_Sheets_ValueRange;

设置GOOGLE_CLIENT

$config = array(
  "type" => "service_account",
  "project_id" => "some_proj_id",
  "private_key_id" => "b4a*********",
  "private_key" => "-----BEGIN PRIVATE KEY-----**********",
  "client_email" => "[email protected]",
  "client_id" => "1181*********",
  "auth_uri" => "https://accounts.google.com/o/oauth2/auth",
   "token_uri" => "https://oauth2.googleapis.com/token",
   "auth_provider_x509_cert_url" => "https://www.googleapis.com/oauth2/v1/certs",
   "client_x509_cert_url" => "https://www.googleapis.com/robot/v1/metadata/x509/something.iam.gserviceaccount.com"
);
$client = new Google_Client();
$client->setAuthConfig( $config );
$client->setApplicationName('cbn-movie-sites');
$client->setScopes([Google_Service_Sheets::SPREADSHEETS]);

获取和更新现有表

$spreadsheetId = "1gNM********";
$sheets_service = new Google_Service_Sheets( $client );
$first_row = 2;
$range = "A$first_row:I";
$rows = $sheets_service->spreadsheets_values->get( $spreadsheetId, $range, ['majorDimension' => 'ROWS'] );
$row = isset($rows['values']) ? count($rows['values']) + 1 : $first_row;
$range = "A$row:I$row";
$values = [
  [
    $data['First_Name'], 
    $data['Last_Name'], 
    $data['Ministry_Name'], 
    $data['Address'], 
    $data['City'], 
    $data['State'], 
    $data['Zip'], 
    $data['Email'], 
    $data['Phone'], 
    date('c')
  ]
];
$requestBody = new Google_Service_Sheets_ValueRange([
  'range' => $updateRange,
  'majorDimension' => 'ROWS',
  'values' => $values,
]);
$result = $sheets_service->spreadsheets->update(
  $spreadsheetId,
  $range,
  $requestBody,
  ['valueInputOption' => 'USER_ENTERED']
);

我也打开了an issue on github

php wordpress google-sheets google-api-php-client
1个回答
0
投票

您可以在JSON数组中删除date('c')并查看它是否正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.