Jordy
Jordy

Reputation: 101

Gmail API curl in PHP gives 404 (with fresh token)

I try to connect to my gmail from a PHP page over the gmail API. I will browse to my PHP page to an embedded system to easily receive and send emails. At the moment i received my token using OAuth (https://developers.google.com/identity/sign-in/web). But it doesn't work to receive the mail list, I end up with a google 404 page when i visit my PHP page. Image of the 404 error on my PHP page: https://i.sstatic.net/iuuVQ.jpg PHP Code:

$apikey = "APIKEY";
$token = "received over OAuth in console";

$q   = array("q"=>"PHP HTTP request");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://www.googleapis.com/gmail/v1/users/[email protected]/messages?includeSpamTrash=true&maxResults=5&key=APIKEY");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$q);  //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = [
    "Content-Type: application/x-www-form-urlencoded",
    "Content-Length: ".strlen("PHP HTTP request"),
    "User-Agent: PHP Test",
    "Accept: application/json",
    "Authorization: Bearer ".base64_encode($token),
    "Cookie: key=".base64_encode($apikey),

    "Host: www.XXXXXXXX.be",
    "Referer: http://www.XXXXXXXX.be/test/test.php" //Your referrer address
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec($ch);
curl_close ($ch);
print  $server_output ;

Upvotes: 0

Views: 255

Answers (1)

Jordy
Jordy

Reputation: 101

I have found this website: https://wp-time.com/send-email-via-gmail-api-using-php/ With this it is already possible to send. I am working to import the inbox to view received mails as well and it is working! I also made it doing the sign-in by itself by adding following headers and $mail the gmail and $password the password in plain text. You can mail me if you need help/php-code. When I am done my code should be translated in an API to easily send and receive emails.

'username' => $mail,
'password' => $password,
'grant_type' => 'authorization_code' 

Upvotes: 1

Related Questions