开发者

twitter4j.auth.AccessToken instantiating in ColdFusion

开发者 https://www.devze.com 2023-04-13 02:15 出处:网络
I\'m working with ColdFusion 9.0.1 and latest (for current date) stable build of twitter4j library - twitter4j-core-2.2.4. I\'m trying to create functionality which allows users to login or register a

I'm working with ColdFusion 9.0.1 and latest (for current date) stable build of twitter4j library - twitter4j-core-2.2.4. I'm trying to create functionality which allows users to login or register at our site using their twitter accounts. I was able to create authorization part: user click on the link on our site and system redirects him to twitter page. On this page he able to "Authorise" our application. After that system redirecting him back using callBackURL. But I have a problem with next step. When I'm trying to setOAuthAccessToken and for that trying to instantiate AccessToken object with follow part of code:

accessToken = createObject( 'java', 'twitter4j.auth.AccessToken' ).init( 'myStoredRequestToken', 'myStoredRequestTokenSecret' );

But I have follow error:

An exception occurred while instantiating a Java object. The class must not be an interface or an abstract class. Error: ''.

Any ideas?

Update:

The start part of stacktrace:

'coldfusion.runtime.java.JavaObjectInstantiationException: Object instantiation exception. at coldfusion.runtime.java.JavaProxy.CreateObject(JavaProxy.java:171) at coldfusion.runtime.java.JavaProxy.invoke(JavaProxy.java:80) at coldfusion.runtime.CfJspPage._invoke(CfJspPage.java:2360) at cftwitter2ecfc2084917956$funcGETUSERCREDENTIALS.runFunction(C:\inetpub\wwwroot_test\twPlayGrnd_com\twitter.cfc:36) at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:472) at coldfusion.runtime.UDFMethod$ArgumentCollectionFilter.invoke(UDFMethod.java:368) at coldfusion.filter.FunctionAccessFilter.invoke(FunctionAccessFilter.java:55) at ...

...cut here, not sure this is important...

the last part is

cfapplication2ecfc665259542$funcONREQUEST.runFunction(C:\inetpub\wwwroot_test\twPlayGrnd\application.cfc:55) ... 55 more Caused by: java.lang.IllegalArgumentException: Invalid access token format. at twitter4j.auth.AccessToken.(AccessToken.java:50) ... 60 more'

I saw the message about wrong format, but based on documentation at http://twitter4j.org it should accept two agruments (strings with keys). Am I wrong?

Update 2 *just find that out - I am sorry that I brought you into confusion with my first post and example... of course I used myStoredRequestToken, myStoredRequestTokenSecret, not a consumer key/secret * *there are relevant parts of code I'm using for this functionality*

application.cfc ("onApplicationStart" function, instantiating components on start of application)

<cffunction name="onApplicationStart" access="public" returntype="boolean" output="false">
  ...
  <cfset application.com.twitterInstance = server.javaloader.create("twitter4j.TwitterFactory").getInstance() />
  <cfset application.com.twitter = createObject("component","_com.twitter").init() />  *<!--- cfc component which will be listed below --->*
  ...
</cffunction>

twitter.cfc (corresponding coldfusion component)

<cfcomponent displayname="twitter" output="false">

  <cffunction name="init" access="public" output="false">
    <cfreturn this>
  </cffunction>     

<cffunction name="authorizeTwitter" access="public" output="false">
  <cfargument name="callBackURL" type="string" required="false" default="#request.twtCallBackURL#" /> 

    <cfset var requestToken = "" />

    <cfset application.com.twitterInstance.setOAuthConsumer(request.twtConsumerKey,request.twtConsumerSecret) />   
    <cfset requestToken = application.com.twitterInstance.getOAuthRequestToken(arguments.callBackURL) />
    <cflock scope="session" type="exclusive" timeout="10">
      <cfset session.oAuthRequestToken = requestToken.getToken()>
      <cfset session.oAuthRequestTokenSecret = requestToken.getTokenSecret()>
    </cflock>
    <cflocation url="#vLocal.requestToken.getAuthorizationURL()#" addtoken="No" />
</cffunction>


<cffunction name="getUserCredentials" access="public" output="true">
  <cfset var vLocal = {} />
  <cfset vLocal.accessToken = "" />
  <cfset vLocal.userData = "" />
  <cfset vLocal.requestToken = "" />

  <cfset vLocal.accessToken = server.javaloader.create("twitter4j.auth.AccessToken").init(session.oAuthRequestToken,session.oAuthRequestTokenSecret)>
  <cfset application.com.twitterInstance.setOAuthAccessToken(vLocal.accessToken) />
  <cfset开发者_JS百科 vLocal.userData = application.com.twitterInstance.verifyCredentials() />

  <cfdump var="#vLocal.userData#" label="User Credentials">
</cffunction>

First function is for first step - requesting twitter for autorization page (where user can autorize or deny application). Call back URL runs the page what calls the second function and I have problem only at this step (line for generation accessToken).

I have the same result if Im using createObject function instead of javaloader.

*So, my main question is still the same - to obtain the users unique Access Token? Please point me, what I'm doing wrong? What is a correct format for unique user's accessToken generation? Should I place oauth_verifier parameter there? if so, how?*


You are passing consumer key/secret instead of access token/secret. You can generate your access token/secret at dev.twitter.com. https://dev.twitter.com/apps » create my access token

Best, Yusuke


I think I figured out what is wrong with the help of the examples 8. Sign in with Twitter and Adding support for automated tweets with OAuth. Only tested with my own account though ..

Before you redirect to the authorization page, save the whole RequestToken object in a session variable. You will need it to extract the AccessToken. Note: I am storing the TwitterFactory in the application scope - not the instance

 <cfset Twitter = application.TwitterFactory.getInstance()>
 <cfset Twitter.setOAuthConsumer(application.TwitterConsumerKey, application.TwitterConsumerSecret)>
 <cfset Session.RequestToken = Twitter.getOAuthRequestToken( YourCallBackURL )>    

On callback, twitter adds a parameter named oauth_verifier to the URL. Use that value and the saved RequestToken to extract the AccessToken.

<cfset AccessToken = Twitter.getOAuthAccessToken(Session.RequestToken, URL.oauth_verifier)>
<cfset session.StoredAccessToken = AccessToken.getToken()>
<cfset session.StoredAccessSecret = AccessToken.getTokenSecret()>

Once you have the AccessToken/Secret you can access user details (update status,...) anywhere.

<cfset Twitter = application.TwitterFactory.getInstance()>
<cfset Twitter.setOAuthConsumer(application.TwitterConsumerKey,application.TwitterConsumerSecret)>
<cfset AccessToken = createObject("java", "twitter4j.auth.AccessToken")>
<cfset OAuthToken = AccessToken.init(session.StoredAccessToken, session.StoredAccessSecret)>
<cfset Twitter.setOAuthAccessToken(OAuthToken)>
<cfset userData = Twitter.verifyCredentials()>

<cfoutput>
    id = #userData.getId()#<br> 
    name = #userData.getName()#<br> 
    followers = #userData.getFollowersCount()#<br>  
    friends = #userData.getFriendsCount()#<br>  
</cfoutput>
0

精彩评论

暂无评论...
验证码 换一张
取 消