Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# https://getpocket.com/developer/docs/authentication
require 'net/http'
require 'uri'
def usage
puts <<USAGE
Usage:
$ ruby #{$0} <consumer_key>
USAGE
end
consumer_key = ARGV.first
unless consumer_key
usage
exit!
end
uri = URI.parse('https://getpocket.com/v3/oauth/request')
params = {
'consumer_key' => consumer_key,
'redirect_uri' => 'http://example.com/',
}
res = Net::HTTP.post_form(uri, params)
if res.code != '200'
puts res.body
exit!
end
query = Hash[URI.decode_www_form(res.body)]
code = query['code']
url = "https://getpocket.com/auth/authorize?request_token=#{code}&redirect_uri=http://example.com/"
system("open #{url}")
puts "Press any key if you have authenticated: "
STDIN.gets
uri = URI.parse('https://getpocket.com/v3/oauth/authorize')
params = {
'consumer_key' => consumer_key,
'code' => code,
}
res = Net::HTTP.post_form(uri, params)
pp Hash[URI.decode_www_form(res.body)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment