Skip to content

Instantly share code, notes, and snippets.

@mamantoha
Created August 6, 2011 18:53
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/1129631 to your computer and use it in GitHub Desktop.
Save mamantoha/1129631 to your computer and use it in GitHub Desktop.
require 'oauth2'
require 'mechanize'
@client = OAuth2::Client.new(
'client_id',
'client_secret',
:site => 'https://api.vk.com/',
:token_url => '/oauth/token',
:authorize_url => '/oauth/authorize'
)
auth_url = @client.auth_code.authorize_url(
:redirect_uri => 'http://api.vk.com/blank.html',
:scope => '',
:display => 'wap'
)
agent = Mechanize.new{|a| a.user_agent_alias = 'Linux Konqueror'}
login_page = agent.get(auth_url)
login_form = login_page.forms.first
login_form.email = 'email'
login_form.pass = 'pass'
verify_page = login_form.submit
if verify_page.uri.path == '/oauth/authorize'
if /m=4/.match(verify_page.uri.query)
raise "Вказано невірний логін або пароль."
elsif /s=1/.match(verify_page.uri.query)
grant_access_page = verify_page.forms.first.submit
end
else
grant_access_page = verify_page
end
code = /code=(?<code>.*)/.match(grant_access_page.uri.fragment)['code']
@access_token = @client.auth_code.get_token(code)
@access_token.options[:param_name] = 'access_token'
@access_token.options[:mode] = :query
@access_token.get('/method/getProfiles', :params => {:uids => '1', :fields => 'city,country'}).parsed
require 'oauth2'
require 'mechanize'
@client = OAuth2::Client.new(
'client_id',
'client_secret',
:site => 'https://api.vk.com/',
:token_url => '/oauth/token',
:authorize_url => '/oauth/authorize'
)
auth_url = @client.auth_code.authorize_url(
:redirect_uri => 'http://api.vk.com/blank.html',
:scope => '',
:display => 'wap'
)
agent = Mechanize.new{|a| a.user_agent_alias = 'Linux Konqueror'}
login_page = agent.get(auth_url)
login_form = login_page.forms.first
login_form.email = 'email'
login_form.pass = 'pass'
verify_page = login_form.submit
if verify_page.uri.path == '/oauth/authorize'
if /m=4/.match(verify_page.uri.query)
raise "Вказано невірний логін або пароль."
elsif /s=1/.match(verify_page.uri.query)
grant_access_page = verify_page.forms.first.submit
end
else
grant_access_page = verify_page
end
code = /code=(?<code>.*)/.match(grant_access_page.uri.fragment)['code']
@access_token = @client.auth_code.get_token(code)
@access_token.options[:param_name] = 'access_token'
@access_token.options[:mode] = :query
@access_token.get('/method/getProfiles', :params => {:uids => '1', :fields => 'city,country'}).parsed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment