Instagram OAuthException : you must provide a client_id

匿名 (未验证) 提交于 2019-12-03 01:05:01

问题:

I've been trying to use server side authentication of instagram API in my web app. I've followed the steps provided at Intagram's API Page, but I keep getting the error you must provide a client_id. The code is written in node/express.js. Here is my code.
PS: Please don't suggest me to use istagram-node API.

index.js

var bodyParser = require('body-parser'); var express = require('express'); var app = express(); var https = require('https'); var session = require('express-session');  app.use(bodyParser.json()); app.use(bodyParser.urlencoded({extended:true})); app.use(express.static(__dirname+'/public')); app.use(session({   secret: process.env.SECRET,   resave: true,   saveUninitialized: false }))  app.set('port',process.env.PORT);  app.get('/',function(req,res){   res.render('index') });  app.get('/home',function(req,res){   req.session.code = req.query.code;   var data = JSON.stringify({       client_id: process.env.CLIENT_ID,       client_secret: process.env.CLIENT_SECRET,       grant_type: "authorizaton_code",       redirect_uri: "...",       code: req.session.code   })   var options = {     headers:{       'content-type': 'application/x-www-form-urlencoded'     },       hostname: 'api.instagram.com',       path: '/oauth/access_token',       method:'POST',       port:443     }     var request = https.request(options,function(resp){       resp.on('data',function(chunk){         req.session.data = chunk.toString();       })     })     request.write(data);     request.end();     setTimeout(function(){       res.json(req.session.data)     },5000); })  app.listen(app.get('port'),function(){   console.log("All eyes at "+process.env.PORT); }); 

回答1:

Have been scratching my head all morning. it turns out you have to post it as form data.

add

   const FormData = require('form-data'); 

at the top then your code is as follows:

  var data = new FormData();   data.append('client_id',config.instagramAuth.client_id)   data.append('client_secret',config.instagramAuth.client_secret)   data.append('redirect_uri',redirect_uri)   data.append('grant_type','authorization_code')   data.append('code',code) 

and your fetch:

  fetch(url, {         method: 'POST',         body: data //JSON.stringify(postData)       }) 

Hope it helps



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!