Error getting access token

Solved!
Posted in General by Ritej Bisaria Sun Jan 22 2017 13:51:59 GMT+0000 (UTC)·7·Viewed 3,257 times

I am trying to initiate the access token generation using the post api call provided in the documentation. This is my code: var myJSONObject = {}; myJSONObject.client_id = clientId; myJSONObject.client_secret = clientSecret; myJSONObject.grant_type = 'test_credentials'; request .post('https://test.instamojo.com/oauth2/token/') .set('Content-Type', 'application/json;charset=utf-8') .send(myJSONObject) .end(function(err, response, body) { console.log(response); res.json({ success:true, error:null, data:response }); }); I am getting a response of { error: 'unsupported_grant_type' } Please if you could guide me in the direction of what i am doing wrong?
Vedhavyas
Jan 22, 2017

The client type should be "client_credentials"

Vedhavyas
Jan 22, 2017

grant_type *

Ritej Bisaria
Jan 22, 2017

sorry posted the wrong code

var myJSONObject = {};
myJSONObject.client_id = clientId;
myJSONObject.client_secret = clientSecret;
myJSONObject.grant_type = 'client_credentials';

request
    .post('https://test.instamojo.com/oauth2/token/')
    .set('Content-Type', 'application/json;charset=utf-8')
    .send(myJSONObject)
    .end(function(err, response, body) {
        console.log(response);
        res.json({
            success:true,
            error:null,
            data:response
        });
    });

and with these credentials im getting:
error:
{ [Error: cannot POST /oauth2/token/ (400)]
status: 400,
text: '{"error": "unsupported_grant_type"}',
method: 'POST',
path: '/oauth2/token/' },

Instamojo
Jan 22, 2017

You are sending the parameters as json. Send them as form-data instead.


Instamojo marked this as solved
Ritej Bisaria
Jan 22, 2017

i did, that's also not doing the trick.
My code is in NodeJS :

request({
    url: 'https://test.instamojo.com/oauth2/token/', //URL to hit
    method: 'POST',
    //Lets post the following key/values as form
    form: {
        client_id: 'MJ6HmHbJ5Sp6s5jVPTLH3j86AGDeGu3RmQp4tmKR',
        client_secret: 'kQT5nMz4cN8AFfvyhEv38594nXfRKF4ds0IpiVU101gEWN8XmI5pMoSl7rAeSC1VVTRejUK3KNeytAMeJIs6CoweufkCMNZD9KjrtzM4LqmmE5k9v6NtKo0VzYken7hB',
        grant_type: 'client_credentials'
    }
}, function(error, response, body){
    if(error) {
        console.log(error);
    } else {
        console.log(response);
        res.json({
            success:true,
            data:body,
            error:null
        })
    }
});

The response this is giving is:

