twitter4J, jquery, coldfusion integration
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 wor开发者_高级运维king 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.
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
精彩评论