如何使用摘要身份验证在 MongoDB Atlas API 进行身份验证?

Kul*_*aha 2 authentication mongodb digest-authentication node.js mongodb-atlas

我想使用其 API“ https://cloud.mongodb.com/api/atlas/v1.0/groups ”获取MongoDB 中的项目列表,但每次我收到错误消息“401 您无权使用此资源” ”。

根据文档摘要认证被使用。

似乎我以错误的方式传递 Private_key 和 Public_key 。

下面是我的请求对象

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

任何人都可以帮我解决这个问题。

小智 5

您缺少的是“立即发送”的关键。您需要将它发送到您的 auth 对象中,如下所示:

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