[英]Tring to connect to Azure Digital Twins from Visual Studio Code: DefaultAzureCredential(); .getToken error
我正在尝试从 Visual Studio Code 连接到 Azure Digital wins 实例,使用 Javascript API for Azure Digital Twins。
我安装了 npm install @azure/identity 和 npm install @azure/digital-twins-core,
我可以在 VS Code 的 Powershell 终端中使用“az login”登录到 Azure 门户。 它会找到我的帐户和订阅并登录。
az account get-access-token --output json 返回我的令牌。
这是我试图运行的代码。 它基本上应该在 Azure 数字孪生实例中创建两个模型:
'''
const { DefaultAzureCredential } = require("@azure/identity");
const { DigitalTwinsClient } = require("@azure/digital-twins-core");
const { v4 } = require("uuid");
const { inspect } = require("util");
async function main() {
const modelId = `dtmi:model_${v4()
.split("-")
.join("")};1`;
const componentId = `dtmi:component_${v4()
.split("-")
.join("")};1`;
const digitalTwinId = `digitalTwin-${v4()}`;
const temporaryComponent = {
"@id": componentId,
"@type": "Interface",
"@context": "dtmi:dtdl:context;2",
displayName: "Component1",
contents: [
{
"@type": "Property",
name: "ComponentProp1",
schema: "string"
}
]
};
const temporaryModel = {
"@id": modelId,
"@type": "Interface",
"@context": "dtmi:dtdl:context;2",
displayName: "TempModel",
contents: [
{
"@type": "Property",
name: "Prop1",
schema: "double"
},
{
"@type": "Component",
name: "Component1",
schema: componentId
}
]
};
const temporaryTwin = {
$dtId: digitalTwinId,
$metadata: {
$model: modelId
},
Prop1: 42,
Component1: {
$metadata: {},
ComponentProp1: "value1"
}
};
// AZURE_DIGITALTWINS_URL: The URL to your Azure Digital Twins instance
const url = 'https://ParADIM-ADT-Dev.api.wcus.digitaltwins.azure.net';
if (url === undefined) {
throw new Error("Required environment variable AZURE_DIGITALTWINS_URL is not set.");
}
// DefaultAzureCredential is provided by @azure/identity. It supports
// different authentication mechanisms and determines the appropriate
// credential type based of the environment it is executing in. See
// https://www.npmjs.com/package/@azure/identity for more information on
// authenticating with DefaultAzureCredential or other implementations of
// TokenCredential.
const credential = new DefaultAzureCredential();
const serviceClient = new DigitalTwinsClient(url, credential);
// Create models
const newModels = [temporaryComponent, temporaryModel];
const models = await serviceClient.createModels(newModels);
//console.log(`Created Models:`);
//console.log(inspect(models));
}
main().catch((err) => {
console.log("error code: ", err.code);
console.log("error message: ", err.message);
console.log("error stack: ", err.stack);
});
'''
这是我得到的错误:C:\\Program Files\\nodejs\\node.exe .\\index.js 错误代码:未定义的错误消息:JSON 输入错误堆栈的意外结束:错误:AzureCliCredential.getToken 的 JSON 输入意外结束(c:\\ADTProject\\node_modules@azure\\identity\\dist\\index.js:1529:27) at processTicksAndRejections (internal/process/task_queues.js:95:5) at async DefaultAzureCredential.getToken (c:\\ADTProject\\node_modules@天蓝色\\身份\\dist\\index.js:1358:25)
因此,JSON 输入意外结束。 这个错误来自@azure\\identity\\dist\\index.js,它是 azure\\identity 包的一部分,所以不是我写的代码。 我使用的代码来自教程,并且 JSON 是兼容的。 似乎在执行 getToken 函数时出错。
任何帮助表示赞赏! 我撞到了一堵墙!
谢谢!!
当从文档中提取代码时,查看错误跟踪,它无法从指定的凭据中检索令牌,这可能是我们传递它的方式的问题。 下面是我们通常在 JavaScript 中的连接方式:
const client = new SecretClient(keyVaultUrl, new DefaultAzureCredential());
以下是与 Cosmos 和存储通信的示例代码:
import { SecretClient } from '@azure/keyvault-secrets';
import { DefaultAzureCredential } from '@azure/identity';
import { CosmosClient } from '@azure/cosmos';
const keyVaultUrl = process.env('APP_KEY_VAULT_URI');
const credential = new DefaultAzureCredential();
let storageClient;
let cosmosClient;
async function configureClients() {
const kvClient = new SecretClient(keyVaultUrl, credential);
const storageUri = await client.getSecret('storageUri');
const cosmosDbConnectionString = await client.getSecret('cosmosDb');
cosmosClient = new CosmosClient(cosmosDbConnectonString);
storageClient = new BlobServiceClient(storageUri, credential);
}
有关环境变量的更多信息,设置 keyvault 请参阅 azure sdk 博客以了解身份验证
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.