当我使用我的AccessToken请求我的网站的API时,得到一个错误,即'无效的访问令牌'

时间:2016-08-23 04:32:14

标签: javascript node.js api express oauth

我使用mashape-oauth来获取AccessToken,但是当我使用我的AccessToken请求我的网站的API时,出现了一个错误,即'无效的访问令牌'。 也许我在mashape-oauth提供的这一步中遇到了错误:

oa.post(options, callback);
oa.get(options, callback);
oa.delete(options, callback);
oa.patch(options, callback);
oa.put(options, callback);

// Alternatively, you can use the old node-oauth style: (Where method is one of five above.)
oa.method(url, oauth_token, oauth_token_secret, body, type, parameters, callback);

如何更改代码以请求API? 这是我的尝试:

var express = require('express');
var router = express.Router();
var OAuth = require('mashape-oauth').OAuth;
var consumerKey    = 'myconsumerKey';
var consumerSecret = 'myconsumerSecret';
var oa = new OAuth({ 
  accessUrl:"http://fanfou.com/oauth/request_token",
  consumerKey:consumerKey,
  consumerSecret:consumerSecret,
  version:'1.0',
  signatureMethod:'HMAC-SHA1'
});

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'hello' });
});
router.route("/fanfou").get(function(req,res){   
    res.render("fanfou",{title:'fanfou'});
}).post(function(req,res){                        
    var username = req.body.username;  
    var password= req.body.password;
    oa.getXAuthAccessToken(username, password, function (error, oauth_token, oauth_token_secret, results) {
      if (error)
        console.log('err');
      else
      {
      /* res.json({
        oauth_token:oauth_token,
        oauth_token_secret:oauth_token_secret
       })*/
       oa.get('http://api.fanfou.com/statuses/home_timeline.json',
       oauth_token,oauth_token_secret,null,null,null,function(error,data){
               res.json({
                 data:data
               });
               //console.log(JSON.parse(data));
       }); 
      }

    });

});
module.exports = router;

0 个答案:

没有答案