且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Mendeley API-如何使用JavaScript SDK-隐式流身份验证

更新时间:2023-12-03 12:45:16

让我给您一个有关如何使用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.