Trying to post image to Instagram from a Javascript using Instagram API

750 Views Asked by At

I am doing this in Google App Scripts. Seems pretty straightforward, so I first setup Meta Developer info, and created a Instagram Test User and created a valid long-lived token for that account to use.

var userID = "139**********890";
// a fresh Instagram API Long Live Access Token
var accessToken = "IGQVJWRzBzWGp2d1hIUThZ*****************************************************************************************************************cWVFclczbDBmTEx0U1B2RAZDZD";

function insertPost3() {
    var formData = {
    'image_url': "https://drive.google.com/uc?id=10QubQQV218reR2qSQ-9qA0QmrOoYoCIL&export=download",
    'caption': "here is sample text",
    'access_token': accessToken
  };
  var options = {
    'method' : 'post',
    'payload' : formData,
    'muteHttpExceptions' : true
  };
  const container = 'https://graph.facebook.com/' + userID + '/media';

  Logger.log(container);

  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': accessToken
  };
  var optionsPublish = {
    'method' : 'post',
    'payload' : formDataPublish
  };
  const sendinstagram = 'https://graph.facebook.com/' + userID + '/media_publish';
  
  UrlFetchApp.fetch(sendinstagram, optionsPublish);
}

And just getting a generic error back, even with muteHttpExceptions. Not sure how to proceed.

Exception: Request failed for https://graph.facebook.com returned code 400. Truncated server response: {"error":{"message":"Invalid OAuth access token.","type":"OAuthException","code":190,"fbtrace_id":"AhuSzful3sLsd7qKziJH6Oi"}} (use muteHttpExceptions option to examine full response)

Any ideas, sample code, or resources to read would be great.

This is one of the guides I've been using to do this: https://developers.facebook.com/docs/instagram-api/guides/content-publishing/

Thanks for any help!!

phi

1

There are 1 best solutions below

0
Ratan Nahn On

get instagram id(from GET account details).

get Long Live user access token.

get Long Live page access token.

use page access token to upload the image. Please note, this LLPAT needs access token together in the URL. it will return media id

use media id to create a insta post with caption

Related Questions in INSTAGRAM-GRAPH-API