本文介绍了Mendeley API - 如何使用 JavaScript SDK - 隐式流认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望你不会介意我发布一个问题,因为我不是程序员,需要一个傻瓜"解释.虽然我可以使用基本的 JavaScript,但我从未使用过 API.

I hope you won't mind my posting a question, because I'm not a programmer and need a "for dummies" explanation. Although I can use basic JavaScript, I've never used an API.

这就是我想要做的:

我在我的硬盘上制作了一个 HTML 页面(我最终想将它与 PhoneGap 之类的东西打包成一个移动应用程序,以便我可以与我的同事分享).

I've made an HTML page on my hard-drive (which I ultimately want to package with something like PhoneGap into a mobile app so I can share with my colleagues).

我的页面的一部分,我想填充我在基于 Web 的 Mendeley 参考管理器应用程序中的带注释的引文文件夹的内容.

Part of my page, I want to be populated with the contents of a folder of annotated citations that I have in the web-based Mendeley reference manager application.

目前,我正在用一个 JSON 文件填充我的页面(我从 Mendeley 导出并重新格式化 - 只是为了让我的引文格式代码正确 - 这部分很好,看起来很棒).

At present, I'm populating my page with a JSON file (which I exported from Mendeley and re-formatted - just to get my citation formatting code right - that part is fine, it looks great).

但我不希望我的页面填充以前导出的文件,而是使用我的 Mendeley 文件夹的最新内容在我的应用程序打开时自动下载到我的页面中.

But I'd like my page to populate not with a previously exported file, but with the up-to-date contents of my Mendeley folder automatically downloaded into my page when my app is opened.

我已经研究了 Mendeley api文档",查看了书籍,YouTube 教程,搜索了网络 - 所有使用 api 下载我可以找到的数据的示例,不要帮我弄清楚如何使用 Mendeley api.我什至无法从 Mendeley SDK 中的示例"中理解!

I've laboured over the Mendeley api "documentation", checked out books, YouTube tutorials, searched the web - all the examples of using an api to download data that I can find, don't help me figure out how to use the Mendeley api. I can't even understand from the 'examples' in the Mendeley SDK!

到目前为止,我只能理解(或者可能是误解!):

  1. 向 Mendeley 注册并获得客户 ID"秘密":我已经做到了
  2. 决定我想要什么样的身份验证流程:这将是隐式流程",因为我需要直接从浏览器访问 Mendeley.我明白隐式流程"不需要秘密",只需要客户 ID".
  3. 使用 Mendeley JavaScript SDK:我使用的是独立"版本 - 我在我的页面中使用脚本标记引用它,如自述 SDK 文件中所述.我还将隐式流所需的代码片段复制并粘贴到名为oauth-config.js"的文件中,如 SDK 示例文件中所述:oauth-config.implicit-grant.js.dist
  1. Register with Mendeley and get a 'client-id' and a 'secret': I've done that
  2. Decide what kind of authentication flow I want: this will be 'implicit flow' since I need to access Mendeley directly from the browser. I understand 'implicit flow' doesn't need the 'secret', just the 'client-id'.
  3. Use the Mendeley JavaScript SDK: I'm using the 'standalone' version - I'm referencing it with a script tag in my page, as described in the Readme SDK file. I've also copied and pasted a required snippet of code for implicit flow into a file named 'oauth-config.js', as described in the SDK examples file: oauth-config.implicit-grant.js.dist

但现在怎么办?

  • 我究竟如何使用 SDK 来获取我的数据?我找不到方向.
  • 假设我可以从 Mendeley 获取我的数据,那么处理 JSON 以在我的页面中显示的脚本如何访问响应数据?

推荐答案

让我举一个例子来说明如何使用 Mendeley JS API.这是我使用隐式流程登录并检索用户个人库中的所有文档的简约实现.

Let me give you an example of how to use the Mendeley JS API. Here's my minimalistic implementation of login with implicit flow and retrieving all documents in user personal library.

var sdk = require('@mendeley/api');
var api = sdk({
  authFlow: sdk.Auth.implicitGrantFlow({
    clientId: <YOUR_CLIENT_ID_GOES_HERE>
  })
})

api.documents.list().then(function(docs) {
    console.log('Success!');
    console.log(docs);
}).catch(function(response) {
    console.log('Failed!');
    console.log('Status:', response.status);
});

您可以扩展它以访问 SDK 提供的任何 API.

You can extend this to access any API provided by the SDK.

但是,由于身份验证的工作方式,您需要在您的网站上托管此脚本,其网址与您在注册​​客户时在重定向网址"中提供的网址完全相同.否则,您将收到一条错误消息 重定向 URI 与为此应用程序注册的 URI 不匹配.

However due to the way the authentication works, you need to host this script on your website on the exact same URL as the one you have provided in the "redirect URL" when you registered your client. Otherwise you will get an error message Redirection URI does not match the one registered for this application.

对于问题的第二部分,我相信您是在问如何检索某个 Mendeley 文件夹中的所有文档(如果我理解有误,请纠正我).假设您有一个文件夹,其文件夹 ID aaaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee.然后您可以修改上面的示例以检索该文件夹中的所有文档,如下所示:

For the second part of the question, I believe you are asking how to retrieve all documents within a certain Mendeley folder (correct me if I am understanding this incorrectly). Say you have a folder with a folder id aaaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee. Then you can modify the above example to retrieve all documents in that folder like so:

var sdk = require('@mendeley/api');
var api = sdk({
  authFlow: sdk.Auth.implicitGrantFlow({
    clientId: <YOUR_CLIENT_ID_GOES_HERE>
  })
})

api.documents.list({
    folderId: 'aaaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'
}).then(function(docIds) {
    console.log('Success!');
    console.log(docIds);
}).catch(function(response) {
    console.log('Failed!');
    console.log('Status:', response.status);
});

在这种情况下,响应将包含文档 ID,因此您需要调用 api.documents.retrieve(<DOCUMENT_ID>) API 以获取文档详细信息.

In this case the response will contain the document ids so you will need to call the api.documents.retrieve(<DOCUMENT_ID>) API to get the document details.

这篇关于Mendeley API - 如何使用 JavaScript SDK - 隐式流认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 20:43