使用Instagram API从Javascript发布图像到Instagram



Instagram Graph API:
https://developers.facebook.com/docs/instagram-api/

内容发布:
https://developers.facebook.com/docs/instagram-api/guides/content-publishing/

我的代码JavascriptGoogle App Script:

function InstagramPost() {
const id = '123456789';
const image = 'https://www.w3schools.com/images/w3schools_green.jpg';
const text = 'Hello%20World';
const access_token = 'TESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTEST';
const container = 'https://graph.facebook.com/v11.0/' + id + '/media?image_url=' + image + '&caption=' + text + '&access_token=' + access_token;
const response = UrlFetchApp.fetch(container);
const creation = response.getContentText();
Logger.log(creation);
}

在我的容器的日志返回到Post通过Instagram API请求如下:

{
"data": [
{
"id": "11111111111111111"
},
{
"id": "22222222222222222"
},
{
"id": "33333333333333333"
}
],
"paging": {
"cursors": {
"before": "QWOURQWNGEWRONHWENYWPETGNWQPGNPGNWEPGNWEPGNWEPNGWPENGPWEG",
"after": "WIWEPGNEPBNWE´GNÉ´BNWE´BNWÉBWNEB´WENBNWEBWEBEWBWE"
},
"next": "https://graph.facebook.com/v11.0/11111111111111111/media?access_token=PQWNFWPQINPWNBQPWNBQPWNBPQWNVQWPNVPQWVNPQWPVNQPWNVQPWVNQPWNVPQWNVQPWNVQPWVNQASASLGÇAJKSGLJAAÇSNAÇKNSVÇLKNASBÇANSBÇAS"
}
}

为了最终调用post image,有必要使用creation_id=:

const sendinstagram = 'https://graph.facebook.com/v11.0/' + id + '/media_publish?creation_id=' + creation + '&access_token=' + access_token;
UrlFetchApp.fetch(sendinstagram);

如果从容器返回的是顺序的几个id,我如何知道为调用定义哪一个?

注意:我不能循环尝试每个id,因为Instagram每天有25个电话和帖子的限制,所以如果我这样做,我的电话最终只是试图发布一张图片。

首先,我们通过点击端点创建IG Container。

POST https://graph.facebook.com/v11.0/{ig-user-id}/media?image_url={image-url}&caption={caption}&access_token={access-token}

一旦你有了IG容器ID,然后我们再次发出POST请求来发布图像。

POST https://graph.facebook.com/v11.0/{ig-user-id}/media_publish?creation_id={creation-id}&access_token={access-token}

我认为你必须在容器中包含版本和sendinstagram

我找到了发布图像的正确方法:

var formData = {
'image_url': image,
'caption': text,
'access_token': access_token
};
var options = {
'method' : 'post',
'payload' : formData
};
const container = 'https://graph.facebook.com/v11.0/' + instagram_business_account + '/media';
const response = UrlFetchApp.fetch(container, options);
const creation = response.getContentText();
var data = JSON.parse(creation);
var creationId = data.id
var formDataPublish = {
'creation_id': creationId,
'access_token': access_token
};
var optionsPublish = {
'method' : 'post',
'payload' : formDataPublish
};
const sendinstagram = 'https://graph.facebook.com/v11.0/' + instagram_business_account + '/media_publish';

UrlFetchApp.fetch(sendinstagram, optionsPublish);

相关内容

  • 没有找到相关文章

最新更新