【问题标题】:Fitbit OAuth API request, invalid signatureFitbit OAuth API 请求,无效签名
【发布时间】:2015-11-01 16:53:19
【问题描述】:

我正在尝试使用来自 fitbit (https://dev.fitbit.com/apps/oauthtutorialpage) 的 oauth 调试器向 fitbit 发出 API 请求,我试图找出我做错了什么。我在下面的代码中添加了 cmets,以帮助您了解我想要实现的目标。我可以肯定的是,我要么错误地签署了我的请求,要么使用了错误的数据来签署它。 API 响应回应了这一点。

我知道 stackoverflow 上有更多的 fitbit api 问题,但是没有找到我的答案。

有没有在 Oauth 签名方面有更多经验的人知道我可能做错了什么?或者可以帮我找到一种不同的方法吗?

var request = require('request');
var crypto = require('crypto');

var params = {
    'oauth_consumer_key' : 'key12345',
    'oauth_nonce' : Math.random().toString(36).substring(3), //random string
    'oauth_signature_method' : 'HMAC-SHA1',
    'oauth_timestamp' : Date.now().toString().substring(0,10), //timestamp with the same length as in the tutorial
    'oauth_version' : '1.0'
}
var oauth_consumer_secret = 'secret123';
var post_string = 'POST&https://api.fitbit.com/oauth/request_token';

for(var key in params){
    post_string += '&' + key + '=' + params[key];
}

/*At this point we have made a post string that we have to hash with hmac-sha1
the post string looks like this:
POST&https://api.fitbit.com/oauth/request_token&oauth_consumer_key=key12345&oauth_nonce=az6r8cqlzyqfr&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1439147378&oauth_version=1.0

The post_string from the tutorial looks like this:
POST&%2Foauth%2Frequest_token&oauth_consumer_key%3D%26oauth_nonce%3D%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1439145944%26oauth_version%3D1.0

*/

var hmac = crypto.createHmac('sha1', oauth_consumer_secret + "&");
// The tutorial page shows me the signature was 'signed with secret&'. I have tried with and without the & at the end, but without luck.
hmac.setEncoding('base64'); //i'm not sure if this is correct
hmac.write(post_string);
hmac.end();
var hash = hmac.read();

//and finally adding the hash to the parameters.
params.oauth_signature = hash; 

//now, making the request with an authorization header.
var header='';
for (var key in params){
    if(header.length === 0){
        header = ' OAuth ' + key + '="' + params[key] + '"';
    }
    else{
        header += ', ' + key + '="' + params[key] + '"';
    }
}

/*
At this point the header parameter looks like this

OAuth oauth_consumer_key="key12345", oauth_nonce="jnr97ppvjs2lnmi", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1439148049", oauth_version="1.0", oauth_signature="random_signature"   

The tutorial tells me to use the headers:
OAuth oauth_consumer_key="key12345", oauth_nonce="jnr97ppvjs2lnmi", oauth_signature="different_signature", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1439145944", oauth_version="1.0"
*/

var headers ={
    'Authorization' : header
}

var url="https://api.fitbit.com/oauth/request_token";
var requestTimeout = 5000;
var opts = {
    url: url,
    timeout: requestTimeout,
    headers : headers
}

request(opts, function (err, res, body) {
    if (err) {
        console.dir(err);
        return;
    }
    var statusCode = res.statusCode;
    if(res.statusCode === 200){
         console.log(body);
    }
    else{
        console.log("http-error-code: " + res.statusCode);
        console.log(body);
    }
})
/*
The response: 
http-error-code: 401
{"errors":[{"errorType":"oauth","fieldName":"oauth_signature","message":"Invalid signature: 9fXI85C7GvZqMyW1AK1EkOSWZCY="}],"success":false}
*/

【问题讨论】:

    标签: javascript node.js api oauth


    【解决方案1】:

    要获取访问令牌和秘密,请使用Grant(您可以在playground 中测试 FitBit)。

    一旦您拥有访问令牌和秘密,请使用 Purest 向 FitBit API 发出后续请求。

    以下是如何获取用户个人资料的示例:

    var Purest = require('purest')
    var fitbit = new Purest({provider:'fitbit',
      key:'[CONSUMER_KEY]', secret:'[CONSUMER_SECRET]'})
    
    fitbit.get('user/-/profile', {
      oauth:{token:'[ACCESS_TOKEN]', secret:'[ACCESS_SECRET]'}
    }, function (err, res, body) {})
    

    或者,您可以使用请求:

    var request = require('request')
    
    request.get('https://api.fitbit.com/1/user/-/profile.json', {
      oauth:{
        consumer_key:'..',
        consumer_secret:'..',
        token:'..',
        token_secret:'..'
      }
    }, function (err, res, body) {})
    

    简而言之 - 不要尝试自己实现 Web 服务器 OAuth 流程 - 使用 Grant,然后使用 Purest 或请求,请记住,您不必通过所有 OAuth自己的参数,只需传递凭据。

    【讨论】:

      猜你喜欢
      • 2011-04-25
      • 2014-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-12
      • 2015-04-21
      • 1970-01-01
      相关资源
      最近更新 更多