Repository navigation
Expand file tree
/
Copy pathinstagram.js
More file actions
181 lines (152 loc) · 4.31 KB
/
Copy pathinstagram.js
File metadata and controls
181 lines (152 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
'use strict'
// Module dependencies.
const url = require('url')
const got = require('got')
const qs = require('querystring')
const debug = require('debug')('insta:client')
/**
* @constant
* @type {string}
* @default
*/
const AUTH_AUTHORIZATION_PATH = '/oauth/authorize'
/**
* @constant
* @type {string}
* @default
*/
const AUTH_ACCESS_TOKEN_PATH = '/oauth/access_token'
/**
* @constant
* @type {string[]}
* @default
*/
const AUTH_PARAMS = ['client_id', 'client_secret', 'access_token']
/**
* Class representing Instagram client.
*/
class Instagram {
/**
* Create an instance.
*
* Authorization options can later be set
* with call to {@link Instagram#setAuth}.
*
* @param {Object} [options] Instance options.
* @param {string} [options.client_id] Client ID.
* @param {string} [options.client_secret] Client Secret.
* @param {string} [options.host=https://api.instagram.com] Instagram API endpoint.
*/
constructor (options) {
options || (options = {})
this._host = options.host || 'https://api.instagram.com'
this._auth = {}
this.setAuth(options)
}
/**
* Set authorization parameters.
*
* @param {Object} options Authorization parameters.
* @param {string} [options.client_id] Client ID.
* @param {string} [options.client_secret] Client Secret.
*
* @returns {Instagram} This instance.
*/
setAuth (options) {
AUTH_PARAMS.forEach((param) => {
if (typeof options[param] !== 'undefined') {
this._auth[param] = options[param]
}
})
return this
}
/**
* Get current authorization parameters.
*
* @returns {Object} Current authorization parameters.
*/
getAuth () {
return Object.assign({}, this._auth)
}
/**
* Create request.
*
* @param {string} method HTTP method.
* @param {string} path Resource path.
* @param {Object} [options] Request options passed to
* [Got]{@link https://github.com/sindresorhus/got#goturl-options}
*
* @returns {Promise<Object>} A promise resolving to response object.
*/
request (method, path, options) {
options = Object.assign({
method: method,
path: path,
json: true
}, options)
debug('request: %s, %j', this._host, options)
return got(this._host, options)
}
/**
* Generate authorization URL.
*
* For more information see
* [Instagram Authentication]{@link https://www.instagram.com/developer/authentication/}.
*
* @param {string} redirectUri Redirect URI
* @param {Object} [options] Additional query parameters.
* @param {string} [options.state] Server specific state.
* @param {string|string[]} [options.scope] Additional permissions outside
* of the "basic" permissions scope. See
* [Instagram Authorization]{@link https://www.instagram.com/developer/authorization/}
*
* @returns {string} An authorization URL.
*/
getAuthorizationUrl (redirectUri, options) {
if (!this._auth.client_id) {
throw new Error('Authorization parameter `client_id` not set')
}
let params = Object.assign({
client_id: this._auth.client_id,
redirect_uri: redirectUri,
response_type: 'code'
}, options)
if (Array.isArray(params.scope)) {
params.scope = params.scope.join('+')
}
let urlObj = url.parse(this._host)
urlObj.pathname = AUTH_AUTHORIZATION_PATH
urlObj.query = params
return url.format(urlObj)
}
/**
* Exchange authorization code for access token.
*
* @param {string} code Authorization code received from service.
* @param {string} redirectUri Redirect URI.
*
* @throws {Error}
*
* @returns {Promise<Object>} Response containing access token.
*/
requestAccessToken (code, redirectUri) {
if (!this._auth.client_id || !this._auth.client_secret) {
throw new Error('Authorization parameters `client_id` and `client_secret` not set')
}
let params = {
client_id: this._auth.client_id,
client_secret: this._auth.client_secret,
grant_type: 'authorization_code',
redirect_uri: redirectUri,
code: code
}
let body = qs.stringify(params)
return this.request('POST', AUTH_ACCESS_TOKEN_PATH, {
body: body,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then((res) => res.body)
}
}
module.exports = Instagram