class Insta::Client

Attributes

client_id[RW]
client_secret[RW]
redirect_uri[RW]
scope[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/insta.rb, line 10
def initialize(options = {})
  @redirect_uri  = options[:redirect_uri]
  @client_id     = options[:client_id]
  @client_secret = options[:client_secret]
  @scope         = options[:scope] || 'user_profile,user_media'
end

Public Instance Methods

access_token(code = '') click to toggle source
# File lib/insta.rb, line 21
def access_token(code = '')
  uri = URI.parse("https://api.instagram.com/oauth/access_token")
  request = Net::HTTP::Post.new(uri)
  request.set_form_data(
    "client_id" => client_id,
    "client_secret" => client_secret,
    "code" => code,
    "grant_type" => "authorization_code",
    "redirect_uri" => redirect_uri,
  )

  req_options = {
    use_ssl: uri.scheme == "https",
  }

  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(request)
  end

  response = JSON.parse(response.body)
  OpenStruct.new(response)
end
auth_url() click to toggle source
# File lib/insta.rb, line 17
def auth_url
  "https://api.instagram.com/oauth/authorize?client_id=#{client_id}&redirect_uri=#{redirect_uri}&scope=#{scope}&response_type=code"
end
long_lived_access_token(access_token) click to toggle source
# File lib/insta.rb, line 44
def long_lived_access_token(access_token)
  url = "https://graph.instagram.com/access_token?grant_type=ig_exchange_token&client_secret=#{client_secret}&access_token=#{access_token}"
  uri = URI.parse(url)
  request = Net::HTTP::Get.new(uri)
  req_options = {
    use_ssl: uri.scheme == "https",
  }

  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(request)
  end
  response = JSON.parse(response.body)
  OpenStruct.new(response)
end