Last active
August 29, 2015 14:01
-
-
Save picsoung/3cdb87fd462bb0a754f9 to your computer and use it in GitHub Desktop.
OAuth twitter middleware for APItools
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return function (request, next_middleware) | |
-- change to your own Twitter keys | |
api_key = "MY_TWITTER_API_KEY" | |
api_secret = "MY_TWITTER_API_SECRET" | |
-- concatenate by ':' | |
str = api_key .. ':' .. api_secret | |
console.log(str) | |
-- generate base64 string | |
auth_header = "Basic ".. base64.encode(str) | |
console.log(auth_header) | |
-- headers to pass to /oauth2/ endpoint | |
headers_val ={} | |
headers_val["Authorization"]=auth_header | |
headers_val["Content-Type"]="application/x-www-form-urlencoded;charset=UTF-8" | |
-- call to get access_token | |
local response = http.urlencoded.post('https://api.twitter.com/oauth2/token',{grant_type = "client_credentials"},{headers = { authorization=auth_header}}) | |
local resp = json.decode(response.body) | |
request.headers.Authorization = "Bearer ".. resp.access_token | |
console.log("access_token",resp.access_token) | |
-- pass the access_token to auth call | |
request.headers.Authorization = "Bearer ".. resp.access_token | |
return next_middleware() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment