twitter4J, jquery, coldfusion integration

こ雲淡風輕ζ 提交于 2019-12-24 20:23:18

问题


I'm using Twitter4j and attempting to obtain and store user's Twitter access tokens to database for later tweeting. I want to do it entirely with jQuery and ajax, silently.

I have a cfc with the basic functions necessary. e.g. tHe following jquery calls a cfc function which generates the requestURL and pops open a Twitter auth window.

$(".cbLinkTwitter").live("click", function(e) {
     $.getJSON(cfcRoot + "/twitter.cfc?method=getRequestURL&returnformat=json, {"user_id":user_id}, function(res,code) {
          openWindow(res);
     });
     e.preventDefault();
});

This is all working fine. However after the user grants authorisation, how to I use jQuery to capture the returned tokens. It needs to return to a specified callback URL, but I would like it to return the data silently if possible. Could this be done with an iframe?

I'm probably asking too much, but if anyone's done anything similar, I'd really appreciate a shove in the right direction.


回答1:


Have a look here.

The way this site does it is by generating a RequestToken using getOAuthRequestToken(), then call getToken() and getSecretToken() on the RequestToken and store those 2 variables in the SESSION scope. Then the Authorization URL is generated using getAuthorizationURL().

After the user has approved the OAuth request, you need to generate an Access Token and store the results of those 2 methods: AccessToken.getToken() and AccessToken.getTokenSecret().

So to answer your question, the way I'd assume it would work, would be to store your oAuthRequestToken and oAuthRequestTokenSecret is SESSION scope before generating and returning the Authorization URL.

When you register your application with Twitter, you specify a callback URL. That's the page where the user is going to be redirected after the user autorizes your application on Twitter.

When the callback URL is called, simply create an Access Token from the request token stored in SESSION sope.

<cfset AccessToken = Twitter.getOAuthAccessToken(Session.oAuthRequestToken,Session.oAuthRequestTokenSecret) />

and store the Token and TokenSecret in database:

<cfset Token = AccessToken.getToken() />
<cfset TokenSecret = AccessToken.getTokenSecret() />

Hope this helps



来源:https://stackoverflow.com/questions/7104243/twitter4j-jquery-coldfusion-integration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!