Instagram API访问令牌请求JavaScript

发布于 2025-02-03 14:55:59 字数 878 浏览 6 评论 0 原文

您好,我试图使用 Instagram API 获得连接令牌。我首先在Postman上进行了测试,这就是我所做的: 我使用此链接将请求帖子发布到Instagram API:

https://api.instagram.com/oauth/access_token?client_id=clientId=clientId&client_secret = secret = clientsecret=clientsecret&am /& code = thecode

api给我一个错误:缺少必需字段client_id

但是当我将内容类型设置为 xwww-form-urlencoded 一切都在邮递员身上效果很好。

因此,我尝试使用节点模块请求在JavaScript中做同样的事情。我试图用模块做与邮递员一样的事情,但它行不通...这是我的代码:

request(`https://api.instagram.com/oauth/access_token?client_id=clientid&client_secret=clientsecret&grant_type=authorization_code&redirect_uri=https://mysite/&code=` + code, {
        method: 'POST',
        headers: {"Content-Type": "x-www-form-urlencoded"}
    }, (error, response, body) => {
        console.log('body:', body)
    })

Hello I have tried to use the instagram api to get a connection token. I first tested it on postman and this is what I did:
I used this link to make a request post to the instagram api:

https://api.instagram.com/oauth/access_token?client_id=clientid&client_secret=clientsecret&grant_type=authorization_code&redirect_uri=https://mysite/&code=thecode

The api gives me an error: Missing required field client_id

But when I set the content type to x-www-form-urlencoded everything works fine on postman.

So I tried to do the same thing in javascript with the node module request. I tried to do the same thing as on postman with the module but it does not work... Here is my code:

request(`https://api.instagram.com/oauth/access_token?client_id=clientid&client_secret=clientsecret&grant_type=authorization_code&redirect_uri=https://mysite/&code=` + code, {
        method: 'POST',
        headers: {"Content-Type": "x-www-form-urlencoded"}
    }, (error, response, body) => {
        console.log('body:', body)
    })

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静若繁花 2025-02-10 14:55:59

根据MDN,内容类型应为 application/x-www-form-urlenCoded

更新:
您应该读取节点文档: https://nodejs.dev/nodejs.dev/learn/making -http-requests-with-nodejs

获取方法:

const https = require('https');

const options = {
  hostname: 'api.instagram.com',
  path: '/oauth/access_token?client_id=clientid&client_secret=clientsecret&grant_type=authorization_code&redirect_uri=https://mysite/&code=thecode',
  method: 'GET',
  headers: {
        "Content-Type": "application/x-www-form-urlencoded", 
        "Accept": "Accept-Encoding"
  }
};

const req = https.request(options, (res) => {
  // ...
});

帖子方法:

var headers = {
  "Content-Type": "application/x-www-form-urlencoded", 
    "Accept": "Accept-Encoding"
};

var options = {
  url: 'https://api.instagram.com/oauth/access_token',
  method: 'POST',
  headers: headers
};

var form = {
  grant_type:'urn:ietf:params:oauth:grant-type:jwt-bearer',
  client_id: 'id',
  client_secret: 'secret'
  redirect_uri : 'https://mysite/&code=thecode'
};

var request = https.request(options, function(response) {
  // do stuff
});

request.write(querystring.stringify(form));
request.end();

As per MDN, the content type should be application/x-www-form-urlencoded
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST

Update:
You should read the node doc : https://nodejs.dev/learn/making-http-requests-with-nodejs

Get method:

const https = require('https');

const options = {
  hostname: 'api.instagram.com',
  path: '/oauth/access_token?client_id=clientid&client_secret=clientsecret&grant_type=authorization_code&redirect_uri=https://mysite/&code=thecode',
  method: 'GET',
  headers: {
        "Content-Type": "application/x-www-form-urlencoded", 
        "Accept": "Accept-Encoding"
  }
};

const req = https.request(options, (res) => {
  // ...
});

Post method:

var headers = {
  "Content-Type": "application/x-www-form-urlencoded", 
    "Accept": "Accept-Encoding"
};

var options = {
  url: 'https://api.instagram.com/oauth/access_token',
  method: 'POST',
  headers: headers
};

var form = {
  grant_type:'urn:ietf:params:oauth:grant-type:jwt-bearer',
  client_id: 'id',
  client_secret: 'secret'
  redirect_uri : 'https://mysite/&code=thecode'
};

var request = https.request(options, function(response) {
  // do stuff
});

request.write(querystring.stringify(form));
request.end();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文