-
Notifications
You must be signed in to change notification settings - Fork 18
/
create_access_token
executable file
·92 lines (84 loc) · 2.23 KB
/
create_access_token
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env ruby
#
# Prints out an access token to STDOUT. You can pass the following
# arguments:
#
# - key
# - secret
#
# Or set them by the env vars:
#
# - RDIO_KEY
# - RDIO_SECRET
#
# This is used to serialize an access_token for tests and so other
# people can run the tests easily if they'd like to.
#
require 'rubygems'
require 'oauth'
class PrintAccessToken
SITE = 'http://api.rdio.com'
def main(argv)
key = ENV['RDIO_KEY']
secret = ENV['RDIO_SECRET']
if not argv.empty?
key = argv.shift
end
if not argv.empty?
secret = argv.shift
end
ok = true
if not key
STDERR.puts 'Must set ENV[\'RDIO_KEY\'] or pass rdio key as arg 1'
ok = false
end
if not secret
STDERR.puts 'Must set ENV[\'RDIO_SECRET\'] or pass rdio secret as arg 2'
ok = false
end
return if not ok
token = access_token key,secret
f = File.open '.rdio_access_token','w'
data = Marshal.dump token,f
end
private
def get_pin(url)
#
# Try to open using launchy, then if this doesn't work use
# system open
#
begin
require 'rubygems'
require 'launchy'
Launchy.open url
rescue Exception => e
system 'open',url
end
#
# Get the pin from the user
#
oauth_verifier = nil
while not oauth_verifier or oauth_verifier == ''
print 'Enter the 4-digit PIN> '
STDOUT.flush
oauth_verifier = gets.strip
end
return oauth_verifier
end
def access_token(key,secret)
consumer =
OAuth::Consumer.new(key,secret,
{:site => SITE,
:request_token_path => "/oauth/request_token",
:authorize_path => "/oauth/authorize",
:access_token_path => "/oauth/access_token",
:http_method => :post})
consumer.http.read_timeout = 600
request_token = consumer.get_request_token({:oauth_callback => 'oob'})
url = 'https://www.rdio.com/oauth/authorize?oauth_token=' +
request_token.token.to_s
oauth_verifier = get_pin url
return request_token.get_access_token({:oauth_verifier => oauth_verifier})
end
end
PrintAccessToken.new.main ARGV