Giter Site home page Giter Site logo

guzzle-oauth2-plugin's Introduction

guzzle-oauth2-plugin

Provides an OAuth2 plugin for Guzzle.

Features

  • Acquires access tokens via one of the supported grant types (code, client credentials, user credentials, refresh token). Or you can set an access token yourself.
  • Supports refresh tokens (stores them and uses them to get new access tokens).
  • Handles token expiration (acquires new tokens and retries failed requests).

Ways to use it:

Just set an access token and make a request.

use Guzzle\Http\Client;
use CommerceGuys\Guzzle\Plugin\Oauth2\Oauth2Plugin;

$accessToken = array(
  'access_token' => 'e72758a43e1646969f9f7bd7737d0cd637ed17ae',
  'expires' => '1391617481', // Optional.
);
$oauth2Plugin = new Oauth2Plugin();
$oauth2Plugin->setAccessToken($accessToken);

$client = new Client('https://mysite.com/api');
$client->addSubscriber($oauth2Plugin);
$request = $client->get('me');

Or use a grant type. Optionally, add a refresh token grant type, used to refresh expired access tokens.

use Guzzle\Http\Client;
use CommerceGuys\Guzzle\Plugin\Oauth2\Oauth2Plugin;
use CommerceGuys\Guzzle\Plugin\Oauth2\GrantType\PasswordCredentials;
use CommerceGuys\Guzzle\Plugin\Oauth2\GrantType\RefreshToken;

$oauth2Client = new Client('https://mysite.com/oauth2/token');
$config = array(
    'username' => 'myusername',
    'password' => 'mypassword',
    'client_id' => 'myclient',
    'client_secret' => 'mysecret',
    'scope' => 'administration', // Optional.
);
$grantType = new PasswordCredentials($oauth2Client, $config);
$refreshTokenGrantType = new RefreshToken($oauth2Client, $config);
$oauth2Plugin = new Oauth2Plugin($grantType, $refreshTokenGrantType);

$client = new Client('https://mysite.com/api');
$client->addSubscriber($oauth2Plugin);
$request = $client->get('me');
// $oauth2Plugin->getAccessToken() and $oauth2Plugin->getRefreshToken() can be
// used to export the tokens so that they can be persisted for next time.

guzzle-oauth2-plugin's People

Contributors

bojanz avatar darrylkuhn avatar marfillaster avatar

Watchers

 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.