Search code examples
phphttp-redirecturigmail-apimismatch

Gmail API error: redirect_uri_mismatch when trying to authenticate. (PHP)


Im using PHP to do my gmail API. So heres how my client its set.

$client = new Google_Client();
$client->setClientId('42XXXXXX3p0o9XXXXXXXXXXXXXXXXX.apps.googleusercontent.com');
$client->setClientSecret('XXXXXXrLXX22Ld-1XXXXXXX');
$client->setRedirectUri('https://mywebsite.com/AilaGmail/gmail.php'); 
$client->addScope('https://mail.google.com/');

When I click to login and authorize my API for the first time, I get this error:

**Error: redirect_uri_mismatch
Request Details
response_type=code
access_type=online
client_id=XXXXX-pXXXXXXX3.apps.googleusercontent.com
redirect_uri=https://mywebsite.com/AilaGmail/gmail.php
state=
scope=https://mail.google.com/
approval_prompt=auto**

Ive already tried some solutions like changing redirect_uri to 'postmessage', but then I got another error:

Error: invalid_request
origin parameter is required!

what can I do? Im using php! Please help...

edit 1* Ive tried more solutions like changing the client_secret.json file, but also did not work :/


Solution

  • OK guys, i did it. My code was missing one important parameter:

    $client->setAuthConfig('client_secret.json');
    

    I still having problems with how to use the code that i get, but the mismatch error is solved.