尝试连接到Azure数字双胞胎从Visual Studio代码:DefaultAzureCredential();.ge



我正在尝试连接到Azure Digital wins实例从Visual Studio Code,使用Azure Digital Twins的Javascript API。

我已经运行了npm install @azure/identitynpm install @azure/digital-twins-core

我可以从VS Code的Powershell终端使用az login登录到Azure门户。它找到我的帐户和订阅并登录。

az account get-access-token --output json返回我的token。

这是我要运行的代码。它应该在Azure Digital Twins实例中创建两个模型:

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 Filesnodejsnode.exe .index.js
error code:  undefined
error message:  Unexpected end of JSON input
error stack:  Error: Unexpected end of JSON input
at AzureCliCredential.getToken (c:ADTProjectnode_modules@azureidentitydistindex.js:1529:27)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async DefaultAzureCredential.getToken (c:ADTProjectnode_modules@azureidentitydistindex.js:1358:25)

JSON输入的意外结束。这个错误来自@azureidentitydistindex.js,它是azureidentity包的一部分,所以不是我写的代码。我使用的代码来自教程,JSON是兼容的。似乎执行getToken函数出错了。

感谢任何帮助!我在这个问题上碰壁了!

当代码从文档中提取时,查看错误跟踪,它无法从指定的凭据中检索令牌,这可能是我们如何传递它的问题。下面是我们通常在JavaScript中连接的方式:

const client = new SecretClient(keyVaultUrl, new DefaultAzureCredential());

下面是与Cosmos和storage通信的示例代码:

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);
}

有关环境变量的更多信息,请参考azure sdk博客了解Authentication

相关内容

  • 没有找到相关文章

最新更新