PHP Google OAuth Login
PHP Google OAuth API allows users to login in a website with their Google credentials. A user with a Google account need not remember yet another username/password for a web application.
Google OAuth Prerequisites
- Create Google API project and get OAuth credentials.
- Here are the steps
- After this click on Create New Project link for create new project.
- Enter Project Name and click on Create button.
- Once you have created a new project then you can see your project list on web page.
- After this click on Google API logo for go to the home page.
- Once you have redirected to home page then select project from the project select box.
- After click on project select box, then one modal will popup and under this, you can find a list of project, so select your project.
- Now from left menu, you have to click on OAuth consent screen.
- Once you have click on OAuth consent screen, then one page will load, here you have to define application name and after this click on save button.
- When you have to click on save button, then after page will redirect another page, and here you have to click on Create credential button, so one drop-down menu will appear and from this, you have to select OAuth client ID.
- After click on OAuth client ID menu then you have redirected to another page, and here you can find different Application type.
- From different Application type option, you have to select Web application. Once you have select the Web application option, then one form will appear on the web page. Here you have to define Name and you have also define Authorized redirect URIs field and lastly click on Create button.
- Once you have click on create button, then you can get your Client ID and your client secret key. You have to copy both keys for future use for implement Login using Google account using PHP.
- Download / Install PHP Google API client library.
- copy this on CMD:- composer require google/apiclient:"^2.0"
click here Google Developers Console
Steps to implement Google authentication
- Create / Send authentication request.
- Access user data from Google.
Create Google OAuth Credentials
1: Create a new project and get the corresponding OAuth credentials using Google developer console page.
Language-specific requirements
To run any of the code samples in this document, you'll need a Google account, access to the Internet, and a web browser. If you are using one of the API client libraries, also see the language-specific requirements below.
To run the PHP code samples in this document, you'll need
- PHP 5.4 or greater with the command-line interface (CLI) and JSON extension installed.
- The Composer dependency management tool.
- The Google APIs Client Library for PHP:
- composer require google/apiclient:"^2.0"
First we need to create a config.php page to enter all the data that we get while creating our google API / OAuth Credentials
config.php page just copy the below code and replace with your own Google OAuth Credentials
Copy Source Code
Now, create a index.php page and then add the following code
Copy Source Code
At last, create a logout.php page and this code
Copy Source Code
This is it. I want you to try by yourself and comment below about your experience.
Great Video 👌👌👌
ReplyDeleteawesome video sir,(hahhhhahah maja a gaya)
ReplyDeleteMake video on send sms from localhost
ReplyDeletegood work
ReplyDeleteCan you make a tutorial for Mac users as well? I'm facing authentication errors.
ReplyDeleteHello,
ReplyDeleteI am not able to fetch data, although login get successful but whenever I want to print username or email it shows blank.
I use something like:
$email = $data['email'];
at the place of:
$_SESSION['user_email_address'] = $data['email'];
As I already check the code two times, the placement of session start and all that but I dont able to fetch the information... although it get successful login...
This issue is solved.
DeleteActually I keep two different pages for login and dashboard, so I keep your index file as it is and create session in that and then insert an header to my dashboard.
Yeeh! Thanks for this option of reply.
I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. hotmail.com loign
ReplyDeleteThe Best Video tutorial i learned alot respect From Pakistan... There is one problem im facing i couldn't be able to access profile picture.. only i can be able to access email and name.. nothing else please need guide why..
ReplyDeletethanks for making this wonderful video! thanks a lot
ReplyDelete
ReplyDeleteFatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: `POST https://oauth2.googleapis.com/revoke` resulted in a `400 Bad Request` response: { "error": "invalid_request", "error_description": "Bad Request" } in C:\xampp\htdocs\gopala\gopala_fastion\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php:111 Stack trace: #0 C:\xampp\htdocs\gopala\gopala_fastion\vendor\guzzlehttp\guzzle\src\Middleware.php(66): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response)) #1 C:\xampp\htdocs\gopala\gopala_fastion\vendor\guzzlehttp\promises\src\Promise.php(203): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Response)) #2 C:\xampp\htdocs\gopala\gopala_fastion\vendor\guzzlehttp\promises\src\Promise.php(156): GuzzleHttp\Promise\Promise::callHandler(1, Object(GuzzleHttp\Psr7\Response), Array) #3 C:\xampp\htdocs\gopala\gopala_fastion\vendor\guzzlehttp\promises\src\TaskQueue.php(47): GuzzleHttp\Promise\Promise::GuzzleHttp\Promise\{clo in C:\xampp\htdocs\gopala\gopala_fastion\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php on line 111
Hello i got this error after selecting the google account for login..pls help
ReplyDeleteFatal error: Uncaught GuzzleHttp\Exception\RequestException: cURL error 77: error setting certificate verify locations: CAfile: C:\xampp\apache\bin\curl-ca-bundle.crt CApath: none (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) in C:\xampp\htdocs\login\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php:208 Stack trace: #0 C:\xampp\htdocs\login\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php(162): GuzzleHttp\Handler\CurlFactory::createRejection(Object(GuzzleHttp\Handler\EasyHandle), Array) #1 C:\xampp\htdocs\login\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php(109): GuzzleHttp\Handler\CurlFactory::finishError(Object(GuzzleHttp\Handler\CurlHandler), Object(GuzzleHttp\Handler\EasyHandle), Object(GuzzleHttp\Handler\CurlFactory)) #2 C:\xampp\htdocs\login\vendor\guzzlehttp\guzzle\src\Handler\CurlHandler.php(45): GuzzleHttp\Handler\CurlFactory::finish(Object(GuzzleHttp\Handler\CurlHandler), Object(GuzzleHttp\Handler\EasyHandle), Object(GuzzleHttp\Handler\CurlFactory)) #3 C:\xampp\htdocs\login\vendor\guz in C:\xampp\htdocs\login\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php on line 208
good
ReplyDeleteClick me for tech information about phones latest tech news and exams updates
ReplyDeleteClick me for tech information about phones latest tech news and exams updates
ReplyDeleteThank You
ReplyDeletegetting error This page isn’t working
ReplyDeletelocalhost is currently unable to handle this request.
HTTP ERROR 500
I have faced an issue in log out. please tell me
ReplyDeleteFatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: `POST https://oauth2.googleapis.com/revoke` resulted in a `400 Bad Request` response: { "error": "invalid_request", "error_description": "Bad Request" } in C:\xampp\htdocs\googleApi\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php:111 Stack trace: #0 C:\xampp\htdocs\googleApi\vendor\guzzlehttp\guzzle\src\Middleware.php(66): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response)) #1 C:\xampp\htdocs\googleApi\vendor\guzzlehttp\promises\src\Promise.php(203): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Response)) #2 C:\xampp\htdocs\googleApi\vendor\guzzlehttp\promises\src\Promise.php(156): GuzzleHttp\Promise\Promise::callHandler(1, Object(GuzzleHttp\Psr7\Response), Array) #3 C:\xampp\htdocs\googleApi\vendor\guzzlehttp\promises\src\TaskQueue.php(47): GuzzleHttp\Promise\Promise::GuzzleHttp\Promise\{closure}() #4 C:\xampp\htdocs\googleApi\vendor\guzzlehttp\promi in C:\xampp\htdocs\googleApi\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php on line 111
ReplyDeleteFatal error: Uncaught InvalidArgumentException: Redirect URI must be absolute in C:\xampp\htdocs\googleyoutubeapi\vendor\google\auth\src\OAuth2.php:771 Stack trace: #0 C:\xampp\htdocs\googleyoutubeapi\vendor\google\auth\src\OAuth2.php(347): Google\Auth\OAuth2->setRedirectUri('\r\n\r\nhttp://loca...') #1 C:\xampp\htdocs\googleyoutubeapi\vendor\google\apiclient\src\Google\Client.php(1021): Google\Auth\OAuth2->__construct(Array) #2 C:\xampp\htdocs\googleyoutubeapi\vendor\google\apiclient\src\Google\Client.php(1001): Google_Client->createOAuth2Service() #3 C:\xampp\htdocs\googleyoutubeapi\vendor\google\apiclient\src\Google\Client.php(362): Google_Client->getOAuth2Service() #4 C:\xampp\htdocs\googleyoutubeapi\india.php(63): Google_Client->createAuthUrl() #5 {main} thrown in C:\xampp\htdocs\googleyoutubeapi\vendor\google\auth\src\OAuth2.php on line 771
great
ReplyDeleteI have to search sites with relevant information on given topic and provide them to teacher our opinion and the article. SKYWESTONLINE login
ReplyDeleteI think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. click
ReplyDeleteThis web page is usually a walk-through for all of the info it suited you in regards to this and didn’t know who must. Glimpse here, and you’ll undoubtedly discover it.
ReplyDeleteMobile price in Bangladesh
We should talk interesting site page substance and SEO content technique.https://proseogroupbuy.com
ReplyDeleteYou invest a lot of time in your blog, making sure it fits your personal brand and style. And a big part of
ReplyDeleteI think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. Mobile Price in Bangladesh
ReplyDeleteI have to search sites with relevant information on given topic and provide them to teacher our opinion and the article. Mobile Maya
ReplyDeleteGoogle Drive is Google's distributed storage choice. They start you with a strong 30 GB at the same time, you can overhaul that to limitless capacity for just $10 every month with G Suite. Without G Suite, you'll pay $10 every month for 1 TB of capacity. remove email address from spam list gmail
ReplyDeleteEssentially a login-content might be of two sorts. www.hotmail.com
ReplyDeletegoogleapi folder kaha se aayaa
ReplyDeleteAnother approach to look for your crowd is through LinkedIn. LinkedIn is an incredible apparatus for this since they permit you to look for individuals as per their socioeconomics for example by their titles, districts, explicit offices, organizations, businesses, and then some. SMM Panel
ReplyDeleteWeb development broadly refers to the tasks associated with developing websites for hosting via intranet or Internet. The Web development process includes Web design, Web content development, client-side/server-side scripting and network security configuration, among other tasks.
ReplyDeleteFor more info: https://lapisstag.com/web-design-and-development/
sir these error occured
ReplyDeleteWarning: require_once(vendor/autoload.php): failed to open stream: No such file or directory in C:\xampp\htdocs\oauth\config.php on line 9
Fatal error: require_once(): Failed opening required 'vendor/autoload.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\oauth\config.php on line 9
Wow, this is fascinating reading. I am glad I found this and got to read it. Great job on this content. I liked it a lot. Thanks for the great and unique info. onlysearcher
ReplyDeleteFor the excellent company Begin with the internet site, the customer service over the cell phone, to the detailed care they deliver in your presence
ReplyDeleteCarhartt Men's Quilted Flannel Lined Duck Active Jacket
to get gender info
ReplyDeleteadd this in your code
fetchAccessTokenWithAuthCode($_GET["code"]);
if(!isset($token['error']))
{
$google_client->setAccessToken($token['access_token']);
$_SESSION['access_token'] = $token['access_token'];
$google_service = new Google_Service_Oauth2($google_client);
$data = $google_service->userinfo->get();
$_SESSION['data']=$data;
and then print the $data by print_r($_SESSION);
in output you will found gender key
We take a hands-on method of making and controlling your earnings per click on take into account constant improvement - all of us
ReplyDeletemobile design agency
I have recently started a blog, the info you provide on this site has helped me greatly. Thanks for all of your time & work buy google reviews
ReplyDeleteInternal stakeholders are pleased with the team's dedication in providing quality SEO strategies and a high-quality website.
ReplyDeleteCustomers can expect transparency with an effective workflow premium niche edit , while communication and quality assurance may need more direct customer involvement.
ReplyDelete