gpt4 book ai didi

javascript - 如何在 Cloudant 的 _user 数据库中分配 Angular 色

转载 作者:行者123 更新时间:2023-12-05 00:36:24 25 4
gpt4 key购买 nike

如何向 Cloudant 用户数据库中的用户添加 Angular 色?(_users)
我无法使用 Google 或 Cloudant Docs 解决这个问题。
我看到有人提到 Cloudant _user db,但还没有找到如何使用它。
我已经用 nodejs 和 express 构建了一个 API。

const { CloudantV1 } = require('@ibm-cloud/cloudant');
const { BasicAuthenticator } = require('ibm-cloud-sdk-core');
我能够创建 ApiKeys 并使用 service.putCloudantSecurityConfiguration() 分配 Angular 色。
service.getSecurity() 的结果如下:
cloudant: {
'apikey-01beebbe10ae46ad9e86cc16a2937939': [ '_replicator', '_writer', '_reader' ],
'apikey-3044abd26f324792a8be5809a5521400': [ '_writer', '_reader' ],
'apikey-8d98aa26f1d246f6b736f313bd45d630': [ '_writer', '_reader' ],
'apikey-231cdadcf38945e9ab48125adddc0fdb': [],
'apikey-7ec3ebdd9c5b4aa691685fae251a255d': [ '_writer', '_reader' ],
'apikey-061e3a0ae05d486583dda500ee6685f6': [ '_writer', '_reader' ],
'apikey-0324472243bd4c0da6ea6e9022e102c8': [ '_writer', '_reader' ]
}
当我查看 service.getSessionInformation();结果,我看到:
"result": {
"userCtx": {
"roles": [],
"name": "apikey-01beebbe10ae46ad9e86cc16a2937939"
},
"ok": true,
"info": {
"authentication_handlers": [
"iam",
"cookie",
"default",
"local"
],
"authenticated": "default",
"authentication_db": "bm-cc-us-south-18/users"
}
}
Angular 色数组为空!
我想将这些新的 apikey 凭据与 Pouchdb 一起使用,如下所示:
    localDB.replicate.to(https://<apikey>:<pass>@<cloudantURL>/<dbname>
).on('complete', function () {
// yay, we're done!
}).on('error', function (err) {
// boo, something went wrong!
});
我得到错误:

message: "You are not authorized to access this db."


谢谢你的帮助。

最佳答案

正如您所发现的,Cloudant 允许您创建可以访问一个或多个数据库的 api-key/password 对。您可以为每个数据库设置每个 API key 的权限,就像您已经完成的那样。GET /session端点(您正在使用 service.getSessionInformation() JavaScript 函数访问)不会在 roles 中返回 API key 的权限数组,但不要被推迟 - 您的 API key 确实具有您在第一步中定义的权限,因此您应该能够使用该 API key 正常读取/写入/查询数据。
请注意,此身份验证机制与 CouchDB 样式的 _users 是分开的。数据库。

例如使用 PouchDB

const PouchDB = require('pouchdb')
const db = new PouchDB('test')
const username = 'apikey-somevalue'
const password = 'somepassword'
const hostname = 'myhost.cloudant.com'
const dbname = 'pouch'
const remoteDB = `https://${username}:${password}@${hostname}/${dbname}`

db.sync(remoteDB).on('complete', async function () {
console.log('done')
const docs = await db.allDocs()
console.log(docs)
}).on('error', function (err) {
console.error(err)
});

关于javascript - 如何在 Cloudant 的 _user 数据库中分配 Angular 色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69651388/

25 4 0