diff --git a/lib/imgur.js b/lib/imgur.js index 193d54cc..0892a0ef 100644 --- a/lib/imgur.js +++ b/lib/imgur.js @@ -111,13 +111,14 @@ imgur._imgurRequest = async (operation, payload, extraFormParams) => { } response = await imgur._request(options); - if (response && !response.success) { + const { statusCode: status, statusMessage: message } = response; + if (status !== 200) { throw new Error({ - status: response.status, - message: response.data ? response.error : 'No body data response', + status, + message, }); } else { - return response.data; + return JSON.parse(response.body).data; } }; @@ -127,7 +128,7 @@ imgur._imgurRequest = async (operation, payload, extraFormParams) => { * @param {object} options * @returns {promise} */ -imgur._request = async (options) => await got(options).json(); +imgur._request = async (options) => await got(options); /** * Get imgur access token using credentials @@ -144,7 +145,7 @@ imgur._getAuthorizationHeader = async () => { } const options = { - uri: 'https://api.imgur.com/oauth2/authorize', + url: 'https://api.imgur.com/oauth2/authorize', method: 'GET', encoding: 'utf8', searchParams: { @@ -166,8 +167,9 @@ imgur._getAuthorizationHeader = async () => { password: imgurPassword, allow: authorize_token, }; + options.followRedirect = false; options.headers = { - Cookie: 'authorize_token=' + authorize_token, + cookie: 'authorize_token=' + authorize_token, }; response = await imgur._request(options);