问题描述
我正在尝试将以下 curl 命令转换为使用来自 node-libcurl 的 curl 的函数调用。当我尝试使用 curl 运行请求时,出现 401 错误。我已经确认当我从终端运行这个 curl 命令时它可以工作,所以我相信错误是我使用 curl 而不是 MongoDB 端的设置或不正确的 curl 命令。
curl --user '{public_key}:{private_key}' --digest \
--header 'Content-Type: application/json' \
--include \
--request POST "https://cloud.mongodb.com/api/atlas/v1.0/groups/{projectId}/customDbroles/roles" --data '
{
"actions" : [{
"action": "FIND","resources" :[{
"db": "my-database"
}]
}],"roleName": "my-new-custom-role"
}'
这是我的卷曲设置。作为旁注 - 我也相信 certFilePath 很好,因为我已经解决了从 curl 获取错误代码 60 的问题:
const { curly } = require('node-libcurl');
const body = {
actions : [{
action: 'FIND',resources :[{
db: 'my-database'
}]
}],roleName: 'my-new-custom-role'
};
const { statusCode,data } = await curly.post(
`https://cloud.mongodb.com/api/atlas/v1.0/groups/${PROJECT_ID}/customDbroles/roles`,{
httpHeader: [
'Content-Type: application/json'
],postFields: JSON.stringify(body),userpwd: `${MONGODB_API_PUBLIC_KEY}:${MONGODB_API_PRIVATE_KEY}`,httpAuth: 'CURLAUTH_DIGEST',caInfo: certFilePath,verbose: true
}
)
if( statusCode !== 200 ) {
error(`Bad statuscode on curl request: ${statusCode}`);
error(`Bad statuscode on curl request: ${JSON.stringify(data,null,2)}`);
} else {
console.log(JSON.stringify(data,2));
}
我执行此操作返回的日志如下:
Host: cloud.mongodb.com
user-agent: node-libcurl/2.3.0
accept: */*
content-type: application/json
content-length: 294
* We are completely uploaded and fine
* old SSL session ID is stale,removing
* Connection state changed (MAX_CONCURRENT_STREAMS == 2147483647)!
< HTTP/2 401
< www-authenticate: Digest realm="MMS Public API",domain="",nonce="l0Safo6pi1MVfAX3zeDSSlXBShRLTQXC",algorithm=MD5,qop="auth",stale=false
< content-type: application/json
< content-length: 106
< x-envoy-upstream-service-time: 1
< date: Sun,17 Jan 2021 17:56:06 GMT
< server: envoy
<
* Connection #0 to host cloud.mongodb.com left intact
[Sun,17 Jan 2021 17:56:06 GMT] Error: Bad statuscode on curl request: 401
[Sun,17 Jan 2021 17:56:06 GMT] Error: Bad statuscode on curl request: {
"error": 401,"reason": "Unauthorized","detail": "You are not authorized for this resource."
}
有没有想过我在卷曲通话中可能把什么东西搞砸了?或者,如果我可以提供更好的日志,请告诉我,我很乐意收集它们。
谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)