Created
November 4, 2012 23:08
-
-
Save voidfiles/4014269 to your computer and use it in GitHub Desktop.
Example integrating App.net and Webscript.io
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
function table.copy(t) | |
local t2 = {} | |
for k,v in pairs(t) do | |
t2[k] = v | |
end | |
return t2 | |
end | |
function return_html(html) | |
return html, {["Content-Type"]="text/html"} | |
end | |
local CLIENT_ID = '<CLIENT_ID FROM APP.NET>' | |
local CLIENT_SECRECT = '<CLIENT_SECRECT FROM APP.NET>' | |
local REDIRECT_URI = '<URL of your script on Webscript.io>' | |
base_auth_url = 'https://alpha.app.net/oauth/' | |
base_auth_params = { | |
client_id = CLIENT_ID, | |
redirect_uri = REDIRECT_URI, | |
} | |
if request.query.code then | |
access_token_params = table.copy(base_auth_params) | |
access_token_params['client_secret'] = CLIENT_SECRECT | |
access_token_params['grant_type'] = 'authorization_code' | |
access_token_params['code'] = request.query.code | |
access_token_url = base_auth_url .. 'access_token' | |
response = http.request { | |
url = access_token_url, | |
method = 'POST', | |
data = access_token_params, | |
} | |
if response.statuscode == 200 then | |
return json.parse(response.content) | |
end | |
return return_html("There was an error during the authorization flow") | |
else | |
authorization_params = table.copy(base_auth_params) | |
authorization_params['response_type'] = 'code' | |
authorization_params['scope'] = 'basic' | |
authorization_url = base_auth_url .. 'authorize?' .. http.qsencode(authorization_params) | |
return return_html("<a href='" .. authorization_url .. "'>authorize</a>") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment