HTTP Response Code 400 - Unsupported Grant Type

Posted in General by Swarup Karavadi Wed May 03 2017 10:42:13 GMT+0000 (UTC)·Viewed 647 times

Hi, I am making a https request to your sandbox environment using node.js. I am using the following code for that purpose - ``` import fetch from 'isomorphic-fetch'; import FormData from 'form-data'; let tokenEndpoint = 'https://test.instamojo.com/oauth2/token/', paymentLinkEndpoint = 'https://test.instamojo.com/v2/payment_requests/'; export const getAccessToken = async ({username, password, clientId, clientSecret}) => { var globalLog = require('global-request-logger'); globalLog.initialize(); globalLog.on('success', function(request, response) { console.log('Request', request); console.log('Response', response); }); let form = new FormData(); form.append('grant_type', "password"); form.append('client_id', clientId); form.append('client_secret', clientSecret); form.append('username', username); form.append('password', password); let headers = { 'cache-control': 'no-cache', 'content-type': 'multipart/form-data; boundary=CUSTOM' } let response = await fetch(tokenEndpoint, { method: 'POST', headers, body: form }); let responsePayload = await response.json(); let { access_token } = responsePayload; globalLog.end(); return access_token; } ``` Since I am logging the global node http requests and responses, I am sharing the logs here hoping they might shed some light as to why I am getting a 400 response. The Request - ``` { port: null, [1] path: '/oauth2/token/', [1] protocol: 'https:', [1] auth: null, [1] hostname: 'test.instamojo.com', [1] method: 'POST', [1] headers: [1] { 'cache-control': [ 'no-cache' ], [1] 'content-type': [ 'multipart/form-data; boundary=CUSTOM' ], [1] 'accept-encoding': [ 'gzip,deflate' ], [1] 'user-agent': [ 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)' ], [1] connection: [ 'close' ], [1] accept: [ '*/*' ], [1] 'content-length': [ '803' ], [1] host: 'test.instamojo.com' }, [1] body: '----------------------------305344778095309636757950\r\nContent-Disposition: form-data; name="grant_type"\r\n\r\npassword\r\n----------------------------305344778095309636757950\r\nContent-Disposition: form-data; name="client_id"\r\n\r\n<my_client_id>r\r\n----------------------------305344778095309636757950\r\nContent-Disposition: form-data; name="client_secret"\r\n\r\n<my_client_secret>\r\n----------------------------305344778095309636757950\r\nContent-Disposition: form-data; name="username"\r\n\r\n<myusername>\r\n----------------------------305344778095309636757950\r\nContent-Disposition: form-data; name="password"\r\n\r\n<mypassword>\r\n----------------------------305344778095309636757950--\r\n' } ``` And here is the response - ``` { statusCode: 400, [1] headers: [1] { date: 'Wed, 03 May 2017 10:39:21 GMT', [1] 'content-type': 'application/json', [1] 'transfer-encoding': 'chunked', [1] connection: 'close', [1] server: 'nginx', [1] 'x-frame-options': 'SAMEORIGIN', [1] vary: 'Cookie', [1] 'cache-control': 'no-store', [1] pragma: 'no-cache', [1] 'set-cookie': [ 'csrftoken=JdmaOvwHE1oH8b7z0qeouVzrcG3lHSi25XtcQhU8VdoCX04z07hv7yC09w0Ja4eX; expires=Wed, 02-May-2018 10:39:21 GMT; Max-Age=31449600; Path=/; secure' ], [1] 'x-xss-protection': '1; mode=block', [1] 'x-content-type-options': 'nosniff', [1] 'strict-transport-security': 'max-age=31536000', [1] p3p: 'CP="Instamojo does not have a P3P policy"' }, [1] trailers: {}, [1] httpVersion: '1.1', [1] url: '', [1] method: null, [1] body: '{"error": "unsupported_grant_type"}' } ``` I'd really appreciate a response on this. Cheers, Swarup.
  
Markdown is allowed