Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Twitter API with Coldfusion

Posted on 2010-11-11
11
Medium Priority
?
1,143 Views
Last Modified: 2012-06-27
All I want to do is pull RSS feeds from many Twitter users of their tweets. I seem to now need to use the API to authenticate. I need a simple way to Authenticate then then pull down 50 or so tweets from 50 users. I just take the first tweet of each user.

I have this programed already but it did not use the API authentication. I have my API key.
I loop through this code in the past to pull down the tweets.

<cfhttp url="http://twitter.com/statuses/user_timeline/89762861.rss" method="get" />
 <cfset objRSS = xmlParse(cfhttp.filecontent)> 

Then a break down the XML

Open in new window

0
Comment
Question by:RickEpnet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 20

Expert Comment

by:Proculopsis
ID: 34118827

Try simulating a login with cfhttp and then use cfhttpparam to recycle the Set-Cookie collection from the responseHeader, as authentication, back into future requests.

0
 
LVL 1

Accepted Solution

by:
CFCanada earned 2000 total points
ID: 34119519
Are you sure you need to authenticate using the API just to consume RSS feeds?  I just had a go using your code and my own feed and it worked fine.

If you do need to authenticate, the Twitter API uses oAuth.  You'll need some kind of oAuth library to tap into this with ColdFusion like http://oauth.riaforge.org/ or a java library like http://twitter4j.org/en/index.html#introduction .
0
 
LVL 3

Expert Comment

by:dagaz_de
ID: 34119606
With this set of functions you will get the x last post of an specific user

<!--- ! function: getstatus(tu,tp,user/id,section,page,since,limit) --->
<!---
get status
tu = twitter username
tp = twitter password
user = screen_name to return status for
id = userid to return status for
section = status/timeline/mentions
page = page number to return
since = results since this id
limit = limit return results
--->    
      <cffunction name="getstatus">
        <cfargument name="tu" type="string" required="yes">
        <cfargument name="tp" type="string" required="yes">
          <cfargument name="user" type="string" required="yes">
          <cfargument name="section" type="string" required="yes">
        <cfargument name="page" type="numeric" required="no">
        <cfargument name="since" type="numeric" required="no" />
        <cfargument name="limit" required="no" type="numeric">

            <cfparam name="usertype" default="screen_name">
            <cfset baseURL = "http://twitter.com/statuses/">

        <cfswitch expression="#arguments.section#">
              <cfcase value="status">
                        <cfif isnumeric("arguments.user")>
                        <cfset geturl = "user_timeline.json?user_id=#arguments.id#">
                        <cfelse>                
                        <cfset geturl = "user_timeline.json?screen_name=#arguments.user#">
                </cfif>
            </cfcase>
              <cfcase value="timeline">
                  <cfset geturl = "friends_timeline.json?temp=0">
            </cfcase>
            <cfcase value="mentions">
                  <cfset geturl = "mentions.json?temp=0">
            </cfcase>
            </cfswitch>
          <cfif isdefined("arguments.limit")>
                  <cfset geturl = geturl & "&count=#arguments.limit#">
            </cfif>

            <cfif isdefined("arguments.page")>
                  <cfset geturl = geturl & "&page=#arguments.page#">
            </cfif>

          <cfif isdefined("arguments.since")>
                  <cfset geturl = geturl & "&since=#arguments.since#">
            </cfif>
      
            <cfset geturl = baseurl & geturl>
        <cfhttp url="#geturl#" method="get" username="#arguments.tu#" password="#arguments.tp#" result="statusmsg" />

        <cfif statusmsg.statuscode eq "200 ok">
                  <cfset statusout = deserializejson(statusmsg.filecontent)>
            <cfelse>
                  <cfset statusout = arraynew(1)>
                  <cfset statusout[1] = "fail">
                  <cfset statusout[2] = statusmsg.statuscode>
            </cfif>
          <cfreturn statusout>
    </cffunction>

<cffunction name="parseTweet" access="public" output="false" returntype="string" hint="Parse Twitter Usernames, Hashtags and URLs in a tweet">
      <cfargument name="tweet" type="string" required="true" hint="The Tweet" />

      <cfset var LOCAL = structNew() />
      <cfset LOCAL.tweet = ARGUMENTS.tweet />

      <!--- Parse the URLs as Links to the resource --->
      <cfset LOCAL.tweet = REReplace(LOCAL.tweet,
                                          '([A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&amp;\?\/.=]+)',
                                          '<a href="\1" target="_blank" rel="nofollow">\1</a>',
                                          'ALL')
                                          />
      <!--- Parse the Usernames as Links to Twitter --->
      <cfset LOCAL.tweet = REReplace(LOCAL.tweet,
                                          '[@]+([A-Za-z0-9-_]+)',
                                          '<a href="http://twitter.com/\1" title="Lookup user @\1 on Twitter" target="_blank" rel="nofollow">@\1</a>',
                                          'ALL')
                                          />
      <!--- Parse Hashtags as Links to Twitter�s Search --->
      <cfset LOCAL.tweet = REReplace(LOCAL.tweet,
                                          '[##]+([A-Za-z0-9-_]+)',
                                          '<a href="http://search.twitter.com/search?q=%23\1" title="Search for ##\1 hashtag on Twitter" target="_blank" rel="nofollow">##\1</a>',
                                          'ALL')
                                          />

      <cfreturn LOCAL.tweet />

</cffunction>


<!---getstatus(tu,tp,user/id,section,page,since,limit)--->
       <strong>last 10 Tweets:</strong><br />
       <br />

       <cfset statusresult = getstatus([youruser],[yourpass],'[theuserid]','status',0,0,'10')>
      <!---<cfdump var="#statusresult#">--->
      <cfloop array="#statusresult#" index="k">
      <cfoutput>
      <span class="Stil1"><small>#k.created_at#</small></span><br />
      #parseTweet(k.text)#<br /><br />
      </cfoutput>
      </cfloop>
      </cfif>
0
Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

 
LVL 14

Author Comment

by:RickEpnet
ID: 34120073
Thanks everyone I will try these and let you know in a few days.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 34122333
@dagaz_de - Nice. Is that your own code?
0
 
LVL 14

Author Closing Comment

by:RickEpnet
ID: 34131156
You were correct. I took another look and it was an issue with my code and the way I was updating the database. I had put some code that would stop if a twitter account was not active but I should have just skipped it. Thank You.
0
 
LVL 19

Expert Comment

by:Jones911
ID: 34133082
You get rate limited if you make to many requests though.
0
 
LVL 14

Author Comment

by:RickEpnet
ID: 34133268
I only request 50 an hour so I am under their limit. You can see how it displays at http://www.pigkinu.com
0
 
LVL 19

Expert Comment

by:Jones911
ID: 34133272
OK great.  Just FYI that page didnt load.
0
 
LVL 14

Author Comment

by:RickEpnet
ID: 34133277
Sorry to see it it is http://www.pigskinu.com/ucla.html
0
 
LVL 19

Expert Comment

by:Jones911
ID: 34133281
Works well.  Nice site.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question