Giter VIP home page Giter VIP logo

php-oauth's Introduction

OAuth service classes with Guzzle (v4 and v5, but not v6)

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

php-oauth's People

Contributors

hannesvdvreken avatar neovdr avatar tankist avatar

Watchers

James Cloos avatar Frank Kessler avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.