OAuth1 2Legged

Hello again,

Anyone already already tried OAuth1 with 2Legged Option? So no token necessary?
Any Code?

Thank You
Roman [import]uid: 140000 topic_id: 30297 reply_id: 330297[/import]

I just wanted to say that I’ve successfully modified the oAuth module from the example for my needs to fullfill a 2legged oAuth request. the difference here was to send the params of the curl via the header - by doing a real GET request.

hope this will help someone.

oAuth.lua

-- Project: Twitter sample app  
--  
-- File name: oAuth.lua  
--  
-- Author: Corona Labs  
--  
-- Abstract: Demonstrates how to connect to Twitter using Oauth Authenication.  
--  
-- Sample code is MIT licensed, see http://www.coronalabs.com/links/code/license  
-- Copyright (C) 2010 Corona Labs Inc. All Rights Reserved.  
-----------------------------------------------------------------------------------------  
  
module(...,package.seeall)  
   
local http = require("socket.http")  
local ltn12 = require("ltn12")  
local crypto = require("crypto")  
local mime = require("mime")  
  
-----------------------------------------------------------------------------------------  
-- MAKE REQUEST  
-----------------------------------------------------------------------------------------  
--  
function makeRequest(url, body, consumer\_key, token, consumer\_secret, token\_secret)  
  
  
 local post\_data =   
 {  
 oauth\_consumer\_key = consumer\_key,  
 oauth\_nonce = get\_nonce(),  
 oauth\_signature\_method = "HMAC-SHA1",  
 oauth\_token = token,  
 oauth\_timestamp = get\_timestamp(),  
 oauth\_version = '1.0',  
 oauth\_token\_secret = token\_secret  
 }  
  
 local url\_param = {}  
  
 for i=1, #body do  
 post\_data[body[i].key] = body[i].value  
 table.insert(url\_param,   
 {  
 key = body[i].key,  
 val = body[i].value  
 })  
 end  
  
 local url\_param\_pairs = {}  
  
 for \_, rec in pairs(url\_param) do  
 table.insert(url\_param\_pairs, rec.key .. "=" .. rec.val)  
 end  
  
 local url\_param\_fin = "?" .. table.concat(url\_param\_pairs, "&")  
 local url\_fin = url .. url\_param\_fin  
  
 local oasign\_data = oAuthSign(url, post\_data, consumer\_secret)  
  
 local result = rawGetRequest(url\_fin, consumer\_key, consumer\_secret, post\_data.oauth\_nonce, oasign\_data.signature)  
  
 return result  
end  
  
-----------------------------------------------------------------------------------------  
-- OAUTH SIGN  
-----------------------------------------------------------------------------------------  
--  
function oAuthSign(url, args, consumer\_secret)  
   
 local token\_secret = args.oauth\_token\_secret or ""  
   
 args.oauth\_token\_secret = nil  
   
 local keys\_and\_values = {}  
   
 for key, val in pairs(args) do  
 table.insert(keys\_and\_values,   
 {  
 key = encode\_parameter(key),  
 val = encode\_parameter(val)  
 })  
 end  
   
 table.sort(keys\_and\_values, function(a,b)  
 if a.key \< b.key then  
 return true  
 elseif a.key \> b.key then  
 return false  
 else  
 return a.val \< b.val  
 end  
 end)  
  
 local key\_value\_pairs = {}  
   
 for \_, rec in pairs(keys\_and\_values) do  
 table.insert(key\_value\_pairs, rec.key .. "=" .. rec.val)  
 end  
  
 local query\_string\_except\_signature = table.concat(key\_value\_pairs, "&")  
  
 local sign\_base\_string = "GET" .. '&' .. encode\_parameter(url) .. '&'  
 .. encode\_parameter(query\_string\_except\_signature)  
   
 local key = encode\_parameter(consumer\_secret) .. '&' .. encode\_parameter(token\_secret)  
 local hmac\_binary = sha1(sign\_base\_string, key, true)  
   
 local hmac\_b64 = mime.b64(hmac\_binary)  
 local signature = encode\_parameter(hmac\_b64)  
 local query\_string = query\_string\_except\_signature .. '&oauth\_signature=' .. signature  
  
 return{  
 query\_string = query\_string,  
 signature = signature  
 }   
  
end  
  
-----------------------------------------------------------------------------------------  
-- ENCODE PARAMETER (URL\_Encode)  
-- Replaces unsafe URL characters with %hh (two hex characters)  
-----------------------------------------------------------------------------------------  
--  
function encode\_parameter(str)  
 return str:gsub('[^-%.\_~a-zA-Z0-9]',function(c)  
 return string.format("%%%02x",c:byte()):upper()  
 end)  
end  
  
-----------------------------------------------------------------------------------------  
-- SHA 1  
-----------------------------------------------------------------------------------------  
--  
function sha1(str,key,binary)  
 binary = binary or false  
 return crypto.hmac(crypto.sha1,str,key,binary)  
