use*_*469 4 rest post oauth shared-secret hmacsha1
我正在尝试一些具体的事情,即尝试调用 REST API。我一直遵循这些指示。
我非常小心地确保正确创建“签名基本字符串”。他们定义它是这样创建的:
(HTTP方法)&(请求URL)&(标准化参数)
您可以仔细检查我的代码中是否需要,但我非常确定它没问题。
我遇到的问题是创建他们所谓的“oauth 签名”,而我的与他们的不匹配。他们应该像这样创建:
使用 [RFC2104] 定义的 HMAC-SHA1 签名算法对请求进行签名,其中 text 是签名基本字符串,key 是由“&”字符分隔的 Consumer Secret 和 Access Secret 的串联值(显示“&”即使访问密钥为空,因为某些方法不需要访问令牌)。
计算出的摘要八位字节字符串,首先按照 [RFC2045] 进行 Base64 编码,然后使用 [RFC3986] 百分比编码 (%xx) 机制进行转义,这就是 oauth_signature。
我在我的代码中这样表达:
var oauthSignature = CryptoJS.HmacSHA1(signatureBaseString, sharedSecret+"&");
var oauthSignature64 = encodeURIComponent(CryptoJS.enc.Base64.stringify(oauthSignature));
console.log("hash in 64: " + oauthSignature64);
Run Code Online (Sandbox Code Playgroud)
我正在使用 Google 的 CryptoJS 库。我将签名基本字符串作为文本,然后将我的消费者秘密作为与“&”连接的密钥,我没有访问密钥,这不是必需的,但没关系。然后我对该散列的结果进行 Base 64 编码,然后对它进行 URI 编码,请一些人理智地检查一下我对此的理解以及我在使用该库的代码中对它的使用/表达,我认为这就是我的问题所在。
这是我的完整代码:
var fatSecretRestUrl = "http://platform.fatsecret.com/rest/server.api";
var d = new Date();
var sharedSecret = "xxxx";
var consumerKey = "xxxx";
//this is yet another test tyring to make this thing work
var baseUrl = "http://platform.fatsecret.com/rest/server.api?";
var parameters = "method=food.search&oauth_consumer_key="+consumerKey+"&oauth_nonce=123&oauth_signature_method=HMAC-SHA1&oauth_timestamp="+getTimeInSeconds()+"&oauth_version=1.0&search_expression=banana";
var signatureBaseString = "POST&" + encodeURIComponent(baseUrl) + "&" + encodeURIComponent(parameters);
console.log("signature base string: " + signatureBaseString);
var oauthSignature = CryptoJS.HmacSHA1(signatureBaseString, sharedSecret+"&");
var oauthSignature64 = encodeURIComponent(CryptoJS.enc.Base64.stringify(oauthSignature));
console.log("hash in 64: " + oauthSignature64);
var testUrl = baseUrl+"method=food.search&oauth_consumer_key=xxxx&oauth_nonce=123&oauth_signature="+oauthSignature64+"&oauth_signature_method=HMAC-SHA1&oauth_timestamp="+getTimeInSeconds()+"&oauth_version=1.0&search_expression=banana";
console.log("final URL: " + testUrl);
var request = $http({
method :"POST",
url: testUrl
});
Run Code Online (Sandbox Code Playgroud)
我已注意确保我发布的参数按字典顺序排列,并且我非常确定它是正确的。
我得到的回复是:
无效签名:oauth_signature 'RWeFME4w2Obzn2x50xsXujAs1yI='
所以很明显
我真的很感谢进行健全性检查,这需要一段时间。
好吧...我做到了,但不是我认为最终会这样做的方式,我花了几个小时尝试使用 Angular 然后 JQuery,最后我尝试了 Node JS 并且它起作用了,这里有两个工作示例,一个与food.get
另一个与foods.search
food.get 示例
var rest = require('restler'),
crypto = require('crypto'),
apiKey = 'xxxx',
fatSecretRestUrl = 'http://platform.fatsecret.com/rest/server.api',
sharedSecret = 'xxxx',
date = new Date;
// keys in lexicographical order
var reqObj = {
food_id: '2395843', // test query
method: 'food.get',
oauth_consumer_key: apiKey,
oauth_nonce: Math.random().toString(36).replace(/[^a-z]/, '').substr(2),
oauth_signature_method: 'HMAC-SHA1',
oauth_timestamp: Math.floor(date.getTime() / 1000),
oauth_version: '1.0'
};
// make the string...got tired of writing that long thing
var paramsStr = '';
for (var i in reqObj) {
paramsStr += "&" + i + "=" + reqObj[i];
}
// had an extra '&' at the front
paramsStr = paramsStr.substr(1);
var sigBaseStr = "GET&"
+ encodeURIComponent(fatSecretRestUrl)
+ "&"
+ encodeURIComponent(paramsStr);
// no access token but we still have to append '&' according to the instructions
sharedSecret += "&";
var hashedBaseStr = crypto.createHmac('sha1', sharedSecret).update(sigBaseStr).digest('base64');
// Add oauth_signature to the request object
reqObj.oauth_signature = hashedBaseStr;
rest.get(fatSecretRestUrl, {
data: reqObj,
}).on('complete', function(data, response) {
console.log(response);
console.log("DATA: " + data + "\n");
});
Run Code Online (Sandbox Code Playgroud)
食品.搜索示例
var rest = require('restler'),
crypto = require('crypto'),
apiKey = 'xxxx',
fatSecretRestUrl = 'http://platform.fatsecret.com/rest/server.api',
sharedSecret = 'xxxx',
date = new Date;
// keys in lexicographical order
var reqObj = {
method: 'foods.search',
oauth_consumer_key: apiKey,
oauth_nonce: Math.random().toString(36).replace(/[^a-z]/, '').substr(2),
oauth_signature_method: 'HMAC-SHA1',
oauth_timestamp: Math.floor(date.getTime() / 1000),
oauth_version: '1.0',
search_expression: 'mcdonalds' // test query
};
// make the string...got tired of writing that long thing
var paramsStr = '';
for (var i in reqObj) {
paramsStr += "&" + i + "=" + reqObj[i];
}
// had an extra '&' at the front
paramsStr = paramsStr.substr(1);
var sigBaseStr = "POST&"
+ encodeURIComponent(fatSecretRestUrl)
+ "&"
+ encodeURIComponent(paramsStr);
// again there is no need for an access token, but we need an '&' according to the instructions
sharedSecret += "&";
var hashedBaseStr = crypto.createHmac('sha1', sharedSecret).update(sigBaseStr).digest('base64');
// Add oauth_signature to the request object
reqObj.oauth_signature = hashedBaseStr;
rest.post(fatSecretRestUrl, {
data: reqObj,
}).on('complete', function(data, response) {
console.log(response);
console.log("DATA: " + data + "\n");
});
Run Code Online (Sandbox Code Playgroud)
对于任何使用 Angular 或 JQuery 的人来说真的很抱歉,如果我有一两分钟的空闲时间,我会尝试使用 Angular,任何使用 Angular 的人如果遇到 CORS 相关错误,只需启动 chrome,如下所示:
chromium-browser --disable-web-security
- 我在终端上执行此操作,或者将该扩展添加到 Windows 上的某些 chrome 快捷方式中,就像一个快速解决方案一样,希望它可以帮助任何人。
归档时间: |
|
查看次数: |
4175 次 |
最近记录: |