有使用CURL添加用户的文档:
https://docs.atlas.mongodb.com/reference/api/database-users-create-a-user/ ,
也许有人已经准备好使用node.js表达代码了?
谢谢!
这是我的尝试:
app.post('/insertUser', function(req, res) {
var user = {
"databaseName": "admin",
"password": "changeme123",
"roles": [{
"databaseName": "sales",
"roleName": "readWrite"
}, {
"databaseName": "marketing",
"roleName": "read"
}],
"username": "david"
}
request.post({ headers: {'content-type' : 'application/json','Accept': 'application/json'},
url: "https://cloud.mongodb.com/api/atlas/v1.0/groups/5d111fdcff7a257766f7fb90/databaseUsers",
data: user ,body:{"admin":"xxxxxx"}},
function(error, response, body){
console.log(body);
});
})
错误消息:
{
"reason" : "Unauthorized",
"error" : 401,
"detail" : "You are not authorized for this resource."
}
编辑2:,这是我的第二次尝试:
app.post('/insertUser', function(req, res) {
var man = {
"databaseName": "evaluetoncoin",
"password": "changeme123",
"roles": [{
"databaseName": "evaluetoncoin",
"roleName": "readWrite"
}, {
"databaseName": "evaluetoncoin",
"roleName": "read"
}],
"username": "david"
}
var authWith = {
user: {
"username": "nicolas",
"apikey": "xxxxxx"
}
}
request.post({ headers: {'content-type' : 'application/json','Accept': 'application/json'},
url: "https://cloud.mongodb.com/api/atlas/v1.0/groups/5d111fdcff7a257766f7fb90/databaseUsers",
data: man ,
body:authWith,
json: true},
function(error, response, body){
console.log(error);
console.log(body);
});
})
仍然出现相同的错误...我的密码正确,GROUP ID也正确(项目ID)。
它缺少数据库名称,但是我不知道在哪里添加它?
是否必须将其放在authWith对象的内部?