{"id":26567613,"url":"https://github.com/musabdev/php-google-login","last_synced_at":"2025-03-22T19:20:01.976Z","repository":{"id":109343348,"uuid":"497960710","full_name":"MusabDev/php-google-login","owner":"MusabDev","description":"This is a template what you can use to get started with Google Login in PHP.","archived":false,"fork":false,"pushed_at":"2022-05-30T15:49:06.000Z","size":11,"stargazers_count":50,"open_issues_count":1,"forks_count":28,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-16T14:39:22.367Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MusabDev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-05-30T13:43:24.000Z","updated_at":"2025-02-05T16:50:19.000Z","dependencies_parsed_at":null,"dependency_job_id":"f8979a75-3192-43cb-82cd-6674888b92d8","html_url":"https://github.com/MusabDev/php-google-login","commit_stats":null,"previous_names":["musabilm/php-google-login","musabdev/php-google-login"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MusabDev%2Fphp-google-login","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MusabDev%2Fphp-google-login/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MusabDev%2Fphp-google-login/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MusabDev%2Fphp-google-login/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MusabDev","download_url":"https://codeload.github.com/MusabDev/php-google-login/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245008098,"owners_count":20546269,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-22T19:20:01.191Z","updated_at":"2025-03-22T19:20:01.962Z","avatar_url":"https://github.com/MusabDev.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Google Login with PHP SDK\n\n## Create a Google API Project\n\nFirstly, you need to create an application with Google which will allow you to register your site with Google. It allows you to set up basic information about your website and a couple of technical details as well.\n\nOnce you’re logged in with Google, open the [Google Developers console](https://console.developers.google.com/). That should open up the **Google Dashboard** page, as shown in the following screenshot.\n\n### Dashboard\n\nFrom the top-left menu, click on the **Select a project** link. That should open up a popup, as shown in the following screenshot.\n\n### New Project Pop Up\n\nClick on the **New Project** link and it will ask you to enter the **Project Name** and other details. Fill in the necessary details, as shown in the following example.\n\n### Create New Project\n\nClick on the **Create** button to save your new project. You will be redirected to the **Dashboard** page. Click on the **Credentials** from the left sidebar, and go to the **OAuth consent screen** tab.\n\n### oAuth Consent Screen\n\nOn this page, you need to enter the details about your application, like the application name, logo, and a few other details. Fill in the necessary details and save them. For testing purposes, just entering the application name should do it.\n\nNext, click on **Credentials** in the left sidebar. That should show you the **API Credentials** box under the **Credentials** tab, as shown in the following screenshot.\n\n### Credentials Tab\n\nClick **Client credentials \u003e OAuth client ID** to create a new set of credentials for our application. That should present you with a screen that asks you to choose an appropriate option. In our case, select the **Web application** option and click on the **Create** button. You will be asked to provide a few more details about your application.\n\n### App Settings\n\nEnter the details shown in the above screenshot and save it! Of course, you need to set the **Redirect URI** as per your application settings. It is the URL where the user will be redirected after login.\n\nAt this point, we’ve created the Google OAuth2 client application, and now we should be able to use this application to integrate Google login on our site. Please note down the **Client ID** and **Client Secret** values that will be required during the application configuration on our end. You can always find the **Client ID** and **Client Secret** when you edit your application.\n\n## Install the Google PHP SDK Client Library\n\nIn this section, we’ll see how to install the Google PHP API client library. There are two options you could choose from to install it:\n\nUse composer to download Google API Client\n\n```bash\ncomposer require google/apiclient:\"^2.0\"\n```\n\n## Client Library Integration\n\nRecall that while configuring the Google application, we had to provide the redirect URI in the application configuration, and we set it to redirect to https://localhost/redirect.php. Now it’s time to create the redirect.php file.\n\nGo ahead and create the redirect.php with the following contents.\n\n```php\n\u003c?php\nrequire_once 'vendor/autoload.php';\n\n// init configuration\n$clientID = '\u003cYOUR_CLIENT_ID\u003e';\n$clientSecret = '\u003cYOUR_CLIENT_SECRET\u003e';\n$redirectUri = '\u003cREDIRECT_URI\u003e';\n\n// create Client Request to access Google API\n$client = new Google_Client();\n$client-\u003esetClientId($clientID);\n$client-\u003esetClientSecret($clientSecret);\n$client-\u003esetRedirectUri($redirectUri);\n$client-\u003eaddScope(\"email\");\n$client-\u003eaddScope(\"profile\");\n\n// authenticate code from Google OAuth Flow\nif (isset($_GET['code'])) {\n  $token = $client-\u003efetchAccessTokenWithAuthCode($_GET['code']);\n  $client-\u003esetAccessToken($token['access_token']);\n\n  // get profile info\n  $google_oauth = new Google_Service_Oauth2($client);\n  $google_account_info = $google_oauth-\u003euserinfo-\u003eget();\n  $email =  $google_account_info-\u003eemail;\n  $name =  $google_account_info-\u003ename;\n\n  // now you can use this profile info to create account in your website and make user logged in.\n} else {\n  echo \"\u003ca href='\".$client-\u003ecreateAuthUrl().\"'\u003eGoogle Login\u003c/a\u003e\";\n}\n?\u003e\n```\n\n#### Let’s go through the key parts of the code.\n\nThe first thing we need to do is to include the autoload.php file. This is part of Composer and ensures that the classes we use in our script are autoloaded.\n\n```php\nrequire_once 'vendor/autoload.php';\n```\n\nNext, there’s a configuration section, which initializes the application configuration by setting up the necessary settings. Of course, you need to replace the placeholders with your corresponding values.\n\n```php\n// init configuration\n$clientID = '\u003cYOUR_CLIENT_ID\u003e';\n$clientSecret = '\u003cYOUR_CLIENT_SECRET\u003e';\n$redirectUri = '\u003cREDIRECT_URI\u003e';\n```\n\nThe next section instantiates the Google_Client object, which will be used to perform various actions. Along with that, we’ve also initialized our application settings.\n\n```php\n// create Client Request to access Google API\n$client = new Google_Client();\n$client-\u003esetClientId($clientID);\n$client-\u003esetClientSecret($clientSecret);\n$client-\u003esetRedirectUri($redirectUri);\n```\n\nNext, we’ve added email and profile scopes, so after login we have access to the basic profile information.\n\n```php\n$client-\u003eaddScope(\"email\");\n$client-\u003eaddScope(\"profile\");\n```\n\nFinally, we have a piece of code which does the login flow magic.\n\n```php\n// authenticate code from Google OAuth Flow\nif (isset($_GET['code'])) {\n  $token = $client-\u003efetchAccessTokenWithAuthCode($_GET['code']);\n  $client-\u003esetAccessToken($token['access_token']);\n\n  // get profile info\n  $google_oauth = new Google_Service_Oauth2($client);\n  $google_account_info = $google_oauth-\u003euserinfo-\u003eget();\n  $email =  $google_account_info-\u003eemail;\n  $name =  $google_account_info-\u003ename;\n\n  // now you can use this profile info to create account in your website and make user logged in.\n} else {\n  echo \"\u003ca href='\".$client-\u003ecreateAuthUrl().\"'\u003eGoogle Login\u003c/a\u003e\";\n}\n```\n\nFirstly, let’s go through the else part, which will be triggered when you access the script directly. It displays a link which takes the user to Google for login. It’s important to note that we’ve used the createAuthUrl method of the Google_Client to build the OAuth URL.\n\nAfter clicking on the Google login link, users will be taken to the Google site for login. Once they log in, Google redirects users back to our site by passing the code query string variable. And that’s when the PHP code in the if block will be triggered. We’ll use the code to exchange the access token.\n\nOnce we have the access token, we can use the Google_Service_Oauth2 service to fetch the profile information of the logged-in user.\n\nSo in this way, you’ll have access to the profile information once the user logs in to the Google account. You can use this information to create accounts on your site, or you could store it in a session. Basically, it’s up to you how you use this information and respond to the fact that the user is logged in to your site.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmusabdev%2Fphp-google-login","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmusabdev%2Fphp-google-login","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmusabdev%2Fphp-google-login/lists"}