oauthor
OAuth2 client library for node
Get started
Install the oauthor package:
npm install --save oauthor
// Step 1: Create oauth config var oauthor = ; var oauth = ; // Step 2: Perform authorize request and handle redirect var code = ; // Step 3: Get code from the redirect and exchange for access token. oauth;
Case study: Facebook
// Step 1: Create oauth config var oauthor = ; var oauth = ; // Step 2: Perform authorize request var code = ; // Step 3: Get code from the redirect and exchange for access token. oauth;
Use with generators
The oauthor package exposes a simple Promise api ready to be used with generators (using co
and the likes).
// Step 1: Create oauth config var oauthor = ; var oauth = ; // Step 2: Perform authorize request and handle redirect var code = ; // Step 3: Get code from the redirect and exchange for access token. var response = oauth; // The provider should have sent an access token in the response var accessToken = responseaccess_token; // Sign all subsequent requests to the provider. var client = oauth; // Use `client` now to make authorized requests. var user = ;
API
authorizeUrl
Creates the url that should be used for authorization, with the specified parameters as query string.
Parameters
params
[Object]
Parameters to send in the query string. (optional, default{}
)
Returns String
The url that can be used to perform authorization.
requestAccessToken
Performs a request to the access token endpoint that was specified, with the code received from the authorization step, and optional parameters.
Parameters
code
String
The code obtained in the authorization step.params
[Object]
Parameters that will be posted to the request. (optional, default{}
)
Returns Object
The response from the provider.
sign
Specifies an access token that should be used to sign all subsequent
requests. The return value is a function that can be used to make
authorized requests. The function takes an object specifying the request
options that will be passed to the request
function.
See request for more info on the supported options.
Parameters
accessToken
String
The access token obtained from the provider.
Examples
// Sign all requests with `youraccesscode`. var client = oauth; // Suppose you have the endpoint `https://provider.com/me` that // returns your user profile when queried.
Returns Function