Skip to content

Instantly share code, notes, and snippets.

@j4rs
Last active August 5, 2020 20:16
Show Gist options
  • Save j4rs/8723088 to your computer and use it in GitHub Desktop.
Save j4rs/8723088 to your computer and use it in GitHub Desktop.
Integrating Instagram(OAuth) using RubyMotion, Promotion, AFMotion and some SugarCube
class Instagram < PM::WebScreen
title "Instagram"
CLIENT_ID = 'your client id'
REDIRECT_URI = 'http://your.domain.com/auth/instagram/callback'
FAILURE_URL = 'http://your.domain.com/auth/failure'
AUTH_URI = "https://instagram.com/oauth/authorize/?client_id=#{CLIENT_ID}&redirect_uri=#{REDIRECT_URI}&response_type=token"
class << self
def api
@api ||= AFMotion::Client.build_shared('https://api.instagram.com/v1/') do
header "Accept", "application/json"
response_serializer :json
end
end
def get_user_info(access_token, &block)
api.get('users/self', access_token: access_token) do |result|
if result.success?
block.call(result.object['data']) if block
elsif result.failure?
block.call(nil) if block
end
end
end
end
def on_init
super
@external_links = true
@activity = UIActivityIndicatorView.alloc.initWithActivityIndicatorStyle(UIActivityIndicatorViewStyleGray)
self.view.addSubview(@activity)
@activity.center = self.view.center
@activity.hidesWhenStopped = true
end
def content
AUTH_URI.nsurl
end
def load_started
@activity.startAnimating
end
def load_finished
@activity.stopAnimating
end
def load_failed(error)
@activity.stopAnimating
end
def on_request(request, in_type)
url = request.URL.absoluteString
# is our redirect url?
if url.start_with? REDIRECT_URI
case url
when /access_token/
# url constains the access_token? (url second part), returning access token
close screen: :instagram, access_token: url.split('#access_token=').last
when /error_reason/
# known user error
reason = url =~ /user_denied/ ? 'User denied access' : 'Authentication failed'
close screen: :instagram, error: reason
else
# unknown error
close screen: :instagram, error: 'Unknown error'
end
false
elsif url.start_with? FAILURE_URL
# TODO: Yeah, we should do something...
false
end
true # let the webview load this url
end
end
class NetworksTableScreen < PM::TableScreen
...
def link_instagram
if User.get.connected_to? :instagram
...
else
open Instagram.new(nav_bar: true)
end
end
def on_return(args)
case args[:screen]
when :instagram
unless args[:error]
start_indicator("Linking your Instagram...")
BwuitInstagram.get_user_info(args[:access_token]) do |user|
stop_indicator
if user
#TODO: Do somenthing with your user basic data
update_table_data
else
puts "Ups, something went wrong"
#TODO: Error
end
end
else
UIAlertView.alert "Error linking Instagram: #{args[:error]}"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment