Class: RedditApi

RedditApi

Reddit API Controller which facilitates authentication and API endpoint queries.

Constructor

new RedditApi(options)

Creates instance of RedditApi.
Parameters:
Name Type Description
options Object Options object
Properties
Name Type Attributes Default Description
app_id String Reddit app ID
app_secret String Reddit app secret
redirect_uri String <optional>
null Reddit app redirect URI
user_agent String <optional>
reddit-oauth/x.y.z by aihamh HTTP user agent header sent to Reddit for each request
access_token String <optional>
null OAuth access token
refresh_token String <optional>
null OAuth refresh token
request_buffer Number <optional>
2000 Time in milliseconds to buffer for each request
Source:

Members

(nullable) access_token :String

OAuth access token
Type:
  • String
Default Value:
  • null
Source:

app_id :String

Reddit app ID
Type:
  • String
Source:

app_secret :String

Reddit app secret
Type:
  • String
Source:

(nullable) redirect_uri :String

Reddit app redirect URI
Type:
  • String
Default Value:
  • null
Source:

(nullable) refresh_token :String

OAuth refresh token
Type:
  • String
Default Value:
  • null
Source:

throttled_request :function

Throttled request function Refer to: https://www.npmjs.com/package/throttled-request
Type:
  • function
Source:

user_agent :String

HTTP user agent header sent to Reddit for each request
Type:
  • String
Default Value:
  • reddit-oauth/x.y.z by aihamh
Source:

Methods

get(path, params, callback)

Execute an authenticated GET request to the specified API endpoint.
Parameters:
Name Type Description
path String API endpoint path
params Object Key/value pairs to send as the request query string
callback RedditApi~ApiRequestCallback Callback function
Source:

getListing(path, params, callback)

Request a page of values from the specified listing endpoint. Use the additional 'next' callback argument to request the next page, repeatedly until 'next' equals null.
Parameters:
Name Type Description
path String
params Object
callback RedditApi~ApiListingRequestCallback Invoke the next callback to retrieve the next page of the list
Source:

isAuthed() → {Boolean}

Checks if user is authenticated based on the presence of an access token.
Source:
Returns:
Type
Boolean

oAuthTokens(state, query, callback)

Upon user returning from authorization URL, use supplied code to request access and fresh tokens.
Parameters:
Name Type Description
state String The same arbitrary string that was used in RedditApi#oAuthUrl
query Object Key/value pairs from HTTP query string constructed by Reddit
Properties
Name Type Description
state String Should be the string passed into RedditApi#oAuthUrl
code String A one time use token provided by Reddit to be exchanged for access and refresh tokens
callback RedditApi~ApiTokenCallback Callback function to invoke after tokens are retrieved
Source:

oAuthUrl(state, scope) → {String}

Get OAuth authorization URL for specific scope
Parameters:
Name Type Description
state String An arbitrary string that is checked when user returns with the code
scope String | Array.<String> Array or comma separated list of scopes to request from user
Source:
Returns:
URL to send user's browser to
Type
String

passAuth(username, password, callback)

Authenticate with username and password
Parameters:
Name Type Description
username String Reddit username
password String Reddit password
callback RedditApi~ApiRequestCallback Request callback
Source:

post(path, params, callback)

Execute an authenticated POST request to the specified API endpoint.
Parameters:
Name Type Description
path String API endpoint path
params Object Key/value pairs to send as the request POST body
callback RedditApi~ApiRequestCallback Callback function
Source:

refreshToken(callback)

Request a new access token using the existing refresh token.
Parameters:
Name Type Description
callback RedditApi~ApiTokenCallback Callback function to invoke after the access token is retrieved
Source:

request(path, optionsopt, callback, is_refreshing_tokenopt)

Create new API request to specified API endpoint with custom options and callback to be invoked on request completion. If authentication error occurs, is_refreshing_token is false and a refresh token is currently defined, then it will automatically attempt to retrieve a new access token then try again.
Parameters:
Name Type Attributes Default Description
path String API endpoint path
options external:Request~Options <optional>
{} Request options
Properties
Name Type Attributes Default Description
method String <optional>
GET HTTP method
url String <optional>
https://(oauth|ssl).reddit.com/:path Request URL. ssl subdomain used for authentication; oauth for authenticated queries
headers Object <optional>
{} HTTP headers
Properties
Name Type Attributes Description
User-Agent String <optional>
User agent
Authorization String <optional>
Bearer token if available
callback RedditApi~ApiRequestCallback Callback function
is_refreshing_token Boolean <optional>
false If false, will attempt to refresh tokens then retry request
Source:

Type Definitions

ApiListingRequestCallback(errornullable, incomingMessage, responseBody, nextnullable)

Parameters:
Name Type Attributes Description
error Object <nullable>
incomingMessage Object
responseBody String | Buffer | Object
next function <nullable>
Invoke to retrieve the next page in the listing, until next equals null
Source:

ApiRequestCallback(errornullable, incomingMessage, responseBody)

Parameters:
Name Type Attributes Description
error Object <nullable>
incomingMessage Object
responseBody String | Buffer | Object
Source:

ApiTokenCallback(success)

Parameters:
Name Type Description
success Boolean
Source: