Posting to Twitter (argh!)

Hi all,

I’m having problems posting to Twitter. I use a slightly adapted version (I copy/pasted all of the request URLs from the Twitter page) of the code that Peach posted at techority:
http://corona.techority.com/2011/05/26/post-to-twitter-from-your-application/

  
io.output():setvbuf('no')  
oAuth = require ("oAuth") -- using the oAuth.lua that came with SDK 868  
  
local bodyTxt = "This is my text to Tweet" -- I just want to tweet out something simple  
local appURL = "http://google.com" -- I tried with my own website, no difference (but is identical to Twitter callback URL)  
local consumer\_key = "consumer\_key" -- key and secret like given by Twitter  
local consumer\_secret = "consumer\_secret"  
  
local access\_token  
local access\_token\_secret  
local user\_id  
local screen\_name  
  
local twitter\_request = (oAuth.getRequestToken(consumer\_key, appURL, "https://api.twitter.com/oauth/request\_token", consumer\_secret))  
print("twitter\_request\_token: ", twitter\_request.token) -- this is always nil for some reason!?  
local twitter\_request\_token = twitter\_request.token  
local twitter\_request\_token\_secret = twitter\_request.token\_secret  
  
local function listener(event)  
  
 local remain\_open = true  
 local url = event.url  
  
 if url:find("oauth\_token") then  
 url = url:sub(url:find("?") + 1, url:len())  
  
 local authorize\_response = responseToTable(url, {"=", "&"})  
  
 if (authorize\_response.oauth\_verifier == nil)  
 then  
 print("verifier nil. keeping webview up by returning true...")  
 return true  
 else  
 print("verifier received! about to get access token")  
 end  
  
 remain\_open = false  
  
 local access\_response = responseToTable(oAuth.getAccessToken(authorize\_response.oauth\_token, authorize\_response.oauth\_verifier, twitter\_request\_token\_secret, consumer\_key, consumer\_secret, "https://api.twitter.com/oauth/access\_token"), {"=", "&"})  
  
 print("after get access token")  
 access\_token = access\_response.oauth\_token  
 access\_token\_secret = access\_response.oauth\_token\_secret  
 user\_id = access\_response.user\_id  
 screen\_name = access\_response.screen\_name  
  
  
 -- API CALL:  
 ------------------------------  
 --change the message posted  
 local params = {}  
   
 params[1] =  
 {  
 key = 'status',  
 value = bodyTxt  
 }  
  
 request\_response = oAuth.makeRequest("http://api.twitter.com/1/statuses/update.json", params, consumer\_key, access\_token, consumer\_secret, access\_token\_secret, "POST")  
 print("req resp ",request\_response)  
 else  
 print("auth token not found")  
 end  
  
 return remain\_open  
end  
  
function tweetit (event)  
 -- this one breaks in the simulator with a runtime error because twitter\_request\_token = nil and remains nil throughout, even though I call oAuth..  
  
 native.showWebPopup(10, 10, 300, 360, "https://api.twitter.com/oauth/authorize?oauth\_token=" .. twitter\_request\_token, {urlRequest = listener})   
  
end  
function responseToTable(str, delimeters)  
local obj = {}  
  
