You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ruby on Rails 通过Instagram API发布图片问题求助

How to Post Images to Instagram via Ruby on Rails

Hey there! I’ve run into this exact issue before, so let me walk you through what’s going on and how to fix it.

First, The Key Context: Instagram API Changes

The old Instagram REST API (v1) that you’re using to fetch recent tagged media doesn’t support posting content anymore. Instagram phased out that functionality years ago, and now all content publishing is handled through the Instagram Graph API—but this only works for Business or Creator accounts (personal accounts can’t use it for posting via API).

The instagram gem you’re using is mostly built around the old REST API, which is why you can’t find a method for posting images. It just doesn’t cover that functionality anymore.

Step-by-Step Solution to Post Images

Here’s how to get this working with Rails:

1. Convert Your Instagram Account to a Business/Creator Account

First, you need to switch your personal account to either a Business or Creator account (this is free and done directly in the Instagram app):

  • Go to your profile → Settings → Account → Switch to Professional Account
  • Follow the prompts to set up a Business Account (you’ll need to link it to a Facebook Page, which is required for the Graph API)

2. Use the Facebook Graph API (Instagram Graph API is part of this)

Instead of the instagram gem, you’ll want to use a gem that works with the Facebook Graph API, like koala (a popular Ruby gem for Facebook APIs). Or you can make direct HTTP requests if you prefer.

First, add koala to your Gemfile:

gem 'koala'

Run bundle install to install it.

3. Get the Right Access Tokens & Permissions

You’ll need:

  • A long-lived Instagram Business Account access token (valid for 60 days, can be refreshed)
  • The following permissions granted: instagram_basic, pages_manage_posts, pages_show_list

You can generate the initial token via the Facebook Graph API Explorer, then exchange it for a long-lived token.

4. Code Example: Post an Image

Here’s how to upload and post an image using koala in Rails:

First, upload the image to Instagram’s media library (you need to do this before publishing):

require 'koala'

# Initialize the Koala client with your long-lived access token
graph = Koala::Facebook::API.new('YOUR_LONG_LIVED_ACCESS_TOKEN')

# Upload the image (replace with your image path or URL)
image_path = Rails.root.join('app', 'assets', 'images', 'your-image.jpg')
upload_response = graph.put_object('me/media', 'image_url', {
  image_url: 'https://your-domain.com/path-to-image.jpg', # Or use a local file with image_file: File.open(image_path)
  caption: 'Your awesome caption here!'
})

# The upload response will include an `id` for the media item
media_id = upload_response['id']

# Now publish the uploaded media
publish_response = graph.post_object(media_id, 'publish', {
  access_token: 'YOUR_LONG_LIVED_ACCESS_TOKEN'
})

puts "Image posted successfully! Post ID: #{publish_response['id']}"

Important Notes

  • Make sure your app is approved by Facebook for the required permissions if you’re going to use this in production (otherwise it’ll only work for test users/accounts linked to your app).
  • If you’re using a local image, use the image_file parameter instead of image_url (pass a File object).
  • You can also schedule posts or add location data by including additional parameters in the upload step.

Why the Old instagram Gem Won’t Work

The instagram gem hasn’t been updated to support the Instagram Graph API’s publishing endpoints. It’s great for fetching data (like the tagged media you’re already doing), but for posting, you need to switch to the Graph API approach above.

内容的提问来源于stack exchange,提问作者Dapeng114

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:16:58