-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.rb
81 lines (67 loc) · 1.77 KB
/
hello.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# --
# http://192.168.0.102:4567/
# http://192.168.0.105:4567/
require 'sinatra'
require 'instagram'
require 'sinatra/base'
# Configuracoes Instagram
# scope permissoes necessarias
# MyApp classe principal da aplicacao
class MyApp < Sinatra::Base
# coding: utf-8
set :bind, '0.0.0.0'
enable :sessions
CALLBACK_URL = 'http://192.168.0.105:4567/oauth/callback'.freeze
Instagram.configure do |config|
config.client_id = 'c7dceb3cadb242fc86c8acf894e444e2'
config.client_secret = '99a27b9b9f3346d9b408f462e1d5ca7c'
config.scope = 'basic public_content'
# For secured endpoints only
# config.client_ips = '<Comma separated list of IPs>'
end
get '/' do
erb :index
end
get '/oauth/connect' do
redirect Instagram.authorize_url(redirect_uri: CALLBACK_URL)
end
get '/oauth/callback' do
if params[:code]
par_code = params[:code]
response = Instagram.get_access_token(par_code, redirect_uri: CALLBACK_URL)
session[:access_token] = response.access_token
redirect '/home'
elsif params[:error]
@titulo = 'Instagram API'
@msg_erro = params[:error_description]
erb :erro_login
else
redirect Instagram.authorize_url(redirect_uri: CALLBACK_URL)
end
end
get '/home' do
if session[:access_token]
client = Instagram.client(access_token: session[:access_token])
user = client.user
@titulo = 'Instagram API'
@subtitulo = 'Bem vindo ' + user.full_name
erb :home
else
redirect '/'
end
end
get '/user_recent_media' do
if session[:access_token]
@titulo = 'Instagram API'
erb :fotos
else
redirect '/'
end
end
get '/logout' do
@titulo = 'Instagram API'
session[:access_token] = nil
redirect '/'
end
run!
end