while str:find(delimeters[1]) ~= nil do  
 if #delimeters \> 1 then  
 local key\_index = 1  
 local val\_index = str:find(delimeters[1])  
 local key = str:sub(key\_index, val\_index - 1)  
  
 --print("key = "..key)  
 str = str:sub((val\_index + delimeters[1]:len()))  
  
 local end\_index  
 local value  
  
 if str:find(delimeters[2]) == nil then  
 end\_index = str:len()  
 value = str  
 else  
 end\_index = str:find(delimeters[2])  
 value = str:sub(1, (end\_index - 1))  
 str = str:sub((end\_index + delimeters[2]:len()), str:len())  
 end  
 --print("value = "..value)  
 obj[key] = value  
 --print(key .. ":" .. value)  
 else  
  
 local val\_index = str:find(delimeters[1])  
 str = str:sub((val\_index + delimeters[1]:len()))  
  
 local end\_index  
 local value  
  
 if str:find(delimeters[1]) == nil then  
 end\_index = str:len()  
 value = str  
 else  
 end\_index = str:find(delimeters[1])  
 value = str:sub(1, (end\_index - 1))  
 str = str:sub(end\_index, str:len())  
 end  
 obj[#obj + 1] = value  
 print(value)  
 end  
end  
return obj  
end  
  
local twitterButton = display.newImageRect ("twitter.png", 100, 100, 10, 10)  
twitterButton.x = 150  
twitterButton.y = 280  
twitterButton:addEventListener("tap", tweetit)  

I’m probably missing something very trivial but I am lost at the moment. The popup does not show in CoronaSim or XCode because of that nil value.
Do I need to change something in the of the app on Twitter apart from the already given items? [import]uid: 106658 topic_id: 30216 reply_id: 330216[/import]

Very silly question - you did put in your key/secret on lines 7 & 8? [import]uid: 62706 topic_id: 30216 reply_id: 120999[/import]

yes I did, only removed them for the post on the forum :slight_smile:

oh and, before anyone asks, I have the app permission on Twitter set to Read & Write

so, I’m wondering what the… I am missing here.

fwiw, building for iOS. [import]uid: 106658 topic_id: 30216 reply_id: 121000[/import]

Sorry, I cant help much more, I haven’t tried using twitter integration yet, plenty of people have though, so there should be some help around :slight_smile:
[import]uid: 62706 topic_id: 30216 reply_id: 121002[/import]

Okay, I’m digging in a bit deeper in the code, adding print statements all over the oAuth.lua code.
seems getRequestToken returns “Failed to validate oauth signature and token”

hmmz… [import]uid: 106658 topic_id: 30216 reply_id: 121004[/import]

My code this work but when i log in to post it redirect to my callback URL and post didn’t appear.
–any one help me to know what is wrong
– main.lua


require(“oAuth”)
consumer_key = “myconsumer_key”
consumer_secret = "consumer_secret "
local access_token
local access_token_secret
local user_id
local screen_name

–your web address below can be anything from what i can make out as long as it is the same as the callback url set in twitter settings
–twitter sends the webaddress with the token back to your app and the code strips out the token to use to authorise it
–doing it this way, means the web popup closes itself - if it doesn’t it means there is some kind of problem with the code
–I found that out the hard way!!

local twitter_request = (oAuth.getRequestToken(consumer_key, “my url”, “http://twitter.com/oauth/request_token”, consumer_secret))
local twitter_request_token = twitter_request.token
local twitter_request_token_secret = twitter_request.token_secret

local function listener(event)
–print(“listener”)
local remain_open = true
local url = event.url

if url:find(“oauth_token”) then

url = url:sub(url:find("?") + 1, url:len())

local authorize_response = responseToTable(url, {"=", “&”})
remain_open = false

local access_response = responseToTable(oAuth.getAccessToken(authorize_response.oauth_token, authorize_response.oauth_verifier, twitter_request_token_secret, consumer_key, consumer_secret, “https://api.twitter.com/oauth/access_token”), {"=", “&”})

access_token = access_response.oauth_token
access_token_secret = access_response.oauth_token_secret
user_id = access_response.user_id
screen_name = access_response.screen_name
– API CALL:

–change the message posted
local params = {}
params[1] =
{
key = ‘status’,
value = "Follow us "
}

request_response = oAuth.makeRequest(“https://api.twitter.com/1/statuses/update.json”, params, consumer_key, access_token, consumer_secret, access_token_secret, “POST”)
print("req resp ",request_response)
end

return remain_open
end

–this is your webpopup, change position/size as you wish
function tweetit (event)
–system.openURL(10, 20, 300, 450, “http://api.twitter.com/oauth/authorize?oauth_token=” … twitter_request_token, {urlRequest = listener})
native.showWebPopup(10, 10, 460, 360, “http://api.twitter.com/oauth/authorize?oauth_token=” … twitter_request_token, {urlRequest = listener})
end

–I use this for testing on a mac, but lack of textfield makes it difficult for my app, i may as well use device
–you could use a random message generator for testing purposes so as to send a unique message each time,
–would let you see messages in terminal then

–system.openURL(10, 20, 300, 450, “http://api.twitter.com/oauth/authorize?oauth_token=” … twitter_request_token, {urlRequest = listener})

–this is the bit that strips the token from the web address returned
–/////////////////////////////////////////////////////////////////////////////////////
–// RESPONSE TO TABLE
–/////////////////////////////////////////////////////////////////////////////////////
function responseToTable(str, delimeters)
local obj = {}

while str:find(delimeters[1]) ~= nil do
if #delimeters > 1 then
local key_index = 1
local val_index = str:find(delimeters[1])
local key = str:sub(key_index, val_index - 1)

str = str:sub((val_index + delimeters[1]:len()))

local end_index
local value

if str:find(delimeters[2]) == nil then
end_index = str:len()
value = str
else
end_index = str:find(delimeters[2])
value = str:sub(1, (end_index - 1))
str = str:sub((end_index + delimeters[2]:len()), str:len())
end
obj[key] = value
print(key … “:” … value)
else

local val_index = str:find(delimeters[1])
str = str:sub((val_index + delimeters[1]:len()))

local end_index
local value

if str:find(delimeters[1]) == nil then
end_index = str:len()
value = str
else
end_index = str:find(delimeters[1])
value = str:sub(1, (end_index - 1))
str = str:sub(end_index, str:len())
end
obj[#obj + 1] = value
print(value)
end
end
return obj
end

local twitterButton = display.newImage (“twitter.png”)
twitterButton.x = 150
twitterButton.y = 280

twitterButton:addEventListener(“tap”, tweetit) [import]uid: 160777 topic_id: 30216 reply_id: 121627[/import]

Very silly question - you did put in your key/secret on lines 7 & 8? [import]uid: 62706 topic_id: 30216 reply_id: 120999[/import]

yes I did, only removed them for the post on the forum :slight_smile:

oh and, before anyone asks, I have the app permission on Twitter set to Read & Write

so, I’m wondering what the… I am missing here.

fwiw, building for iOS. [import]uid: 106658 topic_id: 30216 reply_id: 121000[/import]

Sorry, I cant help much more, I haven’t tried using twitter integration yet, plenty of people have though, so there should be some help around :slight_smile:
[import]uid: 62706 topic_id: 30216 reply_id: 121002[/import]

Okay, I’m digging in a bit deeper in the code, adding print statements all over the oAuth.lua code.
seems getRequestToken returns “Failed to validate oauth signature and token”

hmmz… [import]uid: 106658 topic_id: 30216 reply_id: 121004[/import]

My code this work but when i log in to post it redirect to my callback URL and post didn’t appear.
–any one help me to know what is wrong
– main.lua


require(“oAuth”)
consumer_key = “myconsumer_key”
consumer_secret = "consumer_secret "
local access_token
local access_token_secret
local user_id
local screen_name

–your web address below can be anything from what i can make out as long as it is the same as the callback url set in twitter settings
–twitter sends the webaddress with the token back to your app and the code strips out the token to use to authorise it
–doing it this way, means the web popup closes itself - if it doesn’t it means there is some kind of problem with the code
–I found that out the hard way!!

local twitter_request = (oAuth.getRequestToken(consumer_key, “my url”, “http://twitter.com/oauth/request_token”, consumer_secret))
local twitter_request_token = twitter_request.token
local twitter_request_token_secret = twitter_request.token_secret

local function listener(event)
–print(“listener”)
local remain_open = true
local url = event.url

if url:find(“oauth_token”) then

url = url:sub(url:find("?") + 1, url:len())

local authorize_response = responseToTable(url, {"=", “&”})
remain_open = false

local access_response = responseToTable(oAuth.getAccessToken(authorize_response.oauth_token, authorize_response.oauth_verifier, twitter_request_token_secret, consumer_key, consumer_secret, “https://api.twitter.com/oauth/access_token”), {"=", “&”})

access_token = access_response.oauth_token
access_token_secret = access_response.oauth_token_secret
user_id = access_response.user_id
screen_name = access_response.screen_name
– API CALL:

–change the message posted
local params = {}
params[1] =
{
key = ‘status’,
value = "Follow us "
}

request_response = oAuth.makeRequest(“https://api.twitter.com/1/statuses/update.json”, params, consumer_key, access_token, consumer_secret, access_token_secret, “POST”)
print("req resp ",request_response)
end

return remain_open
end

–this is your webpopup, change position/size as you wish
function tweetit (event)
–system.openURL(10, 20, 300, 450, “http://api.twitter.com/oauth/authorize?oauth_token=” … twitter_request_token, {urlRequest = listener})
native.showWebPopup(10, 10, 460, 360, “http://api.twitter.com/oauth/authorize?oauth_token=” … twitter_request_token, {urlRequest = listener})
end

–I use this for testing on a mac, but lack of textfield makes it difficult for my app, i may as well use device
–you could use a random message generator for testing purposes so as to send a unique message each time,
–would let you see messages in terminal then

–system.openURL(10, 20, 300, 450, “http://api.twitter.com/oauth/authorize?oauth_token=” … twitter_request_token, {urlRequest = listener})

–this is the bit that strips the token from the web address returned
–/////////////////////////////////////////////////////////////////////////////////////
–// RESPONSE TO TABLE
–/////////////////////////////////////////////////////////////////////////////////////
function responseToTable(str, delimeters)
local obj = {}

while str:find(delimeters[1]) ~= nil do
if #delimeters > 1 then
local key_index = 1
local val_index = str:find(delimeters[1])
local key = str:sub(key_index, val_index - 1)

str = str:sub((val_index + delimeters[1]:len()))

local end_index
local value

if str:find(delimeters[2]) == nil then
end_index = str:len()
value = str
else
end_index = str:find(delimeters[2])
value = str:sub(1, (end_index - 1))
str = str:sub((end_index + delimeters[2]:len()), str:len())
end
obj[key] = value
print(key … “:” … value)
else

local val_index = str:find(delimeters[1])
str = str:sub((val_index + delimeters[1]:len()))

local end_index
local value

if str:find(delimeters[1]) == nil then
end_index = str:len()
value = str
else
end_index = str:find(delimeters[1])
value = str:sub(1, (end_index - 1))
str = str:sub(end_index, str:len())
end
obj[#obj + 1] = value
print(value)
end
end
return obj
end

local twitterButton = display.newImage (“twitter.png”)
twitterButton.x = 150
twitterButton.y = 280

twitterButton:addEventListener(“tap”, tweetit) [import]uid: 160777 topic_id: 30216 reply_id: 121627[/import]