How to get a user's oauth_access_token client-side like facebook, for server side integration


#1

In facebook I can get the user’s authId relatively easily client-side. How would I do this via twitter on an AJAX site?

In the psudo-code below I get the Access Token if the user is logged in and pass that server side so I can do a server-side post. And if the user is not logged in/connected then facebook throws up a dialog that lets them log in, then returns (without refreshing the page) and executes a function that I can then get at the Access Token from.

Can this be done in twitter javascript, perhaps with @anywhere?

Thank you for your help!

if (FB) {
FB.getLoginStatus(function(response) {
if (response.status === ‘connected’) {
$.getJSON(window.page.services + “StoreAuthToken”, { type: “facebook”, socialId: response.authResponse.userID, authId: response.authResponse.accessToken, expiresInSeconds: response.authResponse.expiresIn }, null);
window.dialogs.loadFacebookOverlay({ href: href, image: image, description: description, socialId: response.authResponse.userID });
} else {
FB.login(function(response) {
if (response.status === ‘connected’) {
$.getJSON(window.page.services + “StoreAuthToken”, { type: “facebook”, socialId: response.authResponse.userID, authId: response.authResponse.accessToken, expiresInSeconds: response.authResponse.expiresIn }, null);
window.dialogs.loadFacebookOverlay({ href: href, image: image, description: description, socialId: response.authResponse.userID });
} else { /* user cancled */ }
}, { scope: ‘share_item’ });
}
});


#2

For twitter I’ve got this so far.

if (twttr) {
twttr.anywhere(function(T) {
if (T.isConnected()) {
/* how do I get the auth token here? /
window.dialogs.loadTwitter({ href: href, image: image, description: description });
} else {
/
a pop-up is blocked - any way to allow it? */
T.signIn();
}
});
}

My questions are:

  • How do I get the auth token if the user is already connected?
  • The signIn tries to open a window but is blocked by the browser: is there a way to avoid this?

#3

Also asked here: http://stackoverflow.com/questions/14529139/for-twitter-how-to-get-a-users-oauth-access-token-client-side-like-facebook-f


#4

I’ve made some progress. Now all I need is the auth token so I can do my server-side stuff.

I call the anywhere function like this
$(window).bind(“load”, function(e) {
if (twttr) {
twttr.anywhere(function(T) {
window.twitter = T;
});
}
});

Then I can call the signIn() without the pop-up being blocked. The problem is that the when I call window.twitter.bind(“authComplete”, function(e, user) { /* user data doesn’t have the auth token */ });
So I can’t send the auth token back to my server along with the user.data(“id”).

Now, how can I get the auth token now that I’m connected?


#5

@Anywhere is deprecated. It will be retired on March 5th, 2013. I wouldn’t recommend using it.

API v1.1 and OAuth 1.0A aren’t meant to be used through client-side Javascript. For the most part, what you’re trying to do isn’t possible using the API today.


#6

Lol, that figures that it’s deprecated.
(I love your profile pic BTW!)

We’re trying to mimic how the New York Times iPad app does twitter sharing - through a custom dialog that lives in an overlay, not a pop-up window. Are you saying that there currently isn’t a way to do this?

If there is a way to do this is there some documentation somewhere on what endpoints we can converse with to allow a user to approve our app, get authentication and post a tweet? I can’t find anything anywhere.

Thank you for your help!


#7

#8