我试图从MOZ API获取报告。不幸的是,这是我得到的回应:
{
"status" : "503",
"error_message" : "Service Temporarily Unavailable"
}
这是我的代码:
function MozCall(callback) {
var mozCall = '';
// Set your expires times for several minutes into the future.
// An expires time excessively far in the future will not be honored by the Mozscape API.
// Divide the result of Date.now() by 1000 to make sure your result is in seconds.
var expires = Math.floor(Date.now() / 1000) + 300;
var accessId = 'mozscape-b3978adbec';
var secretKey = '5f95cb81c5f121904d012488e28e05e';
// `bitFlagExampleValues` is a list of bitFlag values as strings that we'll
// loop over and sum together using helper function: `sumColumnValues`
var bitFlagExampleValues = [
'144115188075855872',
'68719476736',
'34359738368',
];
var sumColumnValues = function(bitFlagValues) {
return bitFlagValues.reduce(function(accu, bitFlag) {
var accuValBig = new bigJs(accu);
var bitFlagBig = new bigJs(bitFlag);
var bigSum = accuValBig.plus(bitFlagBig);
return bigSum.toString();
}, 0);
};
// 'cols' is the sum of the bit flags representing each field you want returned.
// Learn more here: https://moz.com/help/guides/moz-api/mozscape/api-reference/url-metrics
// returns "144115291155070976"
var cols = sumColumnValues(bitFlagExampleValues);
// Put each parameter on a new line.
var stringToSign = accessId + '\n' + expires;
//create the hmac hash and Base64-encode it.
var signature = crypto
.createHmac('sha1', secretKey)
.update(stringToSign)
.digest('base64');
//URL-encode the result of the above.
signature = encodeURIComponent(signature);
var postData = JSON.stringify(['www.moz.com']);
var options = {
hostname: 'lsapi.seomoz.com',
path: '/linkscape/url-metrics/?Cols=' +
cols +
'&AccessID=' +
accessId +
'&Expires=' +
expires +
'&Signature=' +
signature,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': postData.length,
},
};
var responseData = '';
var req = http.request(options, function(response) {
response.setEncoding('utf8');
response.on('data', function(chunk) {
responseData += chunk;
});
response.on('end', function() {
console.log(responseData);
});
});
req.end();
}
// if (req.user.isPremium == false) {
let freeReportCalls = [MozCall];
// Free user - Single report
// let website = req.body.website0;
async.parallel(freeReportCalls, function(err, results) {
if (err) {
console.log(err);
} else {
console.log(results);
res.render('reports/report', {
title: 'Report',
// bw: JSON.parse(results[0]),
// ps: JSON.parse(results[0]),
// bw: results[1],
// al: results[2],
// moz: results[2],
user: req.user,
});
}
});
我基本上使用了moz文档中的示例代码(在此处找到:https://github.com/seomoz/SEOmozAPISamples/blob/master/javascript/node/batching-urls-sample.js)。我不确定问题是什么。我从昨天开始等待,但问题仍然存在,所以这不是暂时的。
编辑:打破API密钥导致另一个问题,所以我猜测它连接到Moz服务器。
答案 0 :(得分:0)
原来示例代码对我没用。而不是传递选项我只是通过了整个网址,这是有效的。像这样:
var postData = JSON.stringify([website, 'www.google.com']);
console.log(postData);
var options = {
hostname: 'lsapi.seomoz.com',
path: '/linkscape/url-metrics/' + postData + '?Cols=' +
'4' + '&AccessID=' + accessId +
'&Expires=' + expires + '&Signature=' + signature,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': postData.length
}
};
var responseData = "";
// 'http://lsapi.seomoz.com/linkscape/url-metrics/google.com?Cols=4&AccessID='+accessId+'&Expires='+expires+'&Signature='+signature
var req = http.get(`http://lsapi.seomoz.com/linkscape/url-metrics/${website}?Cols=${cols}&AccessID=${accessId}&Expires=${expires}&Signature=${signature}`,function(response) {
response.setEncoding('utf8');
response.on('data', function(chunk) {
responseData += chunk;
});
response.on('end', function() {
console.log(responseData);
});
});
req.end();
Website
是通过视图传递给路由器的变量。