end  
  
-----------------------------------------------------------------------------------------  
-- GET NONCE  
-----------------------------------------------------------------------------------------  
--  
function get\_nonce()  
 --return mime.b64(crypto.hmac(crypto.sha1,tostring(math.random()) .. "random" .. tostring(os.time()),"keyyyy"))  
 return tostring(math.random(111111111,999999999))  
end  
  
-----------------------------------------------------------------------------------------  
-- GET TIMESTAMP  
-----------------------------------------------------------------------------------------  
--  
function get\_timestamp()  
 return tostring(os.time() + 1)  
end  
  
-----------------------------------------------------------------------------------------  
-- RAW GET REQUEST  
-----------------------------------------------------------------------------------------  
--  
  
function rawGetRequest(url, iconsumer\_key, iconsumer\_secret, inonce, isignature)  
  
 headers = {}  
 headers["Content-Type"] = "application/x-www-form-urlencoded"  
 headers["Authorization"] = "OAuth realm = \"\", oauth\_version=\"1.0\", oauth\_signature\_method=\"HMAC-SHA1\", oauth\_consumer\_key=\"" .. iconsumer\_key .. "\", oauth\_nonce=\"" .. encode\_parameter(inonce) .. "\", oauth\_timestamp=\"" .. get\_timestamp() .. "\", oauth\_token=\"\", oauth\_signature=\"" .. isignature .. "\""  
  
 local params = {}  
 params.headers = headers  
  
 local function networkListener(event)  
  
 if(event.isError) then  
 print ( "NETWORK ERROR")  
 else  
 print ( event.response )  
 end  
 end  
  
 local tests = network.request( url, "GET", networkListener, params)  
  
end  
  

example.lua

[code]
require(“oAuth”)

module(…, package.seeall)

consumer_key = “” – key string goes here
consumer_secret = “” – secret string goes here

local access_token = “”
local access_token_secret = “”

local params = {}
params[1] =
{
key = ‘url’,
value = “google.de
}
params[2] =
{
key = ‘countrycode’,
value = ‘DE’
}

request_response = oAuth.makeRequest(“url goes here”, params, consumer_key, access_token, consumer_secret, access_token_secret)

print("req resp ",request_response)

[/code] [import]uid: 140000 topic_id: 30297 reply_id: 122867[/import]

now there is just one problem: how can I return the response-string out of the listener in the function raw get request? I wanna pass it back…

return even.response doesn’t work

does anyone have an idea? [import]uid: 140000 topic_id: 30297 reply_id: 122874[/import]

I just wanted to say that I’ve successfully modified the oAuth module from the example for my needs to fullfill a 2legged oAuth request. the difference here was to send the params of the curl via the header - by doing a real GET request.

hope this will help someone.

oAuth.lua

-- Project: Twitter sample app  
--  
-- File name: oAuth.lua  
--  
-- Author: Corona Labs  
--  
-- Abstract: Demonstrates how to connect to Twitter using Oauth Authenication.  
--  
-- Sample code is MIT licensed, see http://www.coronalabs.com/links/code/license  
-- Copyright (C) 2010 Corona Labs Inc. All Rights Reserved.  
-----------------------------------------------------------------------------------------  
  
module(...,package.seeall)  
   
local http = require("socket.http")  
local ltn12 = require("ltn12")  
local crypto = require("crypto")  
local mime = require("mime")  
  
-----------------------------------------------------------------------------------------  
-- MAKE REQUEST  
-----------------------------------------------------------------------------------------  
--  
function makeRequest(url, body, consumer\_key, token, consumer\_secret, token\_secret)  
  
  
 local post\_data =   
 {  
 oauth\_consumer\_key = consumer\_key,  
 oauth\_nonce = get\_nonce(),  
 oauth\_signature\_method = "HMAC-SHA1",  
 oauth\_token = token,  
 oauth\_timestamp = get\_timestamp(),  
 oauth\_version = '1.0',  
 oauth\_token\_secret = token\_secret  
 }  
  
 local url\_param = {}  
  
 for i=1, #body do  
 post\_data[body[i].key] = body[i].value  
 table.insert(url\_param,   
 {  
 key = body[i].key,  
 val = body[i].value  
 })  
 end  
  
 local url\_param\_pairs = {}  
  
 for \_, rec in pairs(url\_param) do  
 table.insert(url\_param\_pairs, rec.key .. "=" .. rec.val)  
 end  
  
 local url\_param\_fin = "?" .. table.concat(url\_param\_pairs, "&")  
 local url\_fin = url .. url\_param\_fin  
  
 local oasign\_data = oAuthSign(url, post\_data, consumer\_secret)  
  
 local result = rawGetRequest(url\_fin, consumer\_key, consumer\_secret, post\_data.oauth\_nonce, oasign\_data.signature)  
  
 return result  
end  
  
