Friday, 15 March 2013

javascript - Posting to Twitter through OAuthSimple.js -



javascript - Posting to Twitter through OAuthSimple.js -

i've been stuck on 1 while. i'm trying utilize oauthsimple.js interact twitter in chrome extension i've written.

the signing process seems work fine requests retrieve user's statuses, can't seem build request authenticate when seek retweet, reply, or mark tweet favorite.

i'm next guides here. have tried numerous ways of structuring request, , comparing request contents against output of oauth tool provided twitter ( seems check out ), i'm still getting 401 errors , generic "we couldn't authenticate you" responses.

here's how i'm trying form request:

var sendtwitterrequest = function(url, params, method, callback) { var request = null; if ( localstorage.twitterauthtoken ) { oauthsimple().reset(); request = oauthsimple(twitterconsumerkey,twitterconsumersecret).sign({ action:method, method:"hmac-sha1", datatype:"json", path:url, parameters:params, signatures:{ oauth_version:'1.0', oauth_token:localstorage.twitterauthtoken, oauth_secret:localstorage.twitterauthverifier } }); console.log(request); $j.ajax({ url:request.signed_url, type:method, data:request.parameters, success:callback }); } };

then, making calls method this:

// works, info , can stuff sendtwitterrequest('http://api.twitter.com/1/statuses/user_timeline.json?user_id=',null,'get',somemethod()); // fails , throws 401 error every time sendtwitterrequest("https://api.twitter.com/1/statuses/retweet/"+tweetkey+".json",null,'post',someothermethod());

am missing something? in advance!

it turns out requests creating fine, needed final 1 exchange request tokens oauth tokens. thought step covered when user prompted input, turns out wrong.

i ended switching oauthsimple.js oauth.js, on business relationship of fact oauth.js process both token requests , timeline requests.

some of pretty specific application doing, need modify it.

the new sendtwitterrequest method:

var sendtwitterrequest = function(options){ var accessor={ consumersecret:twitterconsumersecret }; var message={ action:options.url, method:options.method||"get", parameters:[ ["oauth_consumer_key",twitterconsumerkey], ["oauth_signature_method","hmac-sha1"], ["oauth_version","1.0"] ] }; if(options.token){ message.parameters.push(["oauth_token",options.token]) } if(options.tokensecret){ accessor.tokensecret=options.tokensecret } for(var in options.parameters) { message.parameters.push(options.parameters[a]) } oauth.settimestampandnonce(message); oauth.signaturemethod.sign(message,accessor); seek { $j.ajax({ url:message.action, async:options.async||true, type:message.method||'get', data:oauth.getparametermap(message.parameters), datatype:options.format||'json', success:function(data) { if (options.success) {options.success(data);} } }); } grab ( e ) { } };

and methods depend on it:

// asks twitter oauth request token. user authorizes , request token provided requesttwittertoken = function() { // semi-specific extension doing, callback string may need // different. var callbackstring = window.top.location + "?t=" + date.now(); var params = [ [ 'oauth_callback', callbackstring ] ]; sendtwitterrequest({ url: "https://api.twitter.com/oauth/request_token", method: 'post', parameters: params, format: 'text', success: function(data) { var returnedparams = getcallbackparams(data); if ( returnedparams.oauth_token ) { chrome.tabs.create({ url:"https://api.twitter.com/oauth/authorize?oauth_token=" + returnedparams.oauth_token }); } },error:function( e ) { console.log( 'error' ); console.log( e ); } }); }; // exchanges twitter request token actual access token. signintotwitter = function(token, secret, callback) { var auth_url = "https://api.twitter.com/oauth/access_token"; var authcallback = function(data) { var tokens = getcallbackparams(data); localstorage.twitterauthtoken = tokens.oauth_token || null; localstorage.twitterauthtokensecret = tokens.oauth_token_secret || null; callback(); }; seek { sendtwitterrequest({url:auth_url, method:'post', async:true, format:'text', token:token, tokensecret:secret, success:authcallback}); } grab ( e ) { console.log(e); } };

with this, steps follows:

ask twitter token ( requesttwittertoken() ) , provide callback in callback, check see if token provided. if so, it's initial token pass token twitter , open twitter auth page, allows user grant access in callback call, see if access token provided exchange request token access token ( signintotwitter() )

after that, utilize sendtwitterrequest() method nail twitter's api fetch timeline , post tweets.

javascript oauth google-chrome-extension twitter twitter-oauth

No comments:

Post a Comment