req:
ClientRequest {
domain: null,
_events: { drain: [Function], error: [Function], response: [Function] },
_eventsCount: 3,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 437,
writable: true,
_last: true,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: true,
sendDate: false,
_removedHeader: { 'content-length': false },
_contentLength: 223,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket:
TLSSocket {
_tlsOptions: [Object],
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
_SNICallback: null,
servername: null,
npnProtocol: false,
authorized: true,
authorizationError: null,
encrypted: true,
_events: [Object],
_eventsCount: 9,
_connecting: false,
_hadError: false,
_handle: null,
_parent: null,
_host: 'test.instamojo.com',
_readableState: [Object],
readable: false,
domain: null,
_maxListeners: undefined,
_writableState: [Object],
writable: false,
allowHalfOpen: false,
destroyed: true,
bytesRead: 604,
_bytesDispatched: 437,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
ssl: [Object],
server: undefined,
_requestCert: true,
_rejectUnauthorized: true,
parser: null,
_httpMessage: [Circular],
read: [Function],
_consuming: true,
_idleNext: null,
_idlePrev: null,
_idleTimeout: -1 },
connection:
TLSSocket {
_tlsOptions: [Object],
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
_SNICallback: null,
servername: null,
npnProtocol: false,
authorized: true,
authorizationError: null,
encrypted: true,
_events: [Object],
_eventsCount: 9,
_connecting: false,
_hadError: false,
_handle: null,
_parent: null,
_host: 'test.instamojo.com',
_readableState: [Object],
readable: false,
domain: null,
_maxListeners: undefined,
_writableState: [Object],
writable: false,
allowHalfOpen: false,
destroyed: true,
bytesRead: 604,
_bytesDispatched: 437,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
ssl: [Object],
server: undefined,
_requestCert: true,
_rejectUnauthorized: true,
parser: null,
_httpMessage: [Circular],
read: [Function],
_consuming: true,
_idleNext: null,
_idlePrev: null,
_idleTimeout: -1 },
_header: 'POST /oauth2/token/ HTTP/1.1\r\nHost: test.instamojo.com\r\nAccept-Encoding: gzip, deflate\r\nUser-Agent: node-superagent/2.3.0\r\nContent-Type: application/x-www-form-ur
lencoded\r\nContent-Length: 223\r\nConnection: close\r\n\r\n',
_headers:
{ host: 'test.instamojo.com',
'accept-encoding': 'gzip, deflate',
'user-agent': 'node-superagent/2.3.0',
'content-type': 'application/x-www-form-urlencoded',
'content-length': 223 },
_headerNames:
{ host: 'Host',
'accept-encoding': 'Accept-Encoding',
'user-agent': 'User-Agent',
'content-type': 'Content-Type',
'content-length': 'Content-Length' },
_onPendingData: null,
agent:
Agent {
domain: null,
_events: [Object],
_eventsCount: 1,
_maxListeners: undefined,
defaultPort: 443,
protocol: 'https:',
options: [Object],
requests: {},
sockets: [Object],
freeSockets: {},
keepAliveMsecs: 1000,
keepAlive: false,
maxSockets: Infinity,
maxFreeSockets: 256,
maxCachedSessions: 100,
_sessionCache: [Object] },
socketPath: undefined,
method: 'POST',
path: '/oauth2/token/',
parser: null,
res:
IncomingMessage {
_readableState: [Object],
readable: false,
domain: null,
_events: [Object],
_eventsCount: 4,
_maxListeners: undefined,
socket: [Object],
connection: [Object],
httpVersionMajor: 1,
httpVersionMinor: 1,
httpVersion: '1.1',
complete: true,
headers: [Object],
rawHeaders: [Object],
trailers: {},
rawTrailers: [],
upgrade: false,
url: '',
method: null,
statusCode: 401,
statusMessage: 'Unauthorized',
client: [Object],
_consuming: true,
_dumped: false,
req: [Circular],
text: '{"error": "invalid_client"}',
read: [Function],
body: [Object] } },
links: {},
text: '{"error": "invalid_client"}',
body: { error: 'invalid_client' },
files: undefined,
buffered: true,
headers:
{ date: 'Sun, 22 Jan 2017 10:45:42 GMT',
'content-type': 'application/json',
'transfer-encoding': 'chunked',
connection: 'close',
server: 'nginx',
'x-frame-options': 'SAMEORIGIN',
vary: 'Cookie',
'cache-control': 'no-store',
pragma: 'no-cache',
'set-cookie': [ 'csrftoken=WYIpctfKYGTGCyRMF3lyLDnyV0GTcTarJ9qmFLJ4vxI1B5SjyJPUzpeCB4MYGNfT; expires=Sun, 21-Jan-2018 10:45:42 GMT; Max-Age=31449600; Path=/; secure' ],
'x-xss-protection': '1; mode=block',
'x-content-type-options': 'nosniff',
'strict-transport-security': 'max-age=31536000',
p3p: 'CP="Instamojo does not have a P3P policy"' },
header:
{ date: 'Sun, 22 Jan 2017 10:45:42 GMT',
'content-type': 'application/json',
'transfer-encoding': 'chunked',
connection: 'close',
server: 'nginx',
'x-frame-options': 'SAMEORIGIN',
vary: 'Cookie',
'cache-control': 'no-store',
pragma: 'no-cache',
header:
{ 'content-type': 'text/html',
'last-modified': 'Wed, 09 Sep 2015 10:51:38 GMT',
'accept-ranges': 'bytes',
etag: '"fa62cb80edead01:0"',
server: 'Microsoft-IIS/7.5',
'x-powered-by': 'ASP.NET',
date: 'Sun, 22 Jan 2017 15:34:49 GMT',
connection: 'close',
'content-length': '689' },
statusCode: 200,
status: 200,
statusType: 2,
info: false,
ok: true,
redirect: false,
clientError: false,
serverError: false,
error: false,
accepted: false,
noContent: false,
badRequest: false,
unauthorized: false,
notAcceptable: false,
forbidden: false,
notFound: false,
type: 'text/html',
setEncoding: [Function: bound ],
redirects: [] }

Ritej Bisaria
Jan 22, 2017

Do you have Node JS specific snippets which I could maybe have a look at.

Markdown is allowed