PHP code example of samuelthomas2774 / oauth-client
1. Go to this page and download the library: Download samuelthomas2774/oauth-client library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
samuelthomas2774 / oauth-client example snippets
use OAuth2\GenericOAuthProvider;
$client_id = 'client-id';
$client_secret = 'client-secret';
$client = new GenericOAuthProvider($client_id, $client_secret, null, [
'session_prefix' => 'facebook_',
'base_api_endpoint' => 'https://graph.facebook.com',
'authorise_endpoint' => 'https://facebook.com/dialog/oauth',
'token_endpoint' => 'oauth/access_token', // Relative to the base API URL
]);
$redirect_url = 'https://example.com/facebook/code.php';
$scope = ['email', 'user_friends']; // Optional scope array
// Returns an OAuth2\AuthoriseUrl object
// This will be converted to a string automatically if needed
// You can use this object to get the OAuth2\State object to store extra state data accessible with
// $client->getRequestState() when the user accepts/rejects the authorise request
$login_url = $client->generateAuthoriseUrlAndState($redirect_url, $scope);
// Add data to the state object
// Every time generateAuthoriseUrlAndState is called a new state object will be created with separate data,
// so you can have multiple authorise links on the same page and have the authorise response page react differently
// depending on which link was used
$login_url->getState()->next_url = '/';
// You can also set the state parameter yourself (not recommended)
// $login_url->getState() will still return a state object, but the attached data won't be saved
// Use $login_url->getStateId(), $login_url->getState()->getId() or cast the state object to a string to get the
// value of the state parameter
$state = '...'; // Generate a random value and store in somewhere the next page can access (or set to null to not set a state parameter)
$login_url = $client->generateAuthoriseUrl($state, $redirect_url, $scope);
// Validates the request state and returns an OAuth2\AccessToken object
$token = $client->getAccessTokenFromRequestCodeAndState();
// You can also validate the state and pass the redirect url and an optional requested scope array yourself
// Only do this if you set the state parameter when you generated the authorise link
$redirect_url = 'https://example.com/facebook/code.php'; // Must match the $redirect_url given to generateAuthoriseUrl exactly
$requested_scope = ['email', 'user_friends']; // Optional requested scope array to
// Returns an object of data returned from the server
// This may with a refresh token returned by all
// getAccessToken* methods
$new_token = $oauth->getAccessTokenFromRefreshToken($token);
// Get
$token = $client->getAccessToken();
// Set
$client->setAccessToken($new_token);
// Set without updating the access token in the session
$client->setAccessToken($new_token, false);
use OAuth2\Providers\GitLab\GitLab;
$client = new GitLab($client_id, $client_secret, null, [
'instance_url' => 'https://gitlab.fancy.org.uk',
]);
$client->setInstanceUrl('https://gitlab.com');
use OAuth2\Providers\Facebook\Facebook;
class My_Extended_Facebook_Class extends Facebook
{
// Change the base_api_endpoint option to use a different API version
public $base_api_endpoint = 'https://graph.facebook.com/v1.0/';
// Add a new function for getting the current user's ID
public function getUserId(): string
{
$user = $this->getUserProfile(['id']);
return $user->id;
}
}
$client = new My_Extended_Facebook_Class('client-id', 'client-secret');
try {
echo 'Your Facebook User ID (for this app) is: ' . htmlentities($client->getUserId());
} catch (Exception $exception) {
echo 'Facebook returned an error: ' . $exception->getMessage();
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.