January 6, 2013 6:49 am

Login with Google Plus Account OAuth

We are going to cover all the Open Authentication (oAuth) login system for Twitter, LinkedIn, Facebook and now I am writing tutorial about Google Open Authentication login. in this tutorial i will explain how to implement Google oAuth for your web project, this script is very quick and sure it helps you to increase your web project registrations.

googlelogin

Step 1: Create new project 

https://code.google.com/apis/console/

 

googlelogi1n

Step2:

Here the application OAuth client ID and client secret.

See Also: How to login with LinkedIn oAuth2 in PHP and MySQL

googleaccount2

config.php

You can find this in src folder, here you have to configure application OAuth keys, Consumer keys and redirection callback URL.

index.php

 

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.


Tutorial Categories:
  • there is a missing semicolon after endif. Thanks. Great post.

    • Huzoor Bux

      Fixed 🙂

  • Phenomenal work, Gang. Works like a charm.

  • Hi PHP gang, I’ve integrated your code, but can’t seem to get the profile picture with the returned content like in your demo. I only get [link] followed by [gender]. Can you please help?

    • huzoorbux

      Share the link where you installed it…

      • Abhay Udgire

        can i import .ics file in Google calender as a backend process using Google Oauth ? please help i don’t know whether it is possible or not thanks in advance….

  • Mani Cloud

    I have followed the above process and running it in my localhost, after redirecting from google login it is throwing an error “Fatal error: Uncaught exception ‘Google_IOException’ with
    message ‘HTTP Error: (0) Failed connect to accounts.google.com:443; No
    error’ in
    C:xampphtdocsgoogle_login_oauthsrcioGoogle_CurlIO.php:128
    Stack trace:
    #0 C:xampphtdocsgoogle_login_oauthsrcauthGoogle_OAuth2.php(101):
    Google_CurlIO->makeRequest(Object(Google_HttpRequest))
    #1 C:xampphtdocsgoogle_login_oauthsrcGoogle_Client.php(131):
    Google_OAuth2->authenticate(Array, ‘4/aoLDyvob3U654…’)
    #2 C:xampphtdocsgoogle_login_oauthindex.php(27):
    Google_Client->authenticate(‘4/aoLDyvob3U654…’)
    #3 {main}
    thrown in C:xampphtdocsgoogle_login_oauthsrcioGoogle_CurlIO.php on line 128”

    • huzoorbux

      This means that the server is unable to perform peer SSL certificate verification. The Windows version of PHP doesn’t come bundled with a Certificate Authority bundle. So you need to add it yourself.

      Solution 1:

      In ‘google-api-php-clientsrcio’ folder, open ‘apiCurlIO.php’

      Replace

      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);

      with

      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

      Solution 2:

      Download the .pem file from the cURL site and rename the extension to .crt

      Save the renamed file to your web server.

      Add the following line to ‘google-api-php-client/src/io/apiCurlIO.php’ right before the ‘curl_exec()’ method call. As per the current version of Google APIs Client Library for PHP, the line of code (‘curl_exec’ method call) is $respData = curl_exec($ch);

      curl_setopt($ch, CURLOPT_CAINFO, ‘c:/path/to/ca-bundle.crt’);

      Remember to replace ‘c:/path/to/ca-bundle.crt’ to with the path for your saved .crt file.

      Hope everything worked well. Comment below if you face any problems or find any improvements to the script.

      • Mani Cloud

        I tired both, still seeing same issue. 🙁

        • huzoorbux

          Host your application on some hosting don’t use on localhost then try.

          • Mani Cloud

            that’s the only problem, i dont have any hosting space..

          • huzoorbux

            @rjilcloud:disqus Use 000webhost for free hosting account it will give you sub-domain where you can perform your all experiments.

          • Mani Cloud

            thanks , i am doing this and will get back to you again.

          • Mani Cloud

            @huzoorbux:disqus : i tried the same by creating subdomain it is working out, thanks a lot…can you please tell how i can fix the same in localhost?

          • huzoorbux

            Ok i will give you its solution soon.

          • Mani Cloud

            desperately will wait.. thanks again 🙂

  • binil billu

    hi, past two days app working correctly .but now can’t access the datas from google.app login permission shows like below and no data return to our website. why this happened??? help me
    This app would like to:
    Have offline access

    AcceptCancel

  • Love Sharma

    hello Sir thsi is great script and print all values according to me like i have need some information we getting but how can print email what use of array name

    • huzoorbux

      Sorry don’t understand what are you talking about please explain.

    • huzoorbux

      you can print only email like this,
      echo $arrayname[’email’];

      • Love Sharma

        Sir what array in this script

      • Love Sharma

        Sir I don’t understand whats Array name please tell me

    • huzoorbux

      $user is your array name.

      • Love Sharma

        I have already print $user but he prints echo $user[’email’];

      • Love Sharma

        hello i findout solutions and thanks Sir

      • Love Sharma

        how can enter value in database
        Please tell me Sir

  • Ikbal Mohamad Hikmat

    Thanks fos sharing. i have problem, this always ask permission every time I’m login. how to skip it? please help..

  • Gaurav Sharma

    really very very thanks its awesome script you help me very much thanks a lot again

  • Gagandeep Sharma

    Thanks a ton 🙂

  • Anoop S

    Hi, Thank You for your script

    I am getting – Fatal error: Class ‘Google_ServiceResource’ not found in /public_html/google-oauth/src/contrib/Google_Oauth2Service.php on line 25.

  • ramiz

    may it work on localhost?

  • Kumaresan S

    Hi sir how to give the redirect uri in localhost.

  • SSL connection error

    how to resolve this, After click Accept the SSL error show on index page thanks.

  • nadeem sheikh

    hi sir,
    i have a query
    how to
    insert data in spreadsheet using google doc api without using username and password in php.

  • govind oza

    i am follow step for login with google but some error are generated URL redirect missing how to solve this error…plz reply

    • Try to debug it and post errors below.

      • govind oza

        Fatal error: Uncaught exception ‘Google_IOException’ with message ‘HTTP Error: (0) Failed to connect to accounts.google.com port 443: Connection timed out’ in /home/tdemocoi/public_html/poptucket/google_login_oauth/src/io/Google_CurlIO.php:128 Stack trace: #0 /home/tdemocoi/public_html/poptucket/google_login_oauth/src/auth/Google_OAuth2.php(101): Google_CurlIO->makeRequest(Object(Google_HttpRequest)) #1 /home/tdemocoi/public_html/poptucket/google_login_oauth/src/Google_Client.php(131): Google_OAuth2->authenticate(Array, ‘4/fwJZtYI77690l…’) #2 /home/tdemocoi/public_html/poptucket/google_login_oauth/index.php(27): Google_Client->authenticate(‘4/fwJZtYI77690l…’) #3 {main} thrown in/home/tdemocoi/public_html/poptucket/google_login_oauth/src/io/Google_CurlIO.php on line 128

  • govind oza

    i am follow step for login with google but some error are generated URL redirect missing how to solve this error…plz reply

  • govind oza

    Fatal error: Uncaught exception ‘Google_IOException’ with message ‘HTTP Error: (0) Failed to connect to accounts.google.com port 443: Connection timed out’ in /home/tdemocoi/public_html/poptucket/google_login_oauth/src/io/Google_CurlIO.php:128 Stack trace: #0 /home/tdemocoi/public_html/poptucket/google_login_oauth/src/auth/Google_OAuth2.php(101): Google_CurlIO->makeRequest(Object(Google_HttpRequest)) #1 /home/tdemocoi/public_html/poptucket/google_login_oauth/src/Google_Client.php(131): Google_OAuth2->authenticate(Array, ‘4/fwJZtYI77690l…’) #2 /home/tdemocoi/public_html/poptucket/google_login_oauth/index.php(27): Google_Client->authenticate(‘4/fwJZtYI77690l…’) #3 {main} thrown in/home/tdemocoi/public_html/poptucket/google_login_oauth/src/io/Google_CurlIO.php on line 128

    above this error are generated..

  • Bugs Nitya

    I got this error..

    Error: invalid_client

    The OAuth client was not found.

  • freddy

    how can i applicated this with codeignter ?

  • Suraj

    This script works with localhost. However, when I deploy it to my server; I get this error. “Class ‘Google_ServiceResource’ not found in /home/user/public_html/src/contrib/Google_Oauth2Service.php on line 25”

    Has anyone resolved it?

  • Sonali Shah

    Can anybody help me indeploying this project in google app engine. I mean can any body help me with app.yaaml file for this project