John Davidson

How to get userinfo in google api Php

0 comments
Message:


I am working with google api(Rest Api) in php, I just want whenever
any user send "access_token/token" then i want to validate and get userinfo,How can i do this ?


I tried with following code but not working for me,showing me always url in my postman,Where i am wrong ?


<?php 
$google_client->authenticate($_GET['code']);

$client = new Google_Client();
$google_client = new Google_Client();
$google_client->setClientId('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com'); //Define your ClientID
$google_client->setClientSecret('xxxxxxxxxxxxxxxxxxxxx'); //Define your Client Secret Key
$google_client->setRedirectUri('xxxxxxxxxxxx'); //Define your Redirect Uri
$google_client->addScope('email');
$google_client->addScope('profile');
$objOAuthService = new Google_Service_Oauth2($google_client);
$getAccesToken = $google_client->getAccessToken();
$getRefreshToken = $google_client->getRefreshToken();
$google_client->setAccessToken($token['access_token']);
$objOAuthService = new Google_Service_Oauth2($google_client);
if($google_client->getAccessToken()) {
$userData = $objOAuthService->userinfo->get();
if(!empty($userData))
{

$outputjson['user_data'] = $userData;
$outputjson['access_token'] = $google_client->getAccessToken();
}else{
$outputjson['login_url'] = $google_client->createAuthUrl();
}

}


My Answer:
a request to my application, I can get their user information from Google API. Here is a step-by-step guide on how to achieve this:

1. Set up Google API credentials:
- Go to the Google API Console (https://console.developers.google.com/)
- Create a new project and enable the necessary APIs (e.g. Google People API)
- Create credentials (OAuth client ID) for your project and download the client secret file

2. Install Google API client library:
- You can install the Google API client library for PHP using Composer by running the following command:

composer require google/apiclient


3. Authenticate with Google API:
- Use the client secret file downloaded in step 1 to authenticate with Google API. Here is an example code snippet to authenticate with Google API:
php
require_once 'vendor/autoload.php';

$client = new Google_Client();
$client->setAuthConfig('path/to/client_secret.json');
$client->setAccessType('offline');
$client->setScopes(['https://www.googleapis.com/auth/userinfo.profile']);

if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
}


4. Get user information:
- Once authenticated, you can make requests to the Google API to get user information. Here is an example code snippet to get user information:
php
$service = new Google_Service_Oauth2($client);
$userInfo = $service->userinfo->get();

echo 'User ID: ' . $userInfo->getId() . '
';
echo 'Name: ' . $userInfo->getName() . '
';
echo 'Email: ' . $userInfo->getEmail() . '
';


5. Handle user information:
- You can now use the user information obtained from Google API in your application as needed.

That's it! By following these steps, you should be able to get user information from Google API in your PHP application.

Rate this post

5 of 5 based on 6372 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us