Constructor
new RedditApi(options)
Creates instance of RedditApi.
Parameters:
Name | Type | Description | ||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
Object | Options object
Properties
|
Members
(nullable) access_token :String
OAuth access token
Type:
- String
app_id :String
Reddit app ID
Type:
- String
app_secret :String
Reddit app secret
Type:
- String
(nullable) redirect_uri :String
Reddit app redirect URI
Type:
- String
(nullable) refresh_token :String
OAuth refresh token
Type:
- String
throttled_request :function
Throttled request function
Refer to: https://www.npmjs.com/package/throttled-request
Type:
- function
user_agent :String
HTTP user agent header sent to Reddit for each request
Type:
- String
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 |
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 |
isAuthed() → {Boolean}
Checks if user is authenticated based on the presence of an access token.
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
|
|||||||||
callback |
RedditApi~ApiTokenCallback | Callback function to invoke after tokens are retrieved |
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 |
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 |
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 |
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 |
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
|
||||||||||||||||||||||||||||||||
callback |
RedditApi~ApiRequestCallback | Callback function | ||||||||||||||||||||||||||||||||||
is_refreshing_token |
Boolean |
<optional> |
false | If false, will attempt to refresh tokens then retry request |
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 |
ApiRequestCallback(errornullable, incomingMessage, responseBody)
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
error |
Object |
<nullable> |
|
incomingMessage |
Object | ||
responseBody |
String | Buffer | Object |
ApiTokenCallback(success)
Parameters:
Name | Type | Description |
---|---|---|
success |
Boolean |