Dropbox令牌API会返回“缺少客户端凭据”。在Node.js中 [英] Dropbox token API returns "Missing client credentials" in Node.js
问题描述
我尝试在普通的Node.js中使用Dropbox Core API。
I tried use Dropbox Core API in plain Node.js.
编程为:
- 用户打开授权页面并获取代码。
- 用户将代码输入到应用中。
- 应用将其发送到Dropbox API
- API返回令牌。
- User opens authorize page and get code.
- User inputs the code to app.
- App send it to Dropbox API.
- API returns token.
但是我无法令牌化,API返回错误消息缺少客户端凭据。
But I cannot token and API returns error with the message "Missing client credentials".
- 我的代码在这里: https://gist.github.com/ginpei/65890135d323f18207c0
- 关于API: https://www.dropbox.com/developers/core/docs
- My code is here: https://gist.github.com/ginpei/65890135d323f18207c0
- About API: https://www.dropbox.com/developers/core/docs
我应该如何编写代码以获得令牌?
How should I write code to get token?
谢谢。
编辑,从链接的摘要中添加代码:
EDIT Adding code from the linked gist:
// About API:
// https://www.dropbox.com/developers/core/docs#oa2-authorize
// https://www.dropbox.com/developers/core/docs#oa2-token
var config = require('./config.json');
// OR...
// var config = {
// 'appKey': 'xxxxxxxxxxxxxxx',
// 'secretKey': 'xxxxxxxxxxxxxxx'
// };
var readline = require('readline');
var https = require('https');
var querystring = require('querystring');
// Show authrize page
var url = 'https://www.dropbox.com/1/oauth2/authorize?' +
querystring.stringify({ response_type:'code', client_id:config.appKey });
console.log('Open and get auth code:\n\n', url, '\n');
// Get the auth code
var rl = readline.createInterface(process.stdin, process.stdout);
rl.question('Input the auth code: ', openRequest); // defined below
function openRequest(authCode) {
var req = https.request({
headers: { 'Content-Type': 'application/json' },
hostname: 'api.dropbox.com',
method: 'POST',
path: '/1/oauth2/token'
}, reseiveResponse); // defined below
// ################################
// Send code
// (maybe wrong...)
var data = JSON.stringify({
code: authCode,
grant_type: 'authorization_code',
client_id: config.appKey,
client_secret: config.secretKey
});
req.write(data);
// ################################
req.end();
console.log('Request:');
console.log('--------------------------------');
console.log(data);
console.log('--------------------------------');
}
function reseiveResponse(res) {
var response = '';
res.on('data', function(chunk) { response += chunk; });
// Show result
res.on('end', function() {
console.log('Response:');
console.log('--------------------------------');
console.log(response); // "Missing client credentials"
console.log('--------------------------------');
process.exit();
});
}
推荐答案
这部分代码是错误:
var data = JSON.stringify({
code: authCode,
grant_type: 'authorization_code',
client_id: config.appKey,
client_secret: config.secretKey
});
req.write(data);
您要发送的是JSON编码的正文,但API希望使用表单编码。
You're sending a JSON-encoded body, but the API expects form-encoding.
我个人建议使用类似 request
,以便更轻松地发送表单编码的数据。 (请参阅此处的用法: https://github.com/smarx /othw/blob/master/Node.js/app.js 。)
I'd personally suggest using a higher-level library like request
to make it easier to send form-encoded data. (See my use here: https://github.com/smarx/othw/blob/master/Node.js/app.js.)
但是您应该可以在此处使用querystring编码。只需将 JSON.stringify
替换为 querystring.stringify
:
But you should be able to use querystring encoding here. Just replace JSON.stringify
with querystring.stringify
:
var data = querystring.stringify({
code: authCode,
grant_type: 'authorization_code',
client_id: config.appKey,
client_secret: config.secretKey
});
req.write(data);
这篇关于Dropbox令牌API会返回“缺少客户端凭据”。在Node.js中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!