forked from horgh/twitter-tcl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_oauth.tcl
executable file
·139 lines (122 loc) · 3.25 KB
/
test_oauth.tcl
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env tclsh
#
# this script provides a way to set up and test OAuth authentication.
#
set auto_path [linsert $auto_path 0 [pwd]]
package require oauth
package require twitlib
# output usage information to stdout.
proc ::usage {} {
global argv0
puts "Usage: $argv0 <mode> \[arguments\]"
puts ""
puts "Mode is one of:"
puts ""
puts " get_pin <consumer key> <consumer secret>"
puts ""
puts " To perform authentication step 1 - get URL to retrieve PIN."
puts " You find the consumer key and secret on the Twitter OAuth"
puts " clients page."
puts ""
puts " get_token <consumer key> <consumer secret> <token> <token secret> <pin>"
puts ""
puts " to get an access token"
puts ""
puts " get_updates <consumer key> <consumer secret> <token> <token secret>"
puts ""
puts " to test usage of the tokens"
puts ""
}
# perform authentication step 1 - request authorisation URL to get
# a PIN.
proc ::get_pin {consumer_key consumer_secret} {
set d [::oauth::get_request_token $consumer_key $consumer_secret]
foreach key [dict keys $d] {
set val [dict get $d $key]
puts "$key = $val"
}
puts "You should now authorise the access by going to the authentication"
puts " URL, and then use it with this script in 'get_token' mode."
return 1
}
# perform authentication step 2 - use the PIN from step 1 to authenticate.
proc ::get_token {consumer_key consumer_secret token token_secret pin} {
set d [::oauth::get_access_token $consumer_key $consumer_secret \
$token $token_secret $pin]
foreach key [dict keys $d] {
set val [dict get $d $key]
puts "$key = $val"
}
puts "You should now have sufficient information to perform"
puts "authenticated requests. Use the above data in the 'get_updates'"
puts "mode of this script to test this."
return 1
}
# use authentication information from step 2 to retrieve recent updates.
proc ::get_updates {consumer_key consumer_secret token token_secret} {
set ::twitlib::oauth_consumer_key $consumer_key
set ::twitlib::oauth_consumer_secret $consumer_secret
set ::twitlib::oauth_token $token
set ::twitlib::oauth_token_secret $token_secret
set updates [::twitlib::get_unseen_updates]
foreach status $updates {
foreach key [dict keys $status] {
set val [dict get $status $key]
puts "$key = $val"
}
}
set count [llength $updates]
puts "Retrieved $count status update(s)."
return 1
}
# program entry.
# we will exit.
proc ::main {} {
global argc
global argv
if {[expr $argc == 0]} {
::usage
exit 1
}
set mode [lindex $argv 0]
if {$mode eq "get_pin"} {
if {[llength $argv] != 3} {
::usage
exit 1
}
lassign $argv mode consumer_key consumer_secret
if {![::get_pin $consumer_key $consumer_secret]} {
exit 1
}
exit 0
}
if {$mode eq "get_token"} {
if {[llength $argv] != 6} {
::usage
exit 1
}
lassign $argv mode consumer_key consumer_secret \
token token_secret pin
if {![::get_token $consumer_key $consumer_secret $token $token_secret \
$pin]} {
exit 1
}
exit 0
}
if {$mode eq "get_updates"} {
if {[llength $argv] != 5} {
::usage
exit 1
}
lassign $argv mode consumer_key consumer_secret \
token token_secret
if {![::get_updates $consumer_key $consumer_secret $token \
$token_secret]} {
exit 1
}
exit 0
}
::usage
exit 1
}
::main