Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid JSON response, only when trying to call Service Account from within Wordpress Plugin #1739

Closed
wesleylhandy opened this issue Nov 7, 2019 · 1 comment
Assignees
Labels
type: question Request for information or clarification. Not an issue.

Comments

@wesleylhandy
Copy link

Environment details

  • OS: MacOS 10.14.6
  • PHP version: 7.2
  • Package name and version: google/apiclient": "^2.0

Steps to reproduce

  1. I have created a Custom Endpoint with Wordpress to handle formdata submission that will be written to a Google Sheet. I validate the JSON scheme via justinrainbow/json-schema and then initiate the Google Client.

  2. I import the client secrets for the service account via a PHP array from the WPDB and pass them to the Client.

  3. This code is wrapped in a function. If I have a standard php project with Google autoloaded, the script runs fine and imports the data. However, when I include the function to be called from a registered endpoint on the wp-json api, it errors out:

<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 />

Code example

$spreadsheetId = "1gNM********";
$config = array(
  "type" => "service_account",
  "project_id" => "some_proj_id",
  "private_key_id" => "b4a*********",
  "private_key" => "-----BEGIN PRIVATE KEY-----**********",
  "client_email" => "some_proj_id@something.iam.gserviceaccount.com",
  "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]);
$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']
);
@jdpedrie jdpedrie added the type: question Request for information or clarification. Not an issue. label Nov 7, 2019
@jdpedrie
Copy link
Contributor

jdpedrie commented Nov 7, 2019

Hi @wesleylhandy,

If the same code is working outside of WordPress but fails inside, I don't think it's related to this library. We aren't experts on how WordPress's JSON API works, so are probably not best equipped to assist. Have you tried asking this on Stackoverflow or WordPress Stack Exchange?

I'm sorry we can't be of more assistance. Should it happen that the issue is due to this library, please let me know and I'd be more than happy to continue to help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

2 participants