尽管可以使用 CURL,但无法通过解析云代码访问 Dropbox API
Could not access Dropbox API via parse cloud code although works with CURL
我正在尝试从解析云代码访问以下端点:
https://api.dropboxapi.com/2/users/get_current_account
终点详情:
https://www.dropbox.com/developers/documentation/http/documentation#users-get_current_account
发出请求时我的云功能。
return Parse.Cloud.httpRequest({
method: 'POST',
url: 'https://api.dropboxapi.com/2/users/get_current_account',
headers: {
'Authorization': 'Bearer ' + accessToken
}
});
从 Dropbox 收到错误:
Error in call to API function "users/get_current_account": Bad HTTP "Content-Type" header: "application/x-www-form-urlencoded". Expecting one of "application/json", "application/json; charset=utf-8", "text/plain; charset=dropbox-cors-hack"
然后我尝试使用 CURL:
curl -X POST https://api.dropboxapi.com/2/users/get_current_account --header "Authorization: Bearer **access_token"
这成功了,我得到了用户的数据。
根据错误,我修改了云端代码:
return Parse.Cloud.httpRequest({
method: 'POST',
url: 'https://api.dropboxapi.com/2/users/get_current_account',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + newAccessToken
}
});
现在我得到以下错误:
Error in call to API function \"users/get_current_account\": request body: could not decode input as JSON
httpRequest 中没有请求体。然后我尝试将正文设置为 null 和 ''。两者都给出了相同的错误。
首先,我不明白为什么保管箱 API 需要 POST
不带参数的请求。
其次,我该如何解决错误。
非常感谢!
-----更新-----
已尝试错误中的其他两种内容类型,即
'Content-Type': 'application/json;charset=utf-8'
'Content-Type': 'text/plain; charset=dropbox-cors-hack'
仍然得到同样的错误:
Error in call to API function \"users/get_current_account\": request body: could not decode input as JSON
我没有使用 Parse.Cloud.httpRequest
,但它可能会发送带有请求正文的默认内容类型参数。然而,curl
不会发送任何内容类型的默认值,这可能就是它适合您的原因。
从错误消息中,我可以看出它也会接受 text/plain
,所以你可以尝试一下。
'Content-Type': text/plain; charset=dropbox-cors-hack',
我不知道他们为什么在没有传入数据的情况下使用 POST
。
终于来了!这对我有用:
return Parse.Cloud.httpRequest({
method: 'POST',
url: 'https://api.dropboxapi.com/2/users/get_current_account',
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Authorization': 'Bearer ' + newAccessToken
},
body: JSON.stringify(null)
});
我不得不在正文中使用JSON.stringify(null)
。
下面的 link 帮助我确定了解决方案:
我正在尝试从解析云代码访问以下端点:
https://api.dropboxapi.com/2/users/get_current_account
终点详情:
https://www.dropbox.com/developers/documentation/http/documentation#users-get_current_account
发出请求时我的云功能。
return Parse.Cloud.httpRequest({
method: 'POST',
url: 'https://api.dropboxapi.com/2/users/get_current_account',
headers: {
'Authorization': 'Bearer ' + accessToken
}
});
从 Dropbox 收到错误:
Error in call to API function "users/get_current_account": Bad HTTP "Content-Type" header: "application/x-www-form-urlencoded". Expecting one of "application/json", "application/json; charset=utf-8", "text/plain; charset=dropbox-cors-hack"
然后我尝试使用 CURL:
curl -X POST https://api.dropboxapi.com/2/users/get_current_account --header "Authorization: Bearer **access_token"
这成功了,我得到了用户的数据。
根据错误,我修改了云端代码:
return Parse.Cloud.httpRequest({
method: 'POST',
url: 'https://api.dropboxapi.com/2/users/get_current_account',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + newAccessToken
}
});
现在我得到以下错误:
Error in call to API function \"users/get_current_account\": request body: could not decode input as JSON
httpRequest 中没有请求体。然后我尝试将正文设置为 null 和 ''。两者都给出了相同的错误。
首先,我不明白为什么保管箱 API 需要 POST
不带参数的请求。
其次,我该如何解决错误。
非常感谢!
-----更新-----
已尝试错误中的其他两种内容类型,即
'Content-Type': 'application/json;charset=utf-8'
'Content-Type': 'text/plain; charset=dropbox-cors-hack'
仍然得到同样的错误:
Error in call to API function \"users/get_current_account\": request body: could not decode input as JSON
我没有使用 Parse.Cloud.httpRequest
,但它可能会发送带有请求正文的默认内容类型参数。然而,curl
不会发送任何内容类型的默认值,这可能就是它适合您的原因。
从错误消息中,我可以看出它也会接受 text/plain
,所以你可以尝试一下。
'Content-Type': text/plain; charset=dropbox-cors-hack',
我不知道他们为什么在没有传入数据的情况下使用 POST
。
终于来了!这对我有用:
return Parse.Cloud.httpRequest({
method: 'POST',
url: 'https://api.dropboxapi.com/2/users/get_current_account',
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Authorization': 'Bearer ' + newAccessToken
},
body: JSON.stringify(null)
});
我不得不在正文中使用JSON.stringify(null)
。
下面的 link 帮助我确定了解决方案: