THAPA TECHNICAL

HOUSE OF WEB DEVELOPERS AND TECHNOLOGY.

Login with Google Account using PHP

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

  1. Create Google API project and get OAuth credentials.
  2. click here Google Developers Console

  3. Here are the steps
    1. After this click on Create New Project link for create new project.
    2. Enter Project Name and click on Create button.
    3. Once you have created a new project then you can see your project list on web page.
    4. After this click on Google API logo for go to the home page.
    5. Once you have redirected to home page then select project from the project select box.
    6. 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.
    7. Now from left menu, you have to click on OAuth consent screen.
    8. 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.
    9. 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.
    10. After click on OAuth client ID menu then you have redirected to another page, and here you can find different Application type.
    11. 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.
    12. 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.
  4. Download / Install PHP Google API client library.
    1. copy this on CMD:- composer require google/apiclient:"^2.0"

Steps to implement Google authentication

  1. Create / Send authentication request.
  2. 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


  1. PHP 5.4 or greater with the command-line interface (CLI) and JSON extension installed.
  2. The Composer dependency management tool.
  3. The Google APIs Client Library for PHP:
  4. 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.

63 comments:

  1. awesome video sir,(hahhhhahah maja a gaya)

    ReplyDelete
  2. Make video on send sms from localhost

    ReplyDelete
  3. Can you make a tutorial for Mac users as well? I'm facing authentication errors.

    ReplyDelete
  4. Hello,
    I 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...

    ReplyDelete
    Replies
    1. This issue is solved.
      Actually 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.

      Delete
  5. 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

    ReplyDelete
  6. The 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..

    ReplyDelete
  7. thanks for making this wonderful video! thanks a lot

    ReplyDelete

  8. Fatal 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

    ReplyDelete
  9. Hello i got this error after selecting the google account for login..pls help

    Fatal 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

    ReplyDelete
  10. getting error This page isn’t working
    localhost is currently unable to handle this request.
    HTTP ERROR 500

    ReplyDelete
  11. I have faced an issue in log out. please tell me


    Fatal 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

    ReplyDelete

  12. Fatal 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

    ReplyDelete
  13. I have to search sites with relevant information on given topic and provide them to teacher our opinion and the article. SKYWESTONLINE login

    ReplyDelete
  14. 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. click

    ReplyDelete
  15. This 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.
    Mobile price in Bangladesh

    ReplyDelete
  16. We should talk interesting site page substance and SEO content technique.https://proseogroupbuy.com

    ReplyDelete
  17. You invest a lot of time in your blog, making sure it fits your personal brand and style. And a big part of

    ReplyDelete
  18. 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. Mobile Price in Bangladesh

    ReplyDelete
  19. I have to search sites with relevant information on given topic and provide them to teacher our opinion and the article. Mobile Maya

    ReplyDelete
  20. Google 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

    ReplyDelete
  21. Essentially a login-content might be of two sorts. www.hotmail.com

    ReplyDelete
  22. Another 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

    ReplyDelete
  23. Web 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.
    For more info: https://lapisstag.com/web-design-and-development/

    ReplyDelete
  24. sir these error occured

    Warning: 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

    ReplyDelete
  25. 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

    ReplyDelete
  26. For the excellent company Begin with the internet site, the customer service over the cell phone, to the detailed care they deliver in your presence
    Carhartt Men's Quilted Flannel Lined Duck Active Jacket

    ReplyDelete
  27. to get gender info


    add 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

    ReplyDelete
    Replies
    1. Google_Service_Oauth2 is giving error that it is not found,
      tell me what to do

      Delete
  28. We take a hands-on method of making and controlling your earnings per click on take into account constant improvement - all of us
    mobile design agency

    ReplyDelete
  29. 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

    ReplyDelete
  30. Internal stakeholders are pleased with the team's dedication in providing quality SEO strategies and a high-quality website.

    ReplyDelete
  31. Customers can expect transparency with an effective workflow premium niche edit , while communication and quality assurance may need more direct customer involvement.

    ReplyDelete
  32. This is such a great resource that you are providing and you give it away for free. I love seeing websites that understand the value of providing a quality
    resource for free. It is the old what goes around comes around routine.com.android.browser.home

    ReplyDelete
  33. I understand this column. I realize You put a many of struggle to found this story. I admire your process. learn this here now

    ReplyDelete
  34. Much obliged to you for some other educational web journal. Where else might I be able to get that kind of data written in such a perfect means? I have a
    mission that I'm a few seconds ago chipping away at, and I have been at the post for such data. I went over this site and I trust you have a ton of awesome
    data, spared to my bookmarks...https://1asiaqq.net

    ReplyDelete

  35. Warning: require_once(vendor/autoload.php): Failed to open stream: No such file or directory in D:\xampp\htdocs\lg\config.php on line 9

    Fatal error: Uncaught Error: Failed opening required 'vendor/autoload.php' (include_path='D:\xampp\php\PEAR') in D:\xampp\htdocs\lg\config.php:9 Stack trace: #0 D:\xampp\htdocs\lg\index.php(6): include() #1 {main} thrown in D:\xampp\htdocs\lg\config.php on line 9

    ReplyDelete
  36. The great content I have seen will be very useful to us, I am grateful.

    Our Service Here:
    https://www.buysmmusa.com/product/buy-google-reviews/

    ReplyDelete
  37. Muchas gracias por compartir tus conocimientos, me ayudo demasiado este tutorial, bendiciones.

    ReplyDelete
  38. Thanks a lot Sir It's working fine

    ReplyDelete
  39. The main thing is to do this you don't need to change the manner in which you construct/plan pages. see cached website

    ReplyDelete
  40. You will communicate your style and character. since toward the day's end, it actually will be your website, selling your items. SEO Antwerpen

    ReplyDelete
  41. Hey what a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this. Thank you very much and will look for more postings from you. prime minister imran khan

    ReplyDelete
  42. Web designers are responsible for developing the look, feel, organization, and graphics content of a website. Some web designers only specialize in creating the graphic content and raw look of a site. At this stage, it is little more than an image. It is then giving to a web developer who will cut it up and code it to make the site function. More and more web designers now are expected to know the coding side of web design which includes coding languages such as HTML, DHTML, Javascript, Actionscript, PHP, CSS, Jquery, and so on. Click here: qaiserm

    ReplyDelete
  43. If i want to give access to only few people in my organization then what should i do?

    ReplyDelete
  44. Shopping for Google Reviews could be a highly effective approach to promote your web site or weblog. You may assume that having the voice of your organization heard on Google is an effective approach to construct up your model, however sadly, that goodwill is not going to come low-cost. On common, the worth of a Google search vary from $35 - $70, whereas the worth varies significantly for various corporations. It's subsequently vital to know simply what you possibly can anticipate if you Buy Google Reviews by BUYSMMUSA, find out how to use them in your marketing campaign, and the place to purchase them from, to be sure you get the most effective worth for cash.

    ReplyDelete
  45. Someone Sometimes with visits your blog regularly and recommended it in my experience to read as well. The way of writing is excellent and also the content is top-notch. Thanks for that insight you provide the readers! login directory

    ReplyDelete
  46. please fix this error



    Fatal error: Uncaught TypeError: Argument 1 passed to Google_AccessToken_Revoke::revokeToken() must be of the type array, null given, called in C:\xampp\htdocs\work\practice\API\loginapi\vendor\google\apiclient\src\Google\Client.php on line 671 and defined in C:\xampp\htdocs\work\practice\API\loginapi\vendor\google\apiclient\src\Google\AccessToken\Revoke.php:51 Stack trace: #0 C:\xampp\htdocs\work\practice\API\loginapi\vendor\google\apiclient\src\Google\Client.php(671): Google_AccessToken_Revoke->revokeToken(NULL) #1 C:\xampp\htdocs\work\practice\API\loginapi\logout.php(8): Google_Client->revokeToken() #2 {main} thrown in C:\xampp\htdocs\work\practice\API\loginapi\vendor\google\apiclient\src\Google\AccessToken\Revoke.php on line 51

    ReplyDelete
  47. Please any solution for this???

    Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in E:\xampp\htdocs\googlelogin\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php:67 Stack trace: #0 E:\xampp\htdocs\googlelogin\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php(67): count(NULL) #1 E:\xampp\htdocs\googlelogin\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php(107): GuzzleHttp\Handler\CurlFactory->release(Object(GuzzleHttp\Handler\EasyHandle)) #2 E:\xampp\htdocs\googlelogin\vendor\guzzlehttp\guzzle\src\Handler\CurlHandler.php(43): GuzzleHttp\Handler\CurlFactory::finish(Object(GuzzleHttp\Handler\CurlHandler), Object(GuzzleHttp\Handler\EasyHandle), Object(GuzzleHttp\Handler\CurlFactory)) #3 E:\xampp\htdocs\googlelogin\vendor\guzzlehttp\guzzle\src\Handler\Proxy.php(28): GuzzleHttp\Handler\CurlHandler->__invoke(Object(GuzzleHttp\Psr7\Request), Array) #4 E:\xampp\htdocs\googlelogin\vendor\guzzlehttp\guzzle\src\Handler\Proxy.php(51): GuzzleHttp\Handler\Proxy::GuzzleHttp\Handler\{closure}(Object(GuzzleHttp\Psr7\Request), Array) #5 E:\xampp\htdocs\googlelogin\vendor\guzzlehttp\guzzle\src\PrepareBodyMiddleware.php(72): GuzzleHttp\Handler\Proxy::GuzzleHttp\Handler\{closure}(Object(GuzzleHttp\Psr7\Request), Array) #6 E:\xampp\htdocs\googlelogin\vendor\guzzlehttp\guzzle\src\Middleware.php(30): GuzzleHttp\PrepareBodyMiddleware->__invoke(Object(GuzzleHttp\Psr7\Request), Array) #7 E:\xampp\htdocs\googlelogin\vendor\guzzlehttp\guzzle\src\RedirectMiddleware.php(68): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Request), Array) #8 E:\xampp\htdocs\googlelogin\vendor\guzzlehttp\guzzle\src\Middleware.php(57): GuzzleHttp\RedirectMiddleware->__invoke(Object(GuzzleHttp\Psr7\Request), Array) #9 E:\xampp\htdocs\googlelogin\vendor\guzzlehttp\guzzle\src\HandlerStack.php(67): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Request), Array) #10 E:\xampp\htdocs\googlelogin\vendor\guzzlehttp\guzzle\src\Client.php(268): GuzzleHttp\HandlerStack->__invoke(Object(GuzzleHttp\Psr7\Request), Array) #11 E:\xampp\htdocs\googlelogin\vendor\guzzlehttp\guzzle\src\Client.php(96): GuzzleHttp\Client->transfer(Object(GuzzleHttp\Psr7\Request), Array) #12 E:\xampp\htdocs\googlelogin\vendor\guzzlehttp\guzzle\src\Client.php(104): GuzzleHttp\Client->sendAsync(Object(GuzzleHttp\Psr7\Request), Array) #13 E:\xampp\htdocs\googlelogin\vendor\google\auth\src\HttpHandler\Guzzle6HttpHandler.php(34): GuzzleHttp\Client->send(Object(GuzzleHttp\Psr7\Request), Array) #14 E:\xampp\htdocs\googlelogin\vendor\google\auth\src\OAuth2.php(492): Google\Auth\HttpHandler\Guzzle6HttpHandler->__invoke(Object(GuzzleHttp\Psr7\Request)) #15 E:\xampp\htdocs\googlelogin\vendor\google\apiclient\src\Google\Client.php(184): Google\Auth\OAuth2->fetchAuthToken(Object(Google\Auth\HttpHandler\Guzzle6HttpHandler)) #16 E:\xampp\htdocs\googlelogin\index.php(14): Google_Client->fetchAccessTokenWithAuthCode('4/0AX4XfWh3UR3a...') #17 {main} thrown in E:\xampp\htdocs\googlelogin\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php on line 67

    ReplyDelete
  48. hey u got solution or not? I'm facing same problem. Pls reply soon...

    ReplyDelete
  49. Thanks For sharing this Superb article.I use this Article to show my assignment in college.it is useful For me Great Work. google ads tips

    ReplyDelete
  50. Wow, this is really interesting reading. I am glad I found this and got to read it. Great job on this content. I like it. here

    ReplyDelete
  51. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. https://hostinglelo.in/

    ReplyDelete