简体   繁体   中英

How do I form an SAS token for Microsoft Azure API Management's REST API in Node.js?

I am using Microsoft Azure API Management service and want to use the REST API service. In creating my SAS token, which is needed otherwise the API call doesn't authorize, I'm having difficulty forming a proper token. Microsoft's webpage about this SAS token for API Management only shows an example in C#. I want to know how to form an SAS token in Node.js, which is not shown. Below is my code that was working last week, but is not now for some unknown reason. The error I get is: 401 Authorization error, token invalid

If someone can help me formulate this token, I would appreciate it.

This is Microsoft's webpage regarding this authentication token: https://docs.microsoft.com/en-us/rest/api/apimanagement/apimanagementrest/azure-api-management-rest-api-authentication

Here's my code:

const crypto = require('crypto');
const util = require('util');

const sign = () => {
  const id = ${process.env.id}
  const key = `${process.env.SASKey}`;
  const date = new Date();
  const newDate = new Date(date.setTime(date.getTime() + 8 * 86400000));
  const expiry = `${newDate.getFullYear()}${
    newDate.getMonth() < 10
      ? '' + newDate.getMonth() + 1
      : newDate.getMonth() + 1
  }${newDate.getDate()}${newDate.getHours()}${
    newDate.getMinutes() < 10
      ? '0' + newDate.getMinutes()
      : newDate.getMinutes()
  }`;
  const dataToSignString = '%s\n%s';
  const dataToSign = util.format(dataToSignString, ${id}, expiry);
  const hash = crypto
    .createHmac('sha512', key)
    .update(dataToSign)
    .digest('base64');

  const encodedToken = `SharedAccessSignature ${id}&${expiry}&${hash}`;
  console.log(encodedToken);
  return encodedToken;
};

Try the code:

protected getAPIManagementSAS(){

    let utf8 = require("utf8")
    let crypto= require("crypto")

    let identifier = process.env.API_IDENTIFIER;
    let key = process.env.API_KEY;

    var now = new Date;
    var utcDate = new Date(now.getUTCFullYear(),now.getUTCMonth(), now.getUTCDate() , now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds(), now.getUTCMilliseconds());

    let expiry = addMinutes(utcDate,1,"yyyy-MM-ddThh:mm:ss") + '.0000000Z'

    var dataToSign = identifier + "\n" + expiry;
    var signatureUTF8 = utf8.encode(key); 
    var signature = crypto.createHmac('sha512', signatureUTF8).update(dataToSign).digest('base64'); 
    var encodedToken = `SharedAccessSignature uid=${identifier}&ex=${expiry}&sn=${signature}`;   

    return encodedToken

}

For more information, see here .

After a million tries, it seems like the only format acceptable right now is: SharedAccessSignature uid=${identifier}&ex=${expiry}&sn=${signature}

If you are using the other format that has the "integration" parameter, that's a hit or a miss, mostly miss though. Set the uid as "integration" if that's your identifier and follow the above format as it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM