【问题标题】:Getting error using bearer authorization to sync PouchDB to Cloudant使用不记名授权将 PouchDB 同步到 Cloudant 时出错
【发布时间】:2019-10-14 02:37:09
【问题描述】:

我是一位经验丰富的 JavaScript 程序员,但不熟悉 PouchDB、Cloudant 和 oAuth。当我尝试使用 Bearer Authorization 同步 PouchDB 和 Cloudant 时,出现错误:

"_reader access is required for this request", status: 401

我正在尝试使用 PouchDB 和 Cloudant 编写一个演示 Web 应用程序。我设置了一个 IBM 帐户。我创建了一项名为 Cloudant-xx 的服务。我选择了身份验证方法:IBM Cloud IAM(我没有选择“and legacy...”)。我去了这个服务的仪表板,1) 创建了一个数据库 (myDatabase),2) 选择了屏幕左侧的 head-profile,然后是 [CORS],并禁用了 CORS(用于开发)。

我回到服务,选择了[服务凭证],并创建了一个新的服务凭证。这给了我一个带有 {"apikey": "ELB4lEVA...IO", ..., "username": "451c...74-bluemix"} 的对象。

我使用 curl 获取访问令牌:

curl -k -X POST \
  --header "Content-Type: application/x-www-form-urlencoded" \
  --header "Accept: application/json" \
  --data-urlencode "grant_type=urn:ibm:params:oauth:grant-type:apikey" \
  --data-urlencode "apikey=ELB4lEVA...IO" \
  "https://iam.cloud.ibm.com/identity/token"

返回{"access_token":"eyJra...6fJns","token_type":"Bearer","expires_in":3600,"expiration":1558965151,"scope":"ibm openid"}

在一小时内,我将访问令牌复制并粘贴到我的 javascript 代码中,运行代码,并收到错误“此请求需要 _reader 访问权限”(见上文)。我找不到在哪里设置 _reader 访问权限。有谁能够帮我?谢谢!

    let localDb;
    const localName = "myDatabase";
    const remoteUrl = "https://451...d974-bluemix.cloudantnosqldb.appdomain.cloud/myDatabase";
    const token = "eyJra...6fJns";
    const syncOptions = {
        live: true,
        retry: true,
        continuous: true,
        ajax: {
            headers: {
                Authorization: "Bearer " + token,
            },
        },
    };
    localDb = new PouchDB(localName);
    localDb.sync(remoteUrl, syncOptions).on('change', function (info) {
        console.log(8888, "change", info);
    }).on('paused', function (info) {
        console.log(8888, "paused", info);
    }).on('active', function (info) {
        console.log(8888, "active", info);
    }).on('denied', function (err) {
        console.log(8888, "denied", err);
    }).on('complete', function (info) {
        console.log(8888, "denied", info);
    }).on('error', function (err) {
        console.log(8888, "error", err);
    });
    console.log(4444, localDb);

在我看到的控制台中:

4444 PouchDB {__opts: {…}, auto_compaction: undefined, prefix: "_pouch_", name: "myDatabase", _adapter: "idb", …}
index.js:194 

451...d974-bluemix.cloudantnosqldb.appdomain.cloud/:1 GET https://451...d974-bluemix.cloudantnosqldb.appdomain.cloud/myDatabase/ 401
index.js:192

8888 "error" CustomPouchError {error: "unauthorized", reason: "_reader access is required for this request", status: 401, name: "unauthorized", message: "_reader access is required for this request", …}

【问题讨论】:

    标签: javascript oauth pouchdb cloudant


    【解决方案1】:

    我回答了我自己的问题。原来我在关注过时的例子。在 PouchDB 7.0 中,{ajax: {headers: {}} 已替换为 fetch: function (url, opts) {}。另外,我必须使用 PouchDB 实例而不是远程数据库的字符串。

    https://pouchdb.com/api.html#create_database

    远程数据库选项:

    fetch(url, opts):拦截或覆盖HTTP请求,您可以添加或修改与http请求相关的任何标头或选项,然后返回一个新的fetch Promise。

    现在可以了。

        let localDb;
        const localName = "myDatabase";
        const remoteUrl = "https://451...74-bluemix.cloudantnosqldb.appdomain.cloud/myDatabase";
        const token = "eyJ...448";
        const remoteOptions = {
            fetch: function (url, opts) {
                opts.headers.set("Authorization", "Bearer " + token);
                return PouchDB.fetch(url, opts);
            }
        };
        const syncOptions = {
            live: true,
            retry: true,
            continuous: true,
        };
        localDb = new PouchDB(localName);
        const remoteDb = new PouchDB(remoteUrl, remoteOptions);
        localDb.sync(remoteDb, syncOptions).on("change", function (info) {
            console.log(8888, "change", info);
        }).on("paused", function (info) {...})...
    

    【讨论】:

      猜你喜欢
      • 2014-12-23
      • 1970-01-01
      • 1970-01-01
      • 2016-03-29
      • 2013-08-01
      • 1970-01-01
      • 2014-10-27
      • 1970-01-01
      • 2020-08-02
      相关资源
      最近更新 更多