hannesvdvreken/php-oauth

This package is abandoned and no longer maintained. The author suggests using the hannesvdvreken/php-oauth package instead.

PHP OAuth services

1.1.0 2015-06-03 21:08 UTC

This package is auto-updated.

Last update: 2020-06-17 12:57:55 UTC


README

Build Status Latest Stable Version Total Downloads Coverage Status License

Usage

Let's dive right in.

Setup

$service = new OAuth\Services\Github();

Some possible configuration can be passed on with the constructor, like so:

$redirectUri = 'https://example.com/oauth/callback';

$credentials = array(
    'client_id'     => 'client-id',
    'client_secret' => '****',
);

$scopes = array('user', 'user:email');

$token = array(
    'access_token' => $accessToken;
);

$service = new OAuth\Services\Github($redirectUri, $credentials, $scopes, $token);

An alternative way is the following:

$service = new OAuth\Services\Github;
$service
    ->setRedirectUri($redirectUri)
    ->setCredentials($credentials)
    ->setScopes($scopes)
    ->setToken($token);

The service class also has the following getters:

$redirectUri = $service->getRedirectUri();
$credentials = $service->getCredentials();
$scopes      = $service->getScopes();
$token       = $service->getToken();

The GuzzleHttp\Client underneath can be accessed like so:

$service->setClient(new GuzzleHttp\Client);
$service->getClient();

Requesting an API

The internal GuzzleHttp\Client can be called by calling the same methods on the service class.

$response = $service->get('users/self')->json();

or

$body = ['status' => 'Tweeted with @hannesvdvreken/php-oauth'];
$status = $twitter->post('statuses/update', compact('body'))->json();

The internal Guzzle Client will be called with the right token in the header or GET parameter. All you need to do is load the service class with the correct credentials or tokens from your persistance layer or session.

Laravel 4

If you're using Laravel 4, feel free to register the contained service provider (OAuth\Support\ServiceProvider). Register the OAuth class alias for the facade to use the following syntax to get a fully configured service class:

$twitter = OAuth::consumer('twitter');

To create an empty config file in app/config/packages just use the artisan command:

php artisan config:publish hannesvdvreken/php-oauth

OAuth 1.0a

For the OAuth1.0a functionality we internally use the Guzzle OAuth1 subscriber. An example:

$twitter = new OAuth\Services\Twitter($redirectUri, $credentials);

// Request token for redirecting the user (store it in session afterwards).
$token = $twitter->requestToken();

// Get the url to which we need to redirect the user.
$url = $twitter->authorizationUrl();

// Redirect the user.
header('Location: '. $url); exit;

Or in short, the authorizationUrl will call the requestToken method, if you haven't done so already:

// Get the url to which we need to redirect the user.
$url = $twitter->authorizationUrl();

// Get the requestToken that has been requested.
$token = $twitter->getToken();
// And store it.

// And redirect the user.
header('Location: '. $url); exit;

In the callback controller...

// Give the stored token back to the service class.
$twitter->setToken($token);

// Exchange the get parameters for an access token.
$token = $twitter->accessToken($oauthToken, $oauthVerifier);

// Do a get request, just like you would do with a Guzzle Client.
$profile = $twitter->get('account/verify_credentials.json')->json();

OAuth 2

The OAuth2 flow is easier.

$fb = new OAuth\Services\Facebook();

$url = $fb->authorizationUrl();

header('Location: '. $url);

In the callback controller...

$fb->accessToken($code);

$profile = $fb->get('me')->json();

Supported services

  • Campaign Monitor
  • Dropbox
  • Facebook
  • Foursquare
  • GitHub
  • Google
  • Instagram
  • MailChimp
  • Twitter (OAuth1.0a)
  • Stack Exchange

Guzzle v3

If you want to continue to work with the old versions of this library that leveraged Guzzle v3 (Guzzle\Http\Client instead of GuzzleHttp\Client) then you might want to install the 0.1.* releases. Pull request with Guzzle v3 compatibility should be made against the guzzle3 branch. Install the latest guzzle v3 version with 0.1.* or dev-guzzle3.

Contributing

Feel free to make a pull request. A new service class can be as simple as 22 lines of code. Please try to be as PSR-2 compliant as possible. There's no shame if you misplaced a bracket or so!

Testing

After installing the dependencies (composer install) you just need to run phpunit to run the entire test-suite.

License

MIT