node-instagram
Advanced tools
Comparing version 2.0.2 to 3.0.0
import Stream from './stream'; | ||
export interface InstagramConfig { | ||
clientId: string; | ||
clientSecret: string; | ||
accessToken: string; | ||
@@ -8,2 +9,3 @@ apiVersion?: string; | ||
declare class Instagram { | ||
private baseApiUrl; | ||
private apiUrl; | ||
@@ -16,4 +18,6 @@ private config; | ||
stream(endpoint: string, options?: any): Stream; | ||
getAuthorizationUrl(redirectUri: string, options?: any): string; | ||
authorizeUser(code: string, redirectUri: string, callback?: (err?: any, data?: any) => void): Promise<any>; | ||
private request(type, endpoint, options?, callback?); | ||
} | ||
export default Instagram; |
@@ -11,3 +11,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var isFunction = require("lodash.isfunction"); | ||
var lodash_1 = require("lodash"); | ||
var requestPromise = require("request-promise"); | ||
@@ -18,5 +18,7 @@ var stream_1 = require("./stream"); | ||
var apiVersion = config.apiVersion || 'v1'; | ||
this.apiUrl = "https://api.instagram.com/" + apiVersion + "/"; | ||
this.baseApiUrl = 'https://api.instagram.com'; | ||
this.apiUrl = this.baseApiUrl + "/" + apiVersion + "/"; | ||
this.config = { | ||
clientId: config.clientId, | ||
clientSecret: config.clientSecret, | ||
accessToken: config.accessToken, | ||
@@ -37,5 +39,30 @@ }; | ||
}; | ||
Instagram.prototype.getAuthorizationUrl = function (redirectUri, options) { | ||
if (options === void 0) { options = {}; } | ||
var authorizationUrl = this.baseApiUrl + "/oauth/authorize/?client_id=" + this | ||
.config.clientId + "&redirect_uri=" + redirectUri + "&response_type=code"; | ||
if (options.scope) { | ||
if (lodash_1.isArray(options.scope)) { | ||
options.scope = options.scope.join('+'); | ||
} | ||
authorizationUrl += "&scope=" + options.scope; | ||
} | ||
if (options.state) { | ||
authorizationUrl += "&state=" + options.state; | ||
} | ||
return authorizationUrl; | ||
}; | ||
Instagram.prototype.authorizeUser = function (code, redirectUri, callback) { | ||
return this.request('POST', 'oauth/access_token', { | ||
uriAbsolute: true, | ||
code: code, | ||
redirect_uri: redirectUri, | ||
client_id: this.config.clientId, | ||
client_secret: this.config.clientSecret, | ||
grant_type: 'authorization_code', | ||
}); | ||
}; | ||
Instagram.prototype.request = function (type, endpoint, options, callback) { | ||
if (options === void 0) { options = {}; } | ||
if (isFunction(options)) { | ||
if (lodash_1.isFunction(options)) { | ||
callback = options; | ||
@@ -45,7 +72,12 @@ options = {}; | ||
var key = 'qs'; | ||
var accessToken = this.config.accessToken; | ||
var uri = "" + this.apiUrl + endpoint; | ||
options.access_token = this.config.accessToken; | ||
if (options.accessToken) { | ||
accessToken = options.accessToken; | ||
options.access_token = options.accessToken; | ||
delete options.accessToken; | ||
} | ||
if (options.uriAbsolute) { | ||
uri = this.baseApiUrl + "/" + endpoint; | ||
delete options.uriAbsolute; | ||
} | ||
if (type === 'POST') { | ||
@@ -56,9 +88,9 @@ key = 'form'; | ||
method: type, | ||
uri: "" + this.apiUrl + endpoint | ||
uri: uri | ||
}, | ||
_a[key] = __assign({ access_token: accessToken }, options), | ||
_a[key] = __assign({}, options), | ||
_a.json = true, | ||
_a)) | ||
.then(function (data) { | ||
if (isFunction(callback)) { | ||
if (lodash_1.isFunction(callback)) { | ||
callback(null, data); | ||
@@ -70,3 +102,3 @@ } | ||
var error = err.error || err; | ||
if (isFunction(callback)) { | ||
if (lodash_1.isFunction(callback)) { | ||
return callback(error); | ||
@@ -73,0 +105,0 @@ } |
{ | ||
"name": "node-instagram", | ||
"description": "Instagram api client for node that support promises", | ||
"version": "2.0.2", | ||
"version": "3.0.0", | ||
"author": "Leo Pradel", | ||
@@ -47,3 +47,3 @@ "keywords": [ | ||
"@types/request-promise": "^4.1.33", | ||
"lodash.isfunction": "^3.0.8", | ||
"lodash": "^4.17.4", | ||
"request": "^2.81.0", | ||
@@ -50,0 +50,0 @@ "request-promise": "^4.2.1" |
127
README.md
@@ -12,3 +12,3 @@ [](https://badge.fury.io/js/node-instagram) | ||
To see all endpoint available take a look at [instagram developer documentation](https://www.instagram.com/developer/endpoints/). | ||
You can find examples in the [examples](https://github.com/pradel/node-instagram/tree/master/examples) directory. | ||
@@ -28,6 +28,6 @@ ## Install | ||
// Create a new instance. | ||
const instagram = new Instagram({ | ||
clientId: 'your-client-id', | ||
clientSecret: 'your-client-secret', | ||
accessToken: 'user-access-token', | ||
@@ -38,3 +38,8 @@ }); | ||
instagram.get('users/self', (err, data) => { | ||
console.log(data); | ||
if (err) { | ||
// an error occured | ||
console.log(err); | ||
} else { | ||
console.log(data); | ||
} | ||
}); | ||
@@ -46,2 +51,79 @@ | ||
// Handle errors | ||
instagram.get('tags/paris').then((data) => { | ||
console.log(data); | ||
}).catch((err) => { | ||
// An error occured | ||
console.log(err); | ||
}); | ||
``` | ||
## Streaming | ||
This lib have a stream method. It is used to receive new post as events. Streaming **can only be used** on all endpoints taking MIN_TAG_ID as parameter. Inside it is running setInterval. | ||
```javascript | ||
const stream = instagram.stream('tags/:tag-name/media/recent'); | ||
stream.on('messages', (messages) => { | ||
console.log(messages); | ||
}); | ||
// handle stream error | ||
stream.on('error', (err) => { | ||
// An error occur | ||
console.log(err); | ||
}); | ||
``` | ||
## Server side authentication | ||
Two steps are needed in order to receive an access_token for a user. | ||
- Get an authentication url from instagram and redirect the user to it | ||
- Exchange the code for an access_token | ||
You can find a working example with express [here](https://github.com/pradel/node-instagram/tree/master/examples/express-auth). | ||
To see more info about server side authentication take a look at the [instagram documentation](https://www.instagram.com/developer/authentication/). | ||
```javascript | ||
// Example with express | ||
// Your redirect url where you will handle the code param | ||
const redirectUri = 'http://localhost:3000/auth/instagram/callback'; | ||
// First redirect user to instagram oauth | ||
app.get('/auth/instagram', (req, res) => { | ||
res.redirect(instagram.getAuthorizationUrl(redirectUri, { | ||
// an array of scopes | ||
scope: ['basic', 'likes'] }, | ||
// an optional state | ||
state: 'your state', | ||
)); | ||
}); | ||
// Handle auth code and get access_token for user | ||
app.get('/auth/instagram/callback', async (req, res) => { | ||
try { | ||
// The code from the request, here req.query.code for express | ||
const code = req.query.code; | ||
const data = await instagram.authorizeUser(code, redirectUri); | ||
// data.access_token contain the user access_token | ||
res.json(data); | ||
} catch (err) { | ||
res.json(err); | ||
} | ||
}); | ||
``` | ||
## Endpoints | ||
To see all endpoint available take a look at [instagram developer documentation](https://www.instagram.com/developer/endpoints/). | ||
```javascript | ||
// Get information about current user | ||
instagram.get('users/self', (err, data) => { | ||
console.log(data); | ||
}); | ||
// Get information about a user. | ||
@@ -106,25 +188,2 @@ instagram.get('users/:user-id').then((data) => { | ||
}); | ||
// Handle errors | ||
instagram.get('tags/paris').then((data) => { | ||
console.log(data); | ||
}).catch((err) => { | ||
// An error occur | ||
console.log(err); | ||
}); | ||
// Fake stream for instagram (running setInterval inside) | ||
// Streaming can be used on all endpoints taking MIN_TAG_ID as parameter | ||
const stream = instagram.stream('tags/:tag-name/media/recent'); | ||
stream.on('messages', (messages) => { | ||
console.log(messages); | ||
}); | ||
// handle stream error | ||
stream.on('error', (err) => { | ||
// An error occur | ||
console.log(err); | ||
}); | ||
``` | ||
@@ -169,1 +228,17 @@ | ||
* `params.minTagId` **boolean** instagram min_tag_id to start request | ||
### `instagram.getAuthorizationUrl(redirectUri, options)` | ||
Get a valid auth url for instagram | ||
#### Arguments | ||
* `redirectUri` **string** the url to redirect the user with the code | ||
* `options` **object** | ||
* `options.scope` **array|string** the scope to request | ||
* `options.state` **string** optional state | ||
* `callback` **function** | ||
### `instagram.authorizeUser(code, redirectUri, [callback])` | ||
Handle the code returned by instagram an get a user access_token | ||
#### Arguments | ||
* `redirectUri` **string** code returned by instagram | ||
* `redirectUri` **string** | ||
* `callback` **function** |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
118880
16
269
239
+ Addedlodash@^4.17.4
- Removedlodash.isfunction@^3.0.8
- Removedlodash.isfunction@3.0.9(transitive)