-----------------------------------------------------------------------------------------  
-- OAUTH SIGN  
-----------------------------------------------------------------------------------------  
--  
function oAuthSign(url, args, consumer\_secret)  
   
 local token\_secret = args.oauth\_token\_secret or ""  
   
 args.oauth\_token\_secret = nil  
   
 local keys\_and\_values = {}  
   
 for key, val in pairs(args) do  
 table.insert(keys\_and\_values,   
 {  
 key = encode\_parameter(key),  
 val = encode\_parameter(val)  
 })  
 end  
   
 table.sort(keys\_and\_values, function(a,b)  
 if a.key \< b.key then  
 return true  
 elseif a.key \> b.key then  
 return false  
 else  
 return a.val \< b.val  
 end  
 end)  
  
 local key\_value\_pairs = {}  
   
 for \_, rec in pairs(keys\_and\_values) do  
 table.insert(key\_value\_pairs, rec.key .. "=" .. rec.val)  
 end  
  
 local query\_string\_except\_signature = table.concat(key\_value\_pairs, "&")  
  
 local sign\_base\_string = "GET" .. '&' .. encode\_parameter(url) .. '&'  
 .. encode\_parameter(query\_string\_except\_signature)  
   
 local key = encode\_parameter(consumer\_secret) .. '&' .. encode\_parameter(token\_secret)  
 local hmac\_binary = sha1(sign\_base\_string, key, true)  
   
 local hmac\_b64 = mime.b64(hmac\_binary)  
 local signature = encode\_parameter(hmac\_b64)  
 local query\_string = query\_string\_except\_signature .. '&oauth\_signature=' .. signature  
  
 return{  
 query\_string = query\_string,  
 signature = signature  
 }   
  
end  
  
-----------------------------------------------------------------------------------------  
-- ENCODE PARAMETER (URL\_Encode)  
-- Replaces unsafe URL characters with %hh (two hex characters)  
-----------------------------------------------------------------------------------------  
--  
function encode\_parameter(str)  
 return str:gsub('[^-%.\_~a-zA-Z0-9]',function(c)  
 return string.format("%%%02x",c:byte()):upper()  
 end)  
end  
  
-----------------------------------------------------------------------------------------  
-- SHA 1  
-----------------------------------------------------------------------------------------  
--  
function sha1(str,key,binary)  
 binary = binary or false  
 return crypto.hmac(crypto.sha1,str,key,binary)  
end  
  
-----------------------------------------------------------------------------------------  
-- GET NONCE  
-----------------------------------------------------------------------------------------  
--  
function get\_nonce()  
 --return mime.b64(crypto.hmac(crypto.sha1,tostring(math.random()) .. "random" .. tostring(os.time()),"keyyyy"))  
 return tostring(math.random(111111111,999999999))  
end  
  
-----------------------------------------------------------------------------------------  
-- GET TIMESTAMP  
-----------------------------------------------------------------------------------------  
--  
function get\_timestamp()  
 return tostring(os.time() + 1)  
end  
  
-----------------------------------------------------------------------------------------  
-- RAW GET REQUEST  
-----------------------------------------------------------------------------------------  
--  
  
function rawGetRequest(url, iconsumer\_key, iconsumer\_secret, inonce, isignature)  
  
 headers = {}  
 headers["Content-Type"] = "application/x-www-form-urlencoded"  
 headers["Authorization"] = "OAuth realm = \"\", oauth\_version=\"1.0\", oauth\_signature\_method=\"HMAC-SHA1\", oauth\_consumer\_key=\"" .. iconsumer\_key .. "\", oauth\_nonce=\"" .. encode\_parameter(inonce) .. "\", oauth\_timestamp=\"" .. get\_timestamp() .. "\", oauth\_token=\"\", oauth\_signature=\"" .. isignature .. "\""  
  
 local params = {}  
 params.headers = headers  
  
 local function networkListener(event)  
  
 if(event.isError) then  
 print ( "NETWORK ERROR")  
 else  
 print ( event.response )  
 end  
 end  
  
 local tests = network.request( url, "GET", networkListener, params)  
  
end  
  

example.lua

[code]
require(“oAuth”)

module(…, package.seeall)

consumer_key = “” – key string goes here
consumer_secret = “” – secret string goes here

local access_token = “”
local access_token_secret = “”

local params = {}
params[1] =
{
key = ‘url’,
value = “google.de
}
params[2] =
{
key = ‘countrycode’,
value = ‘DE’
}

request_response = oAuth.makeRequest(“url goes here”, params, consumer_key, access_token, consumer_secret, access_token_secret)

print("req resp ",request_response)

[/code] [import]uid: 140000 topic_id: 30297 reply_id: 122867[/import]

now there is just one problem: how can I return the response-string out of the listener in the function raw get request? I wanna pass it back…

return even.response doesn’t work

does anyone have an idea? [import]uid: 140000 topic_id: 30297 reply_id: 122874[/import]