Skip to content

Instantly share code, notes, and snippets.

@vexus2
Created October 8, 2012 14:04
Show Gist options
  • Save vexus2/3852700 to your computer and use it in GitHub Desktop.
Save vexus2/3852700 to your computer and use it in GitHub Desktop.
[Ruby]ニコニコ動画APIを叩くとき用。事前にログインさせCookie値を取得する。
# ログインしてクッキー抽出
def login(mail, pass)
host = 'secure.nicovideo.jp'
path = '/secure/login?site=niconico'
body = "mail=#{mail}&password=#{pass}"
https = Net::HTTP.new(host, 443)
https.use_ssl = true
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
response = https.start { |https|
https.post(path, body)
}
cookie = ''
response['set-cookie'].split('; ').each do |st|
if idx=st.index('user_session_')
cookie = "user_session=#{st[idx..-1]}"
break
end
end
return cookie
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment