javascript – 通过request.js从代码中获取Dropbox OAuth令牌失败;等效卷曲工作
作者:互联网
我正在尝试根据http api文档交换我的Dropbox oauth代码以获取令牌.
当我用curl执行命令时:
curl https://api.dropbox.com/1/oauth2/token \
-d code=<authorization code> \
-d grant_type=authorization_code \
-u <app key>:<app secret>
一切正常,我收到了我的持票人令牌.不幸的是,什么
似乎是在node.js中编写的等效代码,请求模块失败.
var request = require("request");
var config = require("./config.json");
request({
url: "https://api.dropboxapi.com/1/oauth2/token",
method: "POST",
auth: {
user: config.client_id,
pass: config.client_secret
},
json: {
code: config.code,
grant_type: "authorization_code"
}
}, function(err, resp, body) {
if (err) throw err;
console.log(body);
});
日志:
{ error_description: 'missing required field "grant_type"',
error: 'invalid_request' }
The docs
如果发生400错误(这是),我有:
Bad input parameter. Error message should indicate which one and why.
虽然从上面的代码可以看出,grant_type正在
指定.
值得注意的是,文档提供了第二种认证选项,尽管这也失败了,
虽然有不同的信息:
Description (abridged)
Calls to /oauth2/token need to be authenticated using the apps’s key and secret. These can either be passed as POST parameters (see parameters below) or via HTTP basic authentication. If basic authentication is used, the app key should be provided as the username, and the app secret should be provided as the password.
Params
code
String The code acquired by directing users to /oauth2/authorize?response_type=code.grant_type
String The grant type, which must beauthorization_code
.client_id
String If credentials are passed in POST parameters, this parameter should be present and should be the app’s key (found in the App Console).client_secret
String If credentials are passed in POST parameters, this parameter should be present and should be the app’s secret.redirect_uri
String Only used to validate that it matches the original /oauth2/authorize, not used to redirect again.
我尝试了备用身份验证过程:
var request = require("request");
var config = require("./config.json");
request({
url: "https://api.dropboxapi.com/1/oauth2/token",
method: "POST",
json: {
code: config.code,
grant_type: "authorization_code",
client_id: config.client_id,
client_secret: config.client_secret
}
}, function(err, resp, body) {
if (err) throw err;
console.log(body);
});
日志:
{ error_description: 'No auth function available for given request',
error: 'invalid_request' }
如果我的两个请求尝试中的任何一个来自Dropbox的完整响应将是有用的I posted it on pastebin.
我不包括redirect_uri,因为我没有将它作为代码的一部分使用
流.根据文档允许这样做.无论如何,我没有任何问题
什么时候在curl命令中省略它确实成功了.
考虑到通过curl发送我的API调用成功,我显然正在做
我的js请求有问题.我该怎么做才能获得持票人令牌
期望?
解决方法:
它看起来像你的curl命令,你发送一个表单编码的POST请求(这是OAuth使用的),但是在你的Node.js代码中,你发送的是一个JSON编码的请求.
尝试使用表单:{…}而不是json:{…}.
标签:dropbox-api,javascript,request,oauth,http-post 来源: https://codeday.me/bug/20190824/1706314.html