maye / oauth-client
PHP OAuth Client based on lusitanian/oauth
Installs: 1 487
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=5.6.0
- lusitanian/oauth: ~0.8
Requires (Dev)
- fabpot/php-cs-fixer: ~1.12
- phpmd/phpmd: ~2.4
- phpunit/phpunit: ~5.6
- squizlabs/php_codesniffer: ~2.7
README
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;