在 ColdFusion 中实例化 twitter4j.auth.AccessToken

发布于 2024-12-09 06:28:48 字数 4391 浏览 0 评论 0原文

我正在使用 ColdFusion 9.0.1 和最新(当前日期)稳定版本的 twitter4j 库 - twitter4j-core-2.2.4。我正在尝试创建允许用户使用他们的 Twitter 帐户登录或注册我们的网站的功能。 我能够创建授权部分:用户单击我们网站上的链接,系统将他重定向到 Twitter 页面。在此页面上,他能够“授权”我们的应用程序。之后系统使用 callBackURL 将他重定向回来。 但我下一步有问题。当我尝试 setOAuthAccessToken 并尝试使用以下部分代码实例化 AccessToken 对象时:

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

但我有以下错误:

实例化 Java 对象时发生异常。班级 不能是接口或抽象类。错误:''。

有什么想法吗?

更新:

堆栈跟踪的开始部分:

'coldfusion.runtime.java.JavaObjectInstantiationException:对象实例化异常。在 Coldfusion.runtime.java.JavaProxy.CreateObject(JavaProxy.java:171) 在 Coldfusion.runtime.java.JavaProxy.invoke(JavaProxy.java:80) 在 Coldfusion.runtime.CfJspPage._invoke(CfJspPage.java:2360) 在cftwitter2ecfc2084917956$funcGETUSERCREDENTIALS.runFunction(C:\ inetpub \ wwwroot_test \ twPlayGrnd_com \ twitter.cfc:36)在coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:472)在Coldfusion.runtime.UDFMethod$ArgumentCollectionFilter.invoke(UDFMethod.java:368) 在 Coldfusion.filter.FunctionAccessFilter.invoke(FunctionAccessFilter.java:55) 在...

...切到这里,不确定这是否重要。 ..

最后一部分是

cfapplication2ecfc665259542$funcONREQUEST.runFunction(C:\inetpub\wwwroot_test\twPlayGrnd\application.cfc:55) ... 55 更多 原因:java.lang.IllegalArgumentException:访问令牌格式无效。在 twitter4j.auth.AccessToken.(AccessToken.java:50) ... 60 more'

我看到了有关格式错误的消息,但基于 http://twitter4j.org 它应该接受两个参数(带有键的字符串)。我错了吗?

更新2 *只要发现这一点 - 很抱歉我的第一篇文章和示例让您感到困惑...当然我使用了 myStoredRequestToken、myStoredRequestTokenSecret,而不是消费者密钥/秘密* *我正在使用此功能的相关代码部分*

application.cfc (“onApplicationStart”函数,在应用程序启动时实例化组件)

<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 (相应的 Coldfusion 组件)

<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 vLocal.userData = application.com.twitterInstance.verifyCredentials() />

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

第一个函数是第一步 - 请求 twitter 的授权页面(用户可以在其中授权或拒绝应用程序)。回调 URL 运行调用第二个函数的页面,我仅在这一步遇到问题(生成 accessToken 的行)。

如果我使用 createObject 函数而不是 javaloader,我会得到相同的结果。 >

*所以,我的主要问题仍然是相同的 - 获取用户唯一的访问令牌?请指出我做错了什么?唯一用户的 accessToken 生成的正确格式是什么?我应该在那里放置 oauth_verifier 参数吗?如果是这样,怎么办?*

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 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?*

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想念有你 2024-12-16 06:28:48

您正在传递消费者密钥/秘密而不是访问令牌/秘密。
您可以在 dev.twitter.com 生成您的访问令牌/秘密。
https://dev.twitter.com/apps » 创建我的访问令牌

最好,
佑介

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

沩ん囻菔务 2024-12-16 06:28:48

我认为我在示例 8 的帮助下找出了问题所在。使用 Twitter 登录
添加对自动推文的支持使用 OAuth。不过,仅使用我自己的帐户进行了测试。

在重定向到授权页面之前,请将整个 RequestToken 对象保存在会话变量中。您将需要它来提取 AccessToken。注意:我将 TwitterFactory 存储在应用程序范围中 - 而不是实例

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

在回调时,twitter 将名为 oauth_verifier 的参数添加到 URL。使用该值和保存的 RequestToken 来提取 AccessToken

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

一旦您拥有AccessToken/Secret,您就可以在任何地方访问用户详细信息(更新状态,...)。

<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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文