本文介绍了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注册并获得一个' client-id'和一个 secret ':我已经完成了
  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.

但是,由于身份验证的工作方式,您需要将该网站的脚本托管在与注册客户端时在重定向URL"中提供的URL完全相同的URL上.否则,您会收到一条错误消息重定向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-eeeeeeeeeeeeee .然后,您可以修改上面的示例以检索该文件夹中的所有文档,如下所示:

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-隐式流身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 17:00