Skip to content

Instantly share code, notes, and snippets.

@mamantoha
Created August 6, 2011 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mamantoha/1129694 to your computer and use it in GitHub Desktop.
Save mamantoha/1129694 to your computer and use it in GitHub Desktop.
# -*- encoding: utf-8 -*-
require 'sinatra'
require 'oauth2'
require 'json'
require 'haml'
CLIENT_SECRET = '***'
CLIENT_ID = '***'
def client
OAuth2::Client.new(CLIENT_ID, CLIENT_SECRET,
:site => 'https://api.vk.com/',
:token_url => '/oauth/token',
:authorize_url => '/oauth/authorize'
)
end
get '/auth/vk' do
url = client.auth_code.authorize_url(
:redirect_uri => redirect_uri,
:scope => ''
)
puts "Redirect to URL: #{url.inspect}"
redirect url
end
get '/auth/vk/callback' do
access_token = client.auth_code.get_token(params[:code], :redirect_uri => redirect_uri)
access_token.options[:param_name] = 'access_token'
access_token.options[:mode] = :query
resp = access_token.get("/method/getProfiles", :params => {:uid=> access_token.params['user_id']}
).parsed['response'].first
haml '%h1 Привіт #{resp["first_name"]} #{resp["last_name"]}!',
:locals => { :resp => resp }
end
def redirect_uri
puts "Request URL: " + request.url
uri = URI.parse(request.url)
uri.path = '/auth/vk/callback'
uri.query = nil
uri.to_s
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment