Easy twitter authentication with ColdFusion
Publicado; 05/05/2013 Arquivado em: ColdFusion Comentários desativados em Easy twitter authentication with ColdFusionAs you might know, Twitter API 1.0 will be soon deprecated and anonymous or basic HTTP-authenticated calls/requests won’t be allowed. OAuth can be cumbersome sometimes, specially if all you need is basic querying the API (such as GET search/tweets). Maybe that’s why Twitter API 1.1 introduced a new authentication method that simplifies the process of authenticating requests. Starting with the 1.1, some endpoints that doesn’t require user personification/identification can be accessed using the new Application-only authentication. This new method is much simpler to work with. To exemplify, take a look on a very basic search for the #ColdFusion hashtag against Twitter API 1.1:
<cfset consumerKey = "YOUR APP CONSUMER KEY"> <cfset consumerSecret = "YOUR APP CONSUMER SECRET"> <cfset bearerToken = ToBase64(consumerKey & ":" & consumerSecret)> <cfset authorization = "Basic " & bearerToken> <cfhttp url="https://api.twitter.com/oauth2/token" method="post" charset="utf-8"> <cfhttpparam type="header" name="Authorization" value="#authorization#"> <cfhttpparam type="header" name="Content-Type" value="application/x-www-form-urlencoded;charset=UTF-8"> <cfhttpparam type="body" value="grant_type=client_credentials"> </cfhttp> <cfset bearerTokenResponse = DeserializeJSON(cfhttp.fileContent)> <cfset authorization2 = "Bearer " & bearerTokenResponse.access_token> <cfhttp url="https://api.twitter.com/1.1/search/tweets.json" method="get" charset="utf-8"> <cfhttpparam type="header" name="Authorization" value="#authorization2#"> <cfhttpparam type="url" name="q" value="%23ColdFusion"> </cfhttp> <cfoutput>#cfhttp.fileContent#</cfoutput>
Of course, you still need to register a Twitter App (more about it here). Need to use a Twitter resource that doesn’t support Application-only authentication? Then you should try the excellent (monkeh)Tweet Twitter API.
Enjoy!