使用 Azure 管理进行身份验证时出现 403 错误 API - nodejs

403 error when authenticating with Azure Management API - nodejs

我正在使用以下 npm 尝试使用 NodeJS 创建云服务。 https://www.npmjs.com/package/azure-mgmt-compute

这是我正在使用的代码:

    var file = __dirname + '/azure-cert.pem';

    var computeManagementClient = computeManagement.createComputeManagementClient(computeManagement.createCertificateCloudCredentials({
        subscriptionId: userCredentials.subscriptionId,
        pem: fs.readFileSync(file, 'utf-8')
    }));

我收到错误:"The server failed to authenticate the request. Verify that the certificate is valid and is associated with this subscription"。 状态码 403.

我已经完成了此处所写的内容: https://msdn.microsoft.com/en-us/library/azure/gg551722.aspx

并使用 OpenSSL 转换为 PEM。

我还尝试了以下方法:

openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout azure-cert.pem -out azure-cert.pem

pkcs12 -export -out azure-cert.pfx -in azure-cert.pem -name "My Self Signed Cert"

openssl x509 -outform der -in azure-cert.pem -out azure-cert.cer

一切都失败了。 有什么想法吗?

我尝试按照 https://www.npmjs.com/package/azure-mgmt-compute 页面的 "How to Use -> Authentication " 部分通过 Azure Xplat-CLI 和 运行 示例代码获取 .pem 文件。它工作正常。

首先需要通过命令npm install -g azure-cli安装Azure Xplat-CLI,请参考https://azure.microsoft.com/en-us/documentation/articles/xplat-cli-install/

然后您需要通过命令 azure login -u <username> 连接到您的 Azure 订阅,请参考 https://azure.microsoft.com/en-us/documentation/articles/xplat-cli-connect/

然后你可以命令azure account cert export获取.pem文件。该代码适用于订阅 ID 和 pem 文件。

此致!