maye/oauth-client

PHP OAuth Client based on lusitanian/oauth

1.4.0 2016-11-09 12:06 UTC

This package is auto-updated.

Last update: 2024-10-20 21:43:51 UTC


README

Build Status

Maye.OAuthClient is the OAuth Client library which has simple interface to support OAuth 1 and OAuth 2, based on lusitanian/oauth (PHPoAuthLib).

e.g. Twitter (OAuth1)

1. Redirects to an authorization page

use Maye\OAuthClient;

define('CONSUMER_KEY', 'your twitter consumer key');
define('CONSUMER_SECRET', 'your twitter consumer secret');

$client = new OAuth1Client(
	'twitter', 
	CONSUMER_KEY, 
	CONSUMER_SECRET, 
	'/oauth/twitter/callback'
);

// redirect
$client->authorize();
exit;

2. Called back after authorization process finished

$denied = $_GET['denied'];
$oauthToken = $_GET['oauth_token'];
$oauthVerifier = $_GET['oauth_verifier'];

if (!empty($denied)) {
	// should be handled as error
	echo 'error';
	exit;
}

/** @var OAuth\OAuth1\Token\TokenInterface $token */
$token = $client->requestAccessToken($oauthToken, $oauthVerifier);

$accessToken       = $token->getAccessToken();
$accessTokenSecret = $token->getAccessTokenSecret();
$userId     = $token->getExtraParams()['user_id'];
$screenName = $token->getExtraParams()['screen_name'];

// gets the authorized user info
$result = $client->api('get', 'users/show.json', ['user_id' => $userId]);
$result = json_decode($result);

$name = $result->name;

e.g. Facebook (OAuth2)

1. Redirects to authorization page

use Maye\OAuthClient;
use OAuth\OAuth2\Service\Facebook;

define('CONSUMER_KEY', 'your facebook consumer key');
define('CONSUMER_SECRET', 'your facebook consumer secret');

$client = new OAuth2Client(
	'facebook',
	CONSUMER_KEY, 
	CONSUMER_SECRET, 
	'/oauth/facebook/callback',
	[Facebook::SCOPE_READ_STREAM, Facebook::SCOPE_PUBLISH_ACTIONS]
);

// redirect
$client->authorize();
exit;

2. Called back after authorization process finished

$code = $_GET['code'];

if (empty($code)) {
	// should be handled as error
	echo 'error';
	exit;
}

/** @var OAuth\OAuth2\Token\TokenInterface $token */
$token = $client->requestAccessToken($code);

$accessToken  = $token->getAccessToken();
$refreshToken = $token->getRefreshToken();

// gets the authorized user info
$result = $client->api('get', '/me');
$result = json_decode($result);

$id = $result->id;
$name = $result->name;