Created
July 3, 2012 13:23
-
-
Save mamantoha/3039674 to your computer and use it in GitHub Desktop.
Vkontakte Ruby Net::HTTP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/http' | |
email = '[email protected]' | |
pass = 'secret' | |
client_id = '1234567' | |
scope = 'friends,audio' | |
redirect_uri = 'http://oauth.vk.com/blank.html' | |
display = 'wap' | |
response_type = 'code' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
url = "http://oauth.vk.com/oauth/authorize?client_id=#{client_id}&scope=#{scope}&redirect_uri=#{redirect_uri}&display=#{display}&response_type=#{response_type}&_hash=0" | |
uri = URI(url) | |
request = Net::HTTP::Get.new(uri.request_uri) | |
response = Net::HTTP.start(uri.host, uri.port){ |http| http.request(request) } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form method="POST" action="https://login.vk.com/?act=login&soft=1&utf8=1"> | |
<input type="hidden" name="q" value="1"> | |
<input type="hidden" name="from_host" value="oauth.vk.com"> | |
<input type="hidden" name="from_protocol" value="http"> | |
<input type="hidden" name="ip_h" value="e1b62a2b2aaaaecec4" /> | |
<input type="hidden" name="to" value="aHR0cDovL29hdXRoLnZrLmNvbS9vYXV0aC9hdXRob3JpemU/Y2xpZW50X2lkPTE5MTUxMDgmcmVkaXJlY3RfdXJpPWJsYW5rLmh0bWwmcmVzcG9uc2VfdHlwZT1jb2RlJnNjb3BlPTImc3RhdGU9JmRpc3BsYXk9d2Fw"> | |
<span class="label">Телефон або e-mail:</span><br /> | |
<input type="text" name="email"><br /> | |
<span class="label">Пароль:</span><br /> | |
<input type="password" name="pass"> | |
<div style="padding: 8px 0px 5px 0px"> | |
<div class="button_yes"> | |
<input type="submit" value="Увійти" /> | |
</div> | |
<a class="button_no" href="https://oauth.vk.com/grant_access?hash=c67933e7624f531360&client_id=1915108&settings=2&redirect_uri=blank.html&cancel=1&state=&token_type=0"> | |
<div> | |
Скасувати | |
</div> | |
</a> | |
</form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
params = { | |
:q => response.body[/name="q" value="(.+?)"/, 1], | |
:from_host => response.body[/name="from_host" value="(.+?)"/, 1], | |
:from_protocol => response.body[/name="from_protocol" value="(.+?)"/, 1], | |
:ip_h => response.body[/name="ip_h" value="(.+?)"/, 1], | |
:to => response.body[/name="to" value="(.+?)"/, 1] | |
} | |
url = response.body[/<form method="POST" action="(.+?)"/, 1] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if response.code == '302' | |
url = response['location'] | |
code = /code=(.+)$/.match(url)[1] | |
elsif response.code == '200' | |
url = /<form method="POST" action="(.+?)"/.match(response.body)[1] | |
uri = URI(url) | |
request = Net::HTTP::Post.new(uri.request_uri, header) | |
response = Net::HTTP.start(uri.host, uri.port, | |
:use_ssl => uri.scheme == 'https') {|http| | |
http.request(request) | |
} | |
if response.code == '302' | |
url = response['location'] | |
code = /code=(.+)$/.match(url)[1] | |
end | |
end | |
puts 'code=' + code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment