Search code examples
ruby-on-railscurlinstagraminstagram-api

Is it possible to authenticate on Instagram API by curl or in the application backend?


After Instagram updated their API on June 1, I couldn't get the access token without logging in to Instagram account.

Is it possible to skip the website authentication and do it by curl or in my application backend?


Solution

  • I ended up by using mechanize gem for Ruby

    I made self method in InstagramPage class:

    def self.instagram_client
      return @instagram_client if @instagram_client.present? &&
             @instagram_client.access_token.present?
    
      # Logging in to Instagram
      url = Instagram.authorize_url(redirect_uri: ENV['INSTAGRAM_REDIRECT_URL'], 
                                    response_type: 'token')
      agent = Mechanize.new
      agent.get(url)
      agent.page.forms[0]['username'] = ENV['INSTAGRAM_USERNAME']
      agent.page.forms[0]['password'] = ENV['INSTAGRAM_PASSWORD']
      agent.page.forms[0].submit
    
      # Retrieving access_token from url and setting it to Instagram client
      access_token = agent.page.uri.to_s.split('=')[-1]
      @instagram_client = Instagram.client(access_token: access_token)
    end
    

    Now I able to call client by running InstagramPage.instagram_client