我正在尝试使用Mendeley API下载附加到资源(文档)的文件。我在这里关注开发者文档:看https://dev.mendeley.com/methods/#downloading-a-file起来它应该是相当直接的。我已经弄清楚了Oauth流程,我能够拉到我所有的文件从Mendeley图书馆。我也能够获得给定文档的附加文件id。
当我试图使用该ID检索文件(在本例中是PDF)时,它失败了。我已经将参数复制到postman,它在那里运行得很好,但是当我尝试在我的Ruby-on-rails应用程序中使用JavaScript时,它失败了。
我是JavaScript的新手,所以我很可能错过了一些明显的东西。
下面是JavaScript代码片段:
downloadFile = (json) => {
this.getAuthCode();
this.provideToken = (token) => {
const access_token = token;
var myHeaders = new Headers();
myHeaders.append("Authorization", `Bearer ${access_token}`);
var requestOptions = {
method: 'GET',
headers: myHeaders,
};
fetch(`https://api.mendeley.com/files/${json[0].id}`, requestOptions).then(resp => {
console.log(resp)
})
}
}
字符串
JSON参数是与文档关联的文件数组。它似乎工作正常,直到Mendeley服务器重定向到有问题文件的AWS S3存储URL。以下是Chrome开发者控制台的输出:
Response {type: "cors", url: "https://mendeley-files.s3.amazonaws.com/31/9f/319f…09d6b8443d1e734fad70c551e37a0a3497b42c69fd0796c91", redirected: true, status: 400, ok: false, …}
body: (...)
bodyUsed: false
headers: Headers
__proto__: Headers
ok: false
redirected: true
status: 400
statusText: "Bad Request"
type: "cors"
url: "https://mendeley-files.s3.amazonaws.com/31/9f/319fe16b678cc65092ef5aa732b3e4c137f272f3?response-content-disposition=attachment%3Bfilename%3D%222003-Psychometric_properties_of_student_ratings_of_instruction_in_online_and_on-campus_courses.pdf%22&response-content-type=application%2Fpdf&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEOz%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCWV1LXdlc3QtMSJIMEYCIQCP0qEL1EJeGKIBVmxX2FaPIZftcmqiW%2FzgDY5ysRs25AIhAMM6q3YzWSubSnuuXOs9gi8voOZ%2BRrkQS343IkihJka1KvYDCDUQAhoMMTA4MTY2MTk0NTA1Igx2zsOuUuv1h6V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z&X-Amz-SignedHeaders=host&X-Amz-Expires=600&X-Amz-Credential=ASIARSLZVEVE5V2CTSJF%2F20200511%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=96b2a6673ab742909d6b8443d1e734fad70c551e37a0a3497b42c69fd0796c91"
__proto__: Response
arrayBuffer: ƒ arrayBuffer()
blob: ƒ blob()
body: (...)
bodyUsed: (...)
clone: ƒ clone()
formData: ƒ formData()
headers: (...)
json: ƒ json()
ok: (...)
redirected: (...)
status: (...)
statusText: (...)
text: ƒ text()
type: (...)
url: (...)
constructor: ƒ Response()
Symbol(Symbol.toStringTag): "Response"
get body: ƒ body()
get bodyUsed: ƒ bodyUsed()
get headers: ƒ headers()
get ok: ƒ ok()
get redirected: ƒ redirected()
get status: ƒ status()
get statusText: ƒ statusText()
get type: ƒ type()
get url: ƒ url()
__proto__: Object
型
它显然找到了正确的文件,但无法下载。当我在Postman中运行它时,它会下载文件。
我试过添加各种标题,显示在 Postman 作为'隐藏',但我得到基本相同的React。对此有任何想法或方向将不胜感激。
1条答案
按热度按时间x6492ojm1#
参考文件:https://dev.mendeley.com/code/core_quick_start_guides.html#file_download
这是为我工作:
字符串