Search code examples
node.jsmongodbauthenticationdigest-authenticationmongodb-atlas

How to authenticate at MongoDB Atlas API using digest authentication?


I want to get a list of projects in MongoDB using its API "https://cloud.mongodb.com/api/atlas/v1.0/groups" but every time I get error as "401 You are not authorized for this resource".

According to the docs digest authentication is used.

Seems like I am passing the Private_key and Public_key in the wrong way.

Below is my request object

{
url: 'https://cloud.mongodb.com/api/atlas/v1.0/groups',
method: 'GET',
headers: {
  'Accept': 'application/json',
},
auth: {
  user: 'Public_Key',
  pass: 'Private_key'
  }
}  

Can anyone please help me with this.


Solution

  • What you are missing is the key "sendImmediately". You need to send it in your auth object as follows :

       request({
           method: 'GET',
           auth: {
           "user": Public_Key,
           "pass": Private_key,
           "sendImmediately": false
       },
           url: 'https://cloud.mongodb.com/api/atlas/v1.0?pretty=true'
       })