I like Twitter... Enjoy ;-)

Bringing the internet highway into your project? Building FTP, HTTP, email, chat or other client solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
jihem
Posts: 53
Joined: Sun Jul 28, 2013 3:21 pm

I like Twitter... Enjoy ;-)

Post by jihem » Fri Jan 30, 2015 5:31 pm

/*
twitter, libTwitter
Version 1.0 Jean-Marc QUERE, @codyssea
30th January 2015

inspired by "How to use Twitter OAuth v1.1 with JavaScript/jQuery"
Mike Rogers, @MikeRogers0

hmacSha1, libHash-Hmac
Version 2.3 Mark Smith, mark@maseurope.net
9th September 2009
*/

function arrayMerge pArray1,pArray2
local pArray
put pArray1 into pArray
repeat for each line tKey in the keys of pArray2
put pArray2[tKey] into pArray[tKey]
end repeat
return pArray
end arrayMerge

function buildBaseString pBaseUrl, pMethod,pParams
local tResult
get the keys of pParams
sort lines of it
repeat for each line tKey in it
put tKey & "%3D" & urlencode(pParams[tKey]) & "%26" after tResult
end repeat
put char 1 to -4 of tResult into tResult
return pMethod & "&" & urlencode(pBaseUrl) & "&" & tResult
end buildBaseString

function hmacSha1 pMsg, pKey
if length(pKey) > 64 then put sha1digest(pKey) into pKey
repeat 64 - length(pKey)
put null after pKey
end repeat
repeat for each byte c in pKey
put numtobyte(bytetonum(c) bitXor 54) after ipad
put numtobyte(bytetonum(c) bitXor 92) after opad
end repeat
return sha1digest(opad & sha1digest(ipad & pMsg))
end hmacSha1

function buildAuthorizationHeader pOAuth
local tResult
put "Authorization: OAuth " into tResult
repeat for each line tKey in the keys of pOAuth
put tKey & "=" & quote & urlencode(pOAuth[tKey]) & quote & comma & space after tResult
end repeat
return char 1 to -3 of tResult
end buildAuthorizationHeader

function twitter pUrl
local tTwitter
put "--- put your token here -------------" into tTwitter["oauth_access_token"]
put "--- put your secret token here ------" into tTwitter["oauth_access_token_secret"]
put "--- put your consumer key here ------" into tTwitter["consumer_key"]
put "--- put your consumer secret here ---" into tTwitter["consumer_secret"]
put "https://api.twitter.com/1.1/" into tTwitter["base_url"]

local tUrlParts
put pUrl into tUrlParts
split tUrlParts by "?"
-- tUrlParts[1] : path, tUrlParts[2]: query

local tUrlArguments
put tUrlParts[2] into tUrlArguments
split tUrlArguments by "&" and "="

local tFullUrl
put tTwitter["base_url"] & pUrl into tFullUrl

local tBaseUrl
put tTwitter["base_url"] & tUrlParts[1] into tBaseUrl

local tTime
put the internet date into tTime
convert tTime to seconds

local tOAuth
put "1.0" into tOAuth["oauth_version"]
put tTwitter["consumer_key"] into tOAuth["oauth_consumer_key"]
put tTime into tOAuth["oauth_nonce"]
put "HMAC-SHA1" into tOAuth["oauth_signature_method"]
put tTwitter["oauth_access_token"] into tOAuth["oauth_token"]
put tTime into tOAuth["oauth_timestamp"]

local tBaseInfo
put buildBaseString(tBaseUrl,"GET",arrayMerge(tOAuth,tUrlArguments)) into tBaseInfo

local tCompositeKey
put urlencode(tTwitter["consumer_secret"]) & "&" & urlencode(tTwitter["oauth_access_token_secret"]) into tCompositeKey
put base64Encode(hmacSha1( tBaseInfo, tCompositeKey)) into tOAuth["oauth_signature"]
set the httpHeaders to buildAuthorizationHeader(tOAuth)
return url(tFullUrl)
end twitter

/* sample : put a json response in the field "fldTweets"

on mouseUp
put twitter("statuses/user_timeline.json?screen_name=codyssea&count=10&include_rts=false&exclude_replies=true") into field "fldTweets"
end mouseUp

*/

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: I like Twitter... Enjoy ;-)

Post by Mark » Sat Jan 31, 2015 9:48 am

Hi,

That's very interesting. I'll give it a try.

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

jihem
Posts: 53
Joined: Sun Jul 28, 2013 3:21 pm

Re: I like Twitter... Enjoy ;-)

Post by jihem » Sun Feb 01, 2015 5:12 pm

Since everything works as expected on Linux, OSX and Windows desktop, I made a run with LiveCode server and...

I got an issue with LiveCode server trying to use my library : https isn't working (known bug waiting to be fixed).
The following trick get the job done. You need the wget command. Windows users can download and install (in c:\windows) the command ( http://users.ugent.be/~bpuype/wget).

Replace :
set the httpHeaders to buildAuthorizationHeader(tOAuth)
return url(tFullUrl)
end twitter

With:
local tHeader
put buildAuthorizationHeader(tOAuth) into tHeader
replace quote with "\" & quote in tHeader
return shell("wget -q -O - --header " & quote & tHeader & quote & " --no-check-certificate "& quote & tFullUrl & quote)

Hope this help,
jihem

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: I like Twitter... Enjoy ;-)

Post by Mark » Tue Feb 24, 2015 3:41 pm

Hi Johem,

I can only retrieve tweets, but I can't post any. Do you have any idea about how to solve this? I think I can adjust the code to post to a Twitter URL, but I have problems getting the encoded headers right.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

jihem
Posts: 53
Joined: Sun Jul 28, 2013 3:21 pm

Re: I like Twitter... Enjoy ;-)

Post by jihem » Tue Feb 24, 2015 7:54 pm

Hi Mark,
I made this to collect tweets with specific hashtags (databases for some brands). I haven't yet send a tweet with. I think its only a change in the URL (with appropriate parameters). I'm quite busy right now. I will give it a try and I will answer in the current of the next week. Happy to see you could use it. Have a good day.
Regards,
jihem

jihem
Posts: 53
Joined: Sun Jul 28, 2013 3:21 pm

Re: I like Twitter... Enjoy ;-)

Post by jihem » Fri Mar 13, 2015 11:58 am

Sorry I'm late but this is the answer.

function twitter.post pUrl
local tUrlParts
put pUrl into tUrlParts
split tUrlParts by "?"
-- tUrlParts[1] : path, tUrlParts[2]: query

local tUrlArguments
put tUrlParts[2] into tUrlArguments
split tUrlArguments by "&" and "="

local tFullUrl
put sTwitter["base_url"] & pUrl into tFullUrl

local tBaseUrl
put sTwitter["base_url"] & tUrlParts[1] into tBaseUrl

local tTime
put the internet date into tTime
convert tTime to seconds

local tOAuth
put "1.0" into tOAuth["oauth_version"]
put sTwitter["consumer_key"] into tOAuth["oauth_consumer_key"]
put tTime into tOAuth["oauth_nonce"]
put "HMAC-SHA1" into tOAuth["oauth_signature_method"]
put sTwitter["oauth_access_token"] into tOAuth["oauth_token"]
put tTime into tOAuth["oauth_timestamp"]

local tBaseInfo
put buildBaseString(tBaseUrl,"POST",arrayMerge(tOAuth,tUrlArguments)) into tBaseInfo

local tCompositeKey
put urlencode(sTwitter["consumer_secret"]) & "&" & urlencode(sTwitter["oauth_access_token_secret"]) into tCompositeKey
put base64Encode(hmacSha1( tBaseInfo, tCompositeKey)) into tOAuth["oauth_signature"]
set the httpHeaders to buildAuthorizationHeader(tOAuth)
post "" to url(tFullUrl)
return it
end twitter.post

on mouseUp
put twitter.post("statuses/update.json?status=Mark%20question%20in%20LiveCode%20Internet%20Forum%20has%20been%20answered.") into field "fldTweets"
end mouseUp

You have to allow your application to post before. If you haven't you will got this message:
{"request":"\/1.1\/statuses\/update.json?status=Mark%20question%20in%20LiveCode%20Internet%20Forum%20has%20been%20answered.","error":"Read-only application cannot POST."}

Have a good WE :)
jihem

I have updated the library (with code optimization => twitter.get and twitter.post use a common function: twitter.http).
http://codyssea.com/downloads/libTwitter.zip

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: I like Twitter... Enjoy ;-)

Post by Mark » Mon Mar 16, 2015 11:30 am

Hi Jihem,

It works perfectly! Many thanks.

https://twitter.com/MoreNewz/status/577416132967165952

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

DeeLondon
Posts: 1
Joined: Mon Jan 25, 2016 1:43 pm

Re: I like Twitter... Enjoy ;-)

Post by DeeLondon » Mon Jan 25, 2016 1:53 pm

Hi,

I'm very new to LiveCode so still learning how it all works, I'm interested in creating an App that can post one photo at a time to Twitter with a predefined Message.

Would it be very difficult to adapt the code so you can upload a photo to Twitter?

What I basically want to create is a list of tasks - say 10 Tasks for people to complete and as each task is completed I want them to be able to upload a photo from their Android Phone to Twitter that shows each task was completed.

MWCoastMedia
Posts: 32
Joined: Fri Jan 16, 2015 5:31 pm

Re: I like Twitter... Enjoy ;-)

Post by MWCoastMedia » Thu May 12, 2016 10:24 pm

Anyone have any luck with posting photos? I'd be willing to compensate for the trouble.

Post Reply

Return to “Internet”