2

I'm trying to develop a script on PHP to make an HTTP Request to Gmail API as seen here, I used the "Try it" function in the page to test the API and it worked well. In the past a developed a script it could make an HTTP request to Dialogflow which is very similar to Gmail API and I have a PHP script using Google Client library to get the access token for the OAuth 2.0 process, so I reused that script to get the token for this case.

Also, I created a new project on Google Cloud Platform for only Gmail API use, I activated the API and created a Service Account with OAuth generation tokens as the function which I used to get the token.

Y only have a PHP script which generates the content of the email which is in RCF 2822 format and base64 encoding, and the Google Access Token

<?php

// Library access
require 'vendor/autoload.php';
use Google\Auth\ApplicationDefaultCredentials;

// specify the path to your application credentials
putenv('GOOGLE_APPLICATION_CREDENTIALS=client-secret.json');

// Scopes for Gmail API
$scopes = [
  'https://mail.google.com/',
  'https://www.googleapis.com/auth/gmail.modify',
  'https://www.googleapis.com/auth/gmail.compose',
  'https://www.googleapis.com/auth/gmail.addons.current.action.compose',
  'https://www.googleapis.com/auth/gmail.send'
];

// Generation of access token
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope($scopes);

// Obtain of the access token
$token = $client->fetchAccessTokenWithAssertion();

// Email content in RFC 2822 format
$headers = "To: 'To User' <myMail@gmail.com>\r\nFrom: 'From User' <myMail@gmail.com>\r\nSubject: Subject Email\r\n\r\n";
$body = "This is a test for an email send through Gmail RESTful API";

echo base64_encode($headers.$body); // Print of email content in base64 coding
echo "\n";
echo $token["access_token"]; // Print of the access token

?>

The data generated from the script, I send it something like this through Postman

POST https://www.googleapis.com/gmail/v1/users/myMail%40gmail.com/messages/send HTTP/1.1

Authorization: Bearer myAccessToken
Accept: application/json
Content-Type: application/json

{
  "raw": "VG86ICdCcmlhbiBOYXZhcnJvJyA8dGVjdWFuZG8yM0BnbWFpbC5jb20+DQpGcm9tOiAnU29tZnkgQ290aXphZG9yJyA8dGVjdWFuZG8yM0BnbWFpbC5jb20+DQpTdWJqZWN0OiBFc3RhIGVzIGxhIHBydWViYQ0KDQpUaGlzIGlzIGEgdGVzdCBmb3IgYW4gZW1haWwgc2VuZGUgdGhyb3VnaCBHbWFpbCBSRVNUZnVsIEFQSQ=="
}

And I get this response

{
  "error": {
    "code": 400,
    "message": "Precondition check failed.",
    "errors": [
      {
        "message": "Precondition check failed.",
        "domain": "global",
        "reason": "failedPrecondition"
      }
    ],
    "status": "FAILED_PRECONDITION"
  }
}

I changed my access token a bit just to check if it was that but when I do a POST with my bad access token I get an authorization error what it makes me think that my token is correct. With a little research, this error haves the next description: Request can not be executed in the current system state, such as deleting a non-empty directory.

¿Someone haves an idea what I'm doing wrong? ¿Do I have to do something else on Google Cloud Platform?

GuaKMole
  • 31
  • 2

0 Answers0