'Instagram embed code from url (Rails)

In my application, I would like to allow user introduce a url of instagram, and then automatically retrieve the embed code of it.

I found this reference: https://www.instagram.com/developer/embedding/#oembed

When I try the given example (https://api.instagram.com/oembed?url=http://instagr.am/p/fA9uwTtkSN) in my Chrome browser, I get the json with the code I am looking for.

However, if I try this from the rails console:

Net::HTTP.get_response(URI("https://api.instagram.com/oembed?url=http://instagr.am/p/fA9uwTtkSN"))

I get this:

#<Net::HTTPMovedPermanently 301 MOVED PERMANENTLY readbody=true> 

I saw that instagram have a new API, but I don't want to make user authenticate from instagram.

Is there a way to do it?



Solution 1:[1]

You can use oembed gem by soulim. nice and clean way to get embed code

In case of instagram it goes like

include following line in your gem file

gem 'oembed'

next

bundle install

create a helper class

class InstaApi
  include Oembed::Client

  def endpoint_uri
    'http://api.instagram.com/oembed'
  end
end

Now you can use

instClient = InstaApi.new
info = instClient.fetch('http://instagr.am/p/BUG/')

to Get your embed code

embed_code = info["html"]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1