Problems with OAuth on node.js

前端 未结 3 1549
别跟我提以往
别跟我提以往 2021-01-30 14:33

I am trying to get OAuth to work on node.js. I found this in the documentation of node-oauth:

var OAuth= require(\'oauth\').OAuth;
var oa = new OAuth(requestUrl,         


        
3条回答
  •  时光取名叫无心
    2021-01-30 15:20

    An update to post tweet to user timeline:

    @mattmcmanus, Extending @mattmcmanus nice answer, I would like to post a tweet to timeline. For this, I am using the same code as mattcmanus given above.

    Step 1:

    oa.getOAuthRequestToken(function(error, oauth_token, oauth_token_secret, results){
      if (error) new Error(error.data)
      else {
        req.session.oauth.token = oauth_token
        req.session.oauth.token_secret = oauth_token_secret
        res.redirect('https://twitter.com/oauth/authenticate?oauth_token='+oauth_token)
       }
    }); 
    

    Step 2:

    app.get('/oauth/callback', function(req, res, next){
          if (req.session.oauth) {
            req.session.oauth.verifier = req.query.oauth_verifier
            var oauth = req.session.oauth
    
            oa.getOAuthAccessToken(oauth.token,oauth.token_secret,oauth.verifier, 
              function(error, oauth_access_token, oauth_access_token_secret, results){
                if (error) new Error(error){
                console.log(results.screen_name)
                }else{
    
                    // NEW CODE TO POST TWEET TO TWITTER
                    oa.post(
                    "https://api.twitter.com/1.1/statuses/update.json",
                    oauth_access_token, oauth_access_token_secret,
                    {"status":"Need somebody to love me! I love OSIpage, http://www.osipage.com"},
                    function(error, data) {
                        if(error) console.log(error)
                        else console.log(data)
                    }
                   );
                   // POST TWEET CODE ENDS HERE
    
                }
            }
          );
        } else
          next(new Error('No OAuth information stored in the session. How did you get here?'))
        });
    

    I have added oauth_access_token & oauth_access_token_secret in commented code. This will post a tweet update to user's timeline. Happy tweeting!!!

提交回复
热议问题