Here Map API 使用 NodeJS 请求令牌

Here Map API request token with NodeJS

我正在尝试使用 NodeJS 向 Here Map API 请求令牌,以获得 OAuth 2.0 令牌凭据。 我在请求级别被阻止并不断出现相同的错误,但根据文档,我没有做错任何事。

这是在 NodeJS 中发出请求的必要代码:

const keyID = "MyKeyID";
const keySecret = "MySecretKey";

const httpMethod = "POST";
const httpUrl = "https://account.api.here.com/oauth2/token";

let oauthNonce = Math.random().toString(36).substring(6);
let oauthTimestamp = Date.now();
let oauthSignatureMethod = "HMAC-SHA256";
let oauthVersion = "1.0";

let baseString = httpMethod + "&" + httpUrl;

let oauth1Param = [];
oauth1Param.oauth_consumer_key = keyID;
oauth1Param.oauth_signature_method = oauthSignatureMethod;
oauth1Param.oauth_signature = "";
oauth1Param.oauth_timestamp = oauthTimestamp;
oauth1Param.oauth_nonce = oauthNonce;
oauth1Param.oauth_version = oauthVersion;

let paramString = "grant_type=client_credentials"
                    + "&oauth_consumer_key=" + oauth1Param.oauth_consumer_key
                    + "&oauth_nonce=" + oauth1Param.oauth_nonce
                    + '&oauth_signature_method=' + oauth1Param.oauth_signature_method
                    + "&oauth_timestamp=" + oauth1Param.oauth_timestamp
                    + "&oauth_version=" + oauth1Param.oauth_version;

baseString += "&" + paramString;

let signingKey = keySecret + "&";

let signature = crypto.createHmac('SHA256', signingKey).update(baseString).digest('base64');

oauth1Param.oauth_signature = signature;

console.log(JSON.stringify(paramString));
console.log(oauth1Param);

let headerOauth = "OAuth ";
for (let key in oauth1Param) {
    headerOauth += key + "="  + oauth1Param[key] + ",";
}

headerOauth = headerOauth.slice(0, -1);
console.log(headerOauth);

var request = require('request');
request.post({
    url : httpUrl,
    headers: {
        'content-type' : 'application/x-www-form-urlencoded',
        'Authorization' : headerOauth
    },
    body : JSON.stringify({
        "grant_type" : "client_credentials"
    })
}, function(error, response, body){
    if(error){
        console.log("------ ERROR -------");
        throw error;
    }
    //console.log(response);
    console.log(body);
});

这是我不断收到的错误:

{"errorId":"ERROR-51fa3a57-1dfa-4da7-b2e9-5f94574a4f75", 
"httpStatus":401, 
"errorCode":401205,
"message":"Unsupported signature method in the header. Require HMAC-SHA256",
"error":"invalid_request",
"error_description":"errorCode: '401205'. Unsupported signature method in the header. Require HMAC-SHA256"}

根据文档,https://developer.here.com/documentation/authentication/dev_guide/topics/error-messages.html,我的签名方法有一个错误,它必须是 HMAC-SHA256 但实际上这是我在代码开头放置的内容: let oauthSignatureMethod = "HMAC-SHA256";

有谁知道如何解决这个问题?

您正在使用的库 request 支持 OAuth 签名。因此代码可以大大缩短:

const request = require('request');

const OAUTH_URL = 'https://account.api.here.com/oauth2/token';
const KEY_ID = '<KEY_ID>';
const KEY_SECRET = '<KEY_SECRET>';

request.post({
  url: OAUTH_URL,
  oauth: {
    consumer_key: KEY_ID,
    consumer_secret: KEY_SECRET,
    signature_method: 'HMAC-SHA256'
  },
  headers: {
    'Content-Type' : 'application/x-www-form-urlencoded',
  },
  form: {
    'grant_type': 'client_credentials'
  }
}, function (e, r, body) {
  console.log(body);
});

至于您的代码片段,JSON.stringify(obj) 不合适,因为它也会对 object 的大括号进行编码。 此外,OAuth header 中的值需要用双引号括起来。 OAuth 很容易出错 :)