这里用NodeJS映射API请求令牌。

问题描述 投票:1回答:1

我试图用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.comdocumentationauthenticationdev_guidetopicserror-messages.html。我在签名方法上出了一个错误,它必须是。HMAC-SHA256 但实际上,这是我已经把我的代码的开头。let oauthSignatureMethod = "HMAC-SHA256";

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

node.js oauth oauth-2.0 http-post here-api
1个回答
2
投票

你正在使用的库。请求,支持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) 是不合适的,因为它将对对象的大括号也进行编码.此外,OAuth头中的值需要用双引号包围。OAuth很容易出小问题:)

© www.soinside.com 2019 - 2024. All rights reserved.