javascript - Get Dropbox OAuth Token from Code via request.js Fails; Equivalent curl Works -
i'm trying exchange dropbox oauth code token per http api documentation.
when perform command curl thusly:
curl https://api.dropbox.com/1/oauth2/token \ -d code=<authorization code> \ -d grant_type=authorization_code \ -u <app key>:<app secret>
everything works fine, , returned bearer token. unfortunately, seems equivalent code written in node.js request module fails.
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); });
logs:
{ error_description: 'missing required field "grant_type"', error: 'invalid_request' }
the docs in event of 400 error (which is), have:
bad input parameter. error message should indicate 1 , why.
though can seen above code, grant_type
is being specified.
notably docs give second option authenticate, though fails, albeit different message:
description (abridged)
calls /oauth2/token need authenticated using apps's key , secret. these can either passed post parameters (see parameters below) or via http basic authentication. if basic authentication used, app key should provided username, , app secret should provided password.
params
code
string code acquired directing users /oauth2/authorize?response_type=code.grant_type
string grant type, mustauthorization_code
.client_id
string if credentials passed in post parameters, parameter should present , should app's key (found in app console).client_secret
string if credentials passed in post parameters, parameter should present , should app's secret.redirect_uri
string used validate matches original /oauth2/authorize, not used redirect again.
my attempt @ alternate authentication procedure:
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); });
logs:
{ error_description: 'no auth function available given request', error: 'invalid_request' }
in case full response dropbox either of 2 request attemps helpful i posted on pastebin.
i not including redirect_uri
did not use part of code flow. permitted per docs. in case, don't have problems when ommitting in curl
command does succeed.
considering api call succeeds when sent through curl, i'm doing wrong js request. can bearer token expect?
it looks in curl
command, you're sending form-encoded post request (which oauth uses), in node.js code, you're sending json-encoded request.
try form: { ... }
instead of json: { ... }
.
Comments
Post a Comment