Skip to content

Instantly share code, notes, and snippets.

@rebolek
Created April 7, 2017 07:08
Show Gist options
  • Save rebolek/a2bd8920432930e44bdab35b8218860f to your computer and use it in GitHub Desktop.
Save rebolek/a2bd8920432930e44bdab35b8218860f to your computer and use it in GitHub Desktop.
Red []
note: {OAuth oauth_nonce="K7ny27JTpKVsTgdyLdDfmQQWVLERj2zAK5BslRsqyw", oauth_callback="http%3A%2F%2Fmyapp.com%3A3005%2Ftwitter%2Fprocess_callback", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1300228849", oauth_consumer_key="OqEqJeafRSF11jBMStrZz", oauth_signature="Pc%2BMLdv028fxCErFyi8KXFM%2BddU%3D", oauth_version="1.0"}
; do %json.red
user: #()
twitter: https://api.twitter.com/
verification-page: %oauth/authorize?oauth_token=
request-page: %oauth/request_token
access-page: %oauth/access_token
twvf-page: https://api.twitter.com/oauth/authorize?oauth_token=
; --- tools
cut-tail: function [
series
/part
length
] [
unless part [length: 1]
head remove/part skip tail series negate length length
]
get-unix-timestamp: function [
"Read UNIX timestamp from Internet"
] [
date: none
page: read http://www.unixtimestamp.com/
parse page [
thru "The Current Unix Timestamp"
thru <h3 class="text-danger">
copy date to <small>
]
to integer! date
]
url-encode: function [
text [any-string!]
] [
value: none
chars: charset ["!'*,-.~_" #"0" - #"9" #"A" - #"Z" #"a" - #"z"]
rejoin head insert parse text [
collect [
some [
keep some chars
| space keep #"+"
| set value skip keep (head insert enbase/base form value 16 %"%")
]
]
] ""
]
; ---
TODO: map!
oauth-proto: [
oauth_callback: "oob"
oauth_consumer_key: (consumer-key)
oauth_nonce: none
oauth_signature_method: "HMAC-SHA1"
oauth_signature: none
oauth_timestamp: none
oauth_token: none
oauth_token_secret: none
oauth_verifier: none
oauth_version: 1.0
oauth_callback_confirmed: none
]
; TODO: make more universal
make-request: function [
data
/only "Ignore NONE values"
/with
pattern
] [
if any [map? data object? data] [data: body-of data]
print mold data
unless with [pattern: [key {="} value {", }]]
output: collect/into [
foreach [key value] data [
if any [not only all [only value]] [
keep rejoin bind pattern 'key
]
]
] make string! 1000
cut-tail/part output either with [length? form last pattern] [2]
]
make-nonce: function [] [
nonce: enbase/base checksum form random/secure 2147483647 'SHA512 64
remove-each char nonce [find "+/=" char]
copy/part nonce 32
]
; TODO: move somewhere
method: 'post
lookup: https://api.twitter.com/oauth/request_token
make-signature: function [
method
link
params
] [
key: rejoin [consumer-secret "&" any [user/secret ""]]
params: make-request/only/with params [key #"=" value #"&"]
url-encode enbase/base checksum/with set 'temp rejoin [
uppercase form method "&" url-encode form link "&"
url-encode replace/all params "+" "%20"
] 'SHA1 key 64
]
; TODO: Rename!!! Decode based on type (form, json, ...)
decode-reply: function [
text
type
] [
switch type [
"application/json" [text]
"application/x-www-form-urlencoded" [
text: make map! split text charset "=&"
]
"text/html" [
text: make map! split text charset "=&"
]
]
text
]
send: function [
link
method
/with
args
/auth
auth-data
] [
data: #()
if with [extend data args]
if auth [
; TODO: Add simple authentization
oauth/oauth_nonce: make-nonce
oauth/oauth_timestamp: get-unix-timestamp
oauth/oauth_signature: make-signature method twitter/:request-page oauth
extend data compose [Authorization: (form reduce ["OAuth" make-request/only auth-data])]
]
print "^/===READ==="
reply: write/info probe link probe reduce [method body-of data]
print "<<<READ>>>^/"
type: first split reply/2/Content-Type #";"
probe make map! compose [
code: (reply/1)
headers: (reply/2)
raw: (reply/3)
data: (decode-reply reply/3 type)
]
]
login: func [] [
method: 'POST
; FIXME: I need to change this to MAP!, but then it doesn't work(?)
oauth: context compose oauth-proto
reply: send/with/auth twitter/:request-page method #(Content-Type: "application/x-www-form-urlencoded") oauth
; TODO: error handling
if equal? reply/code 200 [
foreach key keys-of reply/data [
set in oauth to word! key reply/data/:key
]
browse head append copy twitter/:verification-page oauth/oauth_token
verifier: ask "verifier: " ; TODO: check for input validity
oauth/oauth_verifier: verifier
reply: send/with/auth twitter/:access-page method #(Content-Type: "application/x-www-form-urlencoded") oauth
print ["===SET USER" mold reply/data]
foreach key keys-of reply/data [
user/(to word! key): reply/data/:key
print [key mold user]
]
print mold user
probe reply
]
]
timeline: func [] [
; result: send/with 'get %1.1/statuses/user_timeline.json options
timeline-page: %1.1/statuses/user_timeline.json
oauth/oauth_token_secret: none
oauth/oauth_callback: none
oauth/oauth_callback_confirmed: none
result: send/with/auth twitter/:timeline-page 'GET user oauth
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment