'Required Header authorization is missing. Ensure a valid Authorization token is passed in Azure Cosmos DB

messageToCosmos function is working in my dummy project but not here; I am getting

{ Error: Required Header authorization is missing. Ensure a valid Authorization token is passed. ActivityId: 1250064f-f72d-4b73-adb7-4f2bcd91e778, Microsoft.Azure.Documents.Common/2.7.0

Please find messageToCosmos function, you can give it a try i have given my key and end point

const express = require('express')
const bodyParser = require('body-parser')
const mongoose = require('mongoose')
require('dotenv').config();
var fs = require('fs');
var https = require('https');
var http = require('http');
const app = express();
const dbString = 'mongodb://localhost:27017/blue-isSecure';
const messageRoute = require('./routes/messages')
const multer = require('multer');
const path = require('path');
// const fileUpload = require('express-fileupload');
const cors = require('cors')
const cron = require('node-cron')



// cosmos
const CosmosClient = require('@azure/cosmos').CosmosClient;
const config = require('./config')

const endpoint = config.endpoint;
const cosmosKey = config.key;
const client = new CosmosClient({ endpoint, cosmosKey });

const databaseId = config.database.id
const containerId = config.container.containerMessages




const fileStorage = multer.diskStorage({
    destination:(req, file, cb ) =>{
        cb(null,'files')
    },
    filename:(req, file, cb) => {
        cb(null, file.originalname)
    }
})
// app.use(cors)
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended:false}))
 app.use(multer({storage:multer.memoryStorage()}).single('file'))

// app.use(fileUpload({
//     useTempFiles:true
// }))

app.use('/files', express.static(path.join(__dirname,'files')))
app.use('/blue-isSecure',messageRoute)

async function messageToCosmos(itemBody) {
    const { item } = await client.database(databaseId).container(containerId).items.upsert(itemBody);
    console.log(`Created family item with id:\n${itemBody.id}\n`);
  };

  messageToCosmos({message:'hey buddy'}).catch(err =>console.log(err))

mongoose.connect(dbString,{useNewUrlParser:true, useUnifiedTopology:true})
    .then(connect =>{
        app.listen(process.env.PORT || 2020)
        console.log('listening')
    })
    .catch(err => {
        console.log(err)
    })

My Config file

var config = {}

config.endpoint = "https://bluecosmosdb.documents.azure.com:443/";

config.key = "j2EFBfUe1GQpd5Sewv0LXUUluhrjdOuQ0w14JkJYSe4ISVkN9SBBPxq8wynW6bs7QCicbYsB6DaAfVd5Q1K6Nw==";


config.database = {
    id: 'blueSecures'
  }

  config.container = {
    containerMessages: 'messages'
  }

module.exports = config;


Solution 1:[1]

It worked for me:

Uninstall @azure/cosmos and reinstall it using the 2.1.7 version (npm install @azure/[email protected] --save)

refrence link(thanks to @kilmfer91)

Solution 2:[2]

The issue is the OPs code is that they renamed the "key" variable to "cosmosKey". To fix it:

const client = new CosmosClient({ endpoint, key: cosmosKey });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dharmendra Prajapati
Solution 2 Birla