Download the PHP package risan/oauth1 without Composer
On this page you can find all versions of the php package risan/oauth1. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package oauth1
Short Description OAuth 1.0 client library for PHP
License MIT
Homepage https://github.com/risan/oauth1
Informations about the package oauth1
OAuth 1.0 Client Library for PHP
Simple, fluent and extensible OAuth 1.0 client library for PHP.
Table of Contents
- Installation
- Quick Start Guide
- Configuration
- Signature
- OAuth 1.0 Flow
- Step 1: Obtaining Temporary Credentials
- Step 2: Generate and Redirect User to Authorization URI
- Step 3: Obtaining Token Credentials
- Step 4: Accessing the Protected Resource
- Making HTTP Request
- Working with the Response
- Built-In Providers
- Trello
- Tumblr
- Upwork
Installation
The recommended way to install this package is through Composer. Run the following command in your terminal to install this package:
Quick Start Guide
This package is flexible. You can use it to interact with any providers that implement OAuth 1.0 protocol, like Twitter.
Here's a quick example of how to use this package to interact with Twitter API: fetching the authorized user's tweets.
Configuration
You can use the static create
method on Risan\OAuth1\OAuth1Factory
class to easily create an instance of Risan\OAuth1\Auth1
class. It requires you to pass a configuration array with the following keys:
client_credentials_identifier
: The client credentials identifier, also known as a consumer key or API key.client_credentials_secret
: The client credentials secret, also known as a consumer secret or API secret.temporary_credentials_uri
: The URI for obtaining the temporary credentials (also known as request token).authorization_uri
: The URI for authorizing user.token_credentials_uri
: The URI for obtaining the token credentials (also known as access token).
There are also two optional configuration that you can pass:
callback_uri
: The URI where the user will be redirected to after successfull authorization.base_uri
: The base URI that will be used to build an absolute URI if you pass a relative URI to configuration array or when sending a request to the protected resource.
Signature
Each HTTP request must include a signature so that the provider can verify the authenticity of that request. This signing process is handled by the signer instance that implements the Risan\OAuth1\Signature\SignerInterface
interface. This package includes two signer classes that you can use:
Risan\OAuth1\Signature\HmacSha1Signer
: for signing request with HMAC-SHA1 method.Risan\OAuth1\Signature\PlainTextSigner
: for signing request with PLAIN TEXT method.
You can pass this signer instance as the second argument to the create
static method on Risan\OAuth1\OAuth1Factory
class:
If you do not pass any signer instance to the create
method, the default HMAC-SHA1 signer will be used.
You can also create a custom signer class, as long as it impelements the Risan\OAuth1\Signature\SignerInterface
interface.
OAuth 1.0 Flow
In order to access a protected resource, the OAuth 1.0 flow can be broken down into four steps:
Step 1: Obtaining Temporary Credentials
The very first step is to obtain the temporary credentials or mostly known as the access token. To obtain it, you need to call the requestTemporaryCredentials
method on the Risan\OAuth1\OAuth1
instance:
It will return an instance of Risan\OAuth1\Credentials\TemporaryCredentials
class, which later you'll use to generate an authorization URI (Step 2) and to obtain the token credentials (Step 3).
Step 2: Generate and Redirect User to Authorization URI
Once you've got the temporary credentials, the second step is to generate and redirect the user to the authorization page. This is where the user will be asked to grant their permission to your application. You need to pass the previously obtained Risan\OAuth1\Credentials\TemporaryCredentials
class instance to the buildAuthorizationUri
method to generate the authorization URI:
Step 3: Obtaining Token Credentials
The third step is to obtain the token credentials, or also known as the access token. Upon successful authorization, the provider will redirect the user to the defined callback URI along with at least two additional query parameters:
oauth_token
oauth_verifier
Along with the previously obtained temporary credentials, you'll need to pass these two query parameters to requestTokenCredentials
method to obtain token credentials:
This method will return an instance of Risan\OAuth1\Credentials\TokenCredentials
class, which you're going to need to access the protected resource.
Step 4: Accessing the Protected Resource
Finally, once you've got the token credentials instance, you can start making a request to the protected resource. Pass the obtained Risan\OAuth1\Credentials\TokenCredentials
instance to the setTokenCredentials
method before making any requests to the protected resource, or else an exception will be thrown.
The request
method will return an instance of Psr\Http\Message\ResponseInterface
class.
Making HTTP Request
Once you've set the obtained token credentials with the setTokenCredentials
method, you can start making the HTTP request to the protected API endpoints. You can use the request
method for this purpose:
This method accepts three parameters:
method
(required): The HTTP method that you'd like to use (e.g.GET
,POST
,PUT
,PATCH
,DELETE
)uri
(required): The URI of the API endpoint that you'd like to access. You can also pass a relative URI as long as you pass thebase_uri
in the configuration array.options
(optional): It's an optional array paramater to configure your request. It's the same Request Options that you'll pass when making an HTTP request using Guzzle. You can check all available options that you can pass on Guzzle documentation.
There are also shortcut methods for common HTTP methods:
Working with the Response
The request
method will return an instance of Psr\Http\Message\ResponseInterface
. You can check the returned status code with the getStatusCode
method:
Or you can also get the headers on the returned response like so:
And to get the response's body, you can use the getBody
method. Note that it will return a Psr\Http\Message\StreamInterface
instance. To convert the StreamInterface
instace into a string, you can call the getContents
method or simply cast it into a string.
So if the API endpoint returns a JSON formatted response, you can covert the returned response into an associative array like this:
Built-In Providers
This package also offers some third-party providers that you can use.
Trello
Use the trello
method to create an instance of OAuth1
configured for Trello.
You can get both of your API key and secret from Developer API Keys page. The base URI is set to https://api.trello.com/1/
so you can use a relative URI instead:
Tumblr
Use the tumblr
method to create an instance of OAuth1
configured for Tumblr.
You need to register a Tumblr application here to get the consumer key and secret. The base URI is set to https://api.tumblr.com/v2/
so you can use a relative URI instead:
Use the twitter
method to create an instance of OAuth1
configured for Twitter.
You need to register a Twitter application here to get the consumer key and secret. The base URI is set to https://api.twitter.com/1.1/
so you can use a relative URI instead:
Upwork
Use the upwork
method to create an instance of OAuth1
configured for Upwork.
You need to register a Upwork application here to get the API key and secret. The base URI is set to https://www.upwork.com/
so you can use a relative URI instead:
License
MIT © Risan Bagja Pradana
All versions of oauth1 with dependencies
guzzlehttp/guzzle Version ~6.3
guzzlehttp/psr7 Version ^1.4.2
paragonie/random_compat Version ~1.4|~2.0
psr/http-message Version ^1.0