1

I'm building a simple node app using request and Instagram api and I'm trying to make a call for the instagram access token in the final step of the oauth process. For some reason Instagam's api is responding with a 400 saying my client_id is not specified when it is clearly specified in my request body. Here is a link to instagram's oauth process: https://www.instagram.com/developer/authentication/

HERE IS MY REQUEST CODE:

var options = {
  url: "https://api.instagram.com/oauth/access_token",
  method: 'POST',
  oauth: {
    "client_id": process.env.CLIENT_ID,
    "client_secret": process.env.CLIENT_SECRET
  },
  json: {
    "client_id": process.env.CLIENT_ID,
    "client_secret": process.env.CLIENT_SECRET,
    "grant_type" : "authorization_code",
    "code" : response.req.query.code,
    "redirect_uri" : 'http://127.0.0.1:5000/redirect'
  }
};

request(options, function ( error, response ) {
  if(error) { console.log(error); }
  else { console.log(JSON.stringify(response)); }
});

HERE IS THE RESPONSE:

{"statusCode":400,"body":{"code":400,"error_type":"OAuthException","error_messag
e":"You must provide a client_id"},"headers":{"content-language":"en","expires":
"Sat, 01 Jan 2000 00:00:00 GMT","vary":"Cookie, Accept-Language","pragma":"no-ca
che","cache-control":"private, no-cache, no-store, must-revalidate","date":"Wed,
 27 Apr 2016 05:37:03 GMT","content-type":"application/json","set-cookie":["csrf
 token=456dc5df05f5b0aad286723004e395fe; expires=Wed, 26-Apr-2017 05:37:03 GMT; M
ax-Age=31449600; Path=/","mid=VyBP_wAEAAGhT9YCRDGVBM6tksC7; expires=Tue, 22-Apr-
2036 05:37:03 GMT; Max-Age=630720000; Path=/"],"connection":"close","content-len
gth":"94"},"request":{"uri":{"protocol":"https:","slashes":true,"auth":null,"hos
t":"api.instagram.com","port":443,"hostname":"api.instagram.com","hash":null,"se
arch":null,"query":null,"pathname":"/oauth/access_token","path":"/oauth/access_t
oken","href":"https://api.instagram.com/oauth/access_token"},"method":"POST","he
aders":{"accept":"application/json","content-   type":"application/json","content-l
ength":223,"Authorization":"OAuth oauth_client_id=\"[THE ACUAL RESPONSE       DISPLAYS ID HERE]\",oauth_client_secret=\"[THE ACTUAL RESPONSE DISPLYS SECRET     HERE]\",oauth_nonce=\"cc
173578667148d1bdf341926b9e2cd0\",oauth_signature_method=\"HMAC-SHA1\",oauth_time
stamp=\"1461735413\",oauth_version=\"1.0\",oauth_signature=\"zeQxrYVNSsveztczszW
FiY3jGz8%3D\""}}}
nivo1000
  • 199
  • 1
  • 1
  • 8

2 Answers2

0

I think the environment variable process.env.CLIENT_ID doesnt have value. Try using hard coded strings instead of process.env.CLIENT_ID and process.env.CLIENT_SECRET

Bashir
  • 91
  • 2
  • No the environment variables log as strings of my id and secret so they are definitely not undefined. Could it be the way I'm specifying them in the request body? – nivo1000 Apr 28 '16 at 18:43
0

I was struggling with this also for days with other apis. But this works quitly for all api oAuth 2 requests.

request.post({url:'https://api.instagram.com/oauth/access_token', form: {
    redirect_uri: 'http://localhost/dashboard',
    client_id: 'xxxxxxxxxxx',
    client_secret: 'xxxxxxxxxxxxx',
    code: 'xxxxxxxxxxxxxxx',
    grant_type: 'authorization_code'
}
}, function(err,httpResponse,body){ /* ... */ 
console.log('err: ' + err)
console.log('body: ' + body)
})

I use as redirect url also when register the app http://localhost/dashboard
Don´t use only http://localhost because if you use XAMPP then you will get always redirected to http://localhost/dashboard and this means your can´t get the code from
https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=code

t33n
  • 270
  • 1
  • 3
  • 17