Reputation: 51
I'm trying to connect to an Azure Digital wins Instance from Visual Studio Code, using Javascript API for Azure Digital Twins.
I have ran npm install @azure/identity
and npm install @azure/digital-twins-core
.
I can login to Azure portal using az login
from the Powershell terminal in VS Code. It finds my account and subscription and logs in.
az account get-access-token --output json
returns my token.
Here's the code I'm trying to run. It should create two models in the Azure Digital Twins instance:
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);
});
Here's the error I'm getting:
C:\Program Files\nodejs\node.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:\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\@azure\identity\dist\index.js:1358:25)
So, an unexpected end of JSON input. This error is coming from @azure\identity\dist\index.js
which is part of the azure\identity
package, so not code I wrote. The code I'm using is from a tutorial, and the JSON is compliant. Seems it's getting an error executing the getToken
function.
Any help appreciated! I've hit a wall on this one!
Upvotes: 0
Views: 202
Reputation: 1301
As the code pulled from the documentation, looking into the error trace it got failed to retrieve a token from specified credentials, might be issue in how we are passing it. Below is how we usually connect in JavaScript:
const client = new SecretClient(keyVaultUrl, new DefaultAzureCredential());
Below is the sample code to communicate with Cosmos and 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);
}
For more information about environmental variables, setting keyvault refer to azure sdk blog to know about Authentication
Upvotes: 0