Skip to content

Instantly share code, notes, and snippets.

@picsoung
Last active August 29, 2015 14:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save picsoung/3cdb87fd462bb0a754f9 to your computer and use it in GitHub Desktop.
Save picsoung/3cdb87fd462bb0a754f9 to your computer and use it in GitHub Desktop.
OAuth twitter middleware for APItools
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