An open API service indexing awesome lists of open source software.

https://github.com/rohit-chouhan/googleauth

Most Streamlined and User-Friendly PHP Library for Google OAuth 2.0 & 3.0 Integration
https://github.com/rohit-chouhan/googleauth

access auth easy google token

Last synced: 8 months ago
JSON representation

Most Streamlined and User-Friendly PHP Library for Google OAuth 2.0 & 3.0 Integration

Awesome Lists containing this project

README

          

GoogleAuth PHP Library Documentation

GoogleAuth PHP Library Documentation
====================================

Introduction
------------

GoogleAuth is a PHP library developed by Rohit Chouhan for integrating Google OAuth authentication into your web applications. It simplifies the process of handling OAuth authorization, token generation, and user information retrieval.

Table of Contents
-----------------

* [Installation](#installation)
* [Usage](#usage)
* [Initialization](#initialization)
* [Setting Redirect URI](#setting-redirect-uri)
* [Setting OAuth Scopes](#setting-oauth-scopes)
* [Getting OAuth Login URL](#getting-oauth-login-url)
* [Getting Access Token](#getting-access-token)
* [Refresh Access Token](#refresh-access-token)
* [Resetting Access Token](#resetting-access-token)
* [Getting User Information](#getting-user-information)

Installation
------------

Clone the repository from [GitHub](https://github.com/rohit-chouhan/GoogleAuth) or download the `GoogleAuth.php` file and include it in your project.

Usage
-----

### Initialization
This function initializes the GoogleAuth class with the provided OAuth client ID and client secret.

```php
setRedirectURI($redirectURI);
```

### Setting OAuth Scopes
This function sets the OAuth scopes required for authorization. It takes an array of OAuth scopes as a parameter.

```php
$scope = ['email', 'profile']; // Example scopes
$googleAuth->setScope($scope);
```

### Getting OAuth Login URL
This function generates the OAuth login URL. Users are redirected to this URL to authorize the application.

```php
$authURL = $googleAuth->getLoginURI();
```

### Getting Access Token
This function returns the access token received after user login for an access token, you don't need to refresh your token, this function can refresh access token every time, so user don't need to login everytime.
`$code` parameter is only required, when user authorize first time.
```php
$code = $_GET['code']; // Authorization code received after user login
$accessToken = $googleAuth->getAccessToken($code);
```
### Refresh Access Token
This function help you to refresh your access token, when your original token is expired.
This function is optinal to use, because its already implemente with `getAccessToken()`, if `getAccessToken` not able to refresh token, then use this function.

```php
$googleAuth->refreshToken("YOUR_REFRESH_TOKEN");
```

### Resetting Access Token
This function resets the stored access token. It can be used to perform a debug login.

```php
$googleAuth->resetAccessToken();
```

### Getting User Information
This function retrieves user information using the stored access token. It returns an object containing user details such as name and email address.

```php
$userInfo = $googleAuth->getUserInfo();
echo "User Name: " . $userInfo->name;
echo "User Email: " . $userInfo->email;
```

### Complete Example Code
```php
setRedirectURI($redirectURI);
$google_auth->setScope($scope);

if (isset($_GET['code'])) {
$code = $_GET['code'];
$userInfo = $google_auth->getUserInfo();
echo '';
echo '

';
echo "Name: " . $userInfo->name;
echo '

';
echo "Email: " . $userInfo->email;
echo '

';
echo "Access Token: " . $google_auth->getAccessToken($code);
} else {
echo "

Login to Continue

";
echo '';
}

```

For more details, refer to the [GoogleAuth GitHub Repository](https://github.com/rohit-chouhan/GoogleAuth).