Medrise
asked on
ColdFusion and Twitter OAuth
I'm current writing a script that will get any new post from my forum and post it to our Twitter account. I had this script working before, but since Twitter moved to OAuth I need some help.
I'm using the following code
Now if I use this code it does work. I don't see the difference since the StoredAccessToken, and StoredAccessSecret is always the same. But this code now the user need to Accept the application.
I'm using the following code
<cfset TwitterConsumerKey = "xxxxx">
<cfset TwitterConsumerSecret = "xxxx">
<cfset StoredAccessToken = "xxxxx">
<cfset StoredAccessSecret = "xxxx">
<cfset Twitter = createObject("java", "twitter4j.Twitter")>
<cfset Twitter.setOAuthConsumer(TwitterConsumerKey,TwitterConsumerSecret)>
<cfset Twitter.setOAuthAccessToken(StoredAccessToken,StoredAccessSecret)>
<cfset Twitter.updateStatus("My first custom Twitter update! Thanks @RobOBrien!")>
Now if I use this code it does work. I don't see the difference since the StoredAccessToken, and StoredAccessSecret is always the same. But this code now the user need to Accept the application.
<cfset Twitter = createObject("java", "twitter4j.Twitter")>
<cfset Twitter.setOAuthConsumer(TwitterConsumerKey,TwitterConsumerSecret)>
<cfif structKeyExists(url,'oauth_token') IS FALSE>
<!--- // 2. Authorize --->
<cfset RequestToken = Twitter.getOAuthRequestToken()>
<cfset Session.oAuthRequestToken = RequestToken.getToken()>
<cfset Session.oAuthRequestTokenSecret = RequestToken.getTokenSecret()>
<cflocation url="#RequestToken.getAuthorizationURL()#" addtoken="No">
<cfelse>
<!--- // 3. Authenticate // --->
<cfset AccessToken = Twitter.getOAuthAccessToken(Session.oAuthRequestToken,Session.oAuthRequestTokenSecret)>
<cfset session.StoredAccessToken = AccessToken.getToken()>
<cfset session.StoredAccessSecret = AccessToken.getTokenSecret()>
<cfset Twitter.setOAuthAccessToken(Session.StoredAccessToken,Session.StoredAccessSecret)>
<cfset Twitter.updateStatus("My first custom Twitter update! Thanks @RobOBrien!")>
<cfdump var="#session#">
</cfif>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try storing it at the Application level.
ASKER
How can I do that?
These are variables you access as Application.VariableName similar to Session.VariableName. I am a bit of an old school ColdFusion developer (I need to get up-to-date as I miss CFML), so I would put them in the Application.CFM file.
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=appFramework_16.html
However, it would appear that you need to use Application.CFC for newer versions of ColdFusion.
http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-750b.html
Hope that helps!
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=appFramework_16.html
However, it would appear that you need to use Application.CFC for newer versions of ColdFusion.
http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-750b.html
Hope that helps!
ASKER
Thanks... that helped... :D
And this is probably a better reference for CF 9.x or higher:
http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7d48.html
It shows using the onApplicationStart function of Application.CFC. You will see in the code example that they set (at the start of the application) the values of two application scoped variables.
"Application.availableReso urces=0;
Application.counter1=1;"
You can reference those variables later on as #Application.availableReso urces# like you would your session variables.
http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7d48.html
It shows using the onApplicationStart function of Application.CFC. You will see in the code example that they set (at the start of the application) the values of two application scoped variables.
"Application.availableReso
Application.counter1=1;"
You can reference those variables later on as #Application.availableReso
ASKER