Logo

11 views Asked 4 hours ago in PHP
2

Hi Community, I am integrating " login with Google"  into my website using PHP and MYSQL database. I am using the official PHP Google API Client Library  in my website. When I click on the " Login with Google"  button or the "Signup with Google " button, it redirects me to a Google error page that says: "Error 400: redirect_uri_mismatch.

 I registered my application in Google Cloud Console, but I am still dealing with this error. I have created the project in Google Cloud Console for the Google client key and Google client secret.

Can anyone tell me exactly why this happens and how to fix it step-by-step in PHP?

User shakti (9.5K Points)

1 Answer
1
vishalrana (3.5K Points)

answered 4 hours ago

Hey! Don’t worry about this error. This is the most common error, “Redirect URI mismatch" error in Google OAuth 2.0 login. It means you registered the wrong Google Auth path in your Google Cloud Console, which does not exactly match the URL that you are using inside the PHP code.

Here is the step-by-step guide to fix this common error:

1. Check Your PHP Code (google_auth.php)

Look at the line where you are setting the redirect URI in your PHP script. For example:

PHP

$ru = 'http://localhost/web/google-callback.php';
$client->setRedirectUri($ru);

Note down this exact URL. Even a small mistake using http instead of https, or missing a trailing slash (/), will cause this error.

2. Update Google Cloud Console Settings

Now, we need to make sure Google knows this exact URL: This is an important step; be careful with it.

Open the Google Cloud Console.

Select your project and go to APIs & Services > Credentials.

Under OAuth 2.0 Client IDs, click the edit icon (pencil) next to your Web Client.

Scroll down to the Authorized redirect URIs section.

Click Add URI and paste the exact URL from your PHP code here:

Example:

http://localhost/web/google-callback.php

Set the website's correct callback file URL for Google login.

Click on the save button.

3. Important Tips to Remember:

Localhost vs Live Server: If you run your website on localhost, then move to the live website, it’s important to update the Callback URL (like https://example.com/google-callback.php )  in the Google console and the Google auth file.

4. Check your Google Auth file code google-auth.php –

If your problem is not fixed, check your Google auth file code. There is a RedirectUri variable in this file.

$ru = 'http://localhost/web/google-callback.php';
$client->setRedirectUri($ru);

Set your correct callback file URL.

Clear Browser Cache: Sometimes Google takes 2-5 minutes to update these settings globally. If you still see the error, try clearing your browser cache or opening the page in an Incognito/Private window.

This will completely fix your problem! Let me know if you are still getting any errors.

For reference, check the complete tutorial  - 

How to Integrate Login with Google OAuth 2.0 in PHP and MySQL


🚀 Build Your Own Q&A Community Website

This live website is powered by Techno Smarter QA. The complete source code is now available with a powerful admin panel, responsive design, future updates, and support.

Admin Panel Responsive SEO Friendly PHP
Get Source Code

Your Answer
×
Login Required

You must login to continue.

Login Login with Google
Register