Hi,I sent a request to salesforce , then get a JSON stream. Now I want to save the stream object into a Dropbox file by sending Post request in my code. Does Dropbox have relative Post rest api ? How can I do it?
Thanks for your help!
You can upload data to Dropbox using the /files_put endpoint on the Core API:
https://www.dropbox.com/developers/core/docs#files_put
That endpoint accepts both PUT and POST. You can use the HTTP endpoints, such as the above, directly, but we recommend using an official SDK if possible:
https://www.dropbox.com/developers/core
Otherwise, there are some third party libraries listed here that may be helpful:
https://www.dropbox.com/developers/core/sdks/other
Hi Greg,Thanks very much for your prompt reply. It is very helpful to me.I tried to create an APP using 'Drop-In' Saver type. The APP just give me APP key, but no APP Secret. Before I post my stream data to the /file_put endpoint, I need to get authorized. right ?
My APP is an browser client app. And I found the endpoint '/oauth2/token' (OAuth 2.0) maybe useful to me to get token (without user interactive), but where can I get APP Secret? Or which endpoint is easy to get authorized without user interactive?
Best Regards.
By the way, After I got access token, How to use the token to post request to '/files_put' ? It seems no parameter for access token to the endpoint. Thanks.
To use the Core API, you'll need to register a "Dropbox API app", not a "Drop-ins app":
https://www.dropbox.com/developers/apps/create
Drop-ins don't have app secrets, unlike Dropbox API apps.
And yes, to use the Core API, you'll need to have the user authorize the app to access their account. Then, you can make API calls using the resulting access tokens. (If you only need to connect to your own account, you just need to do this once. You can also easily get an OAuth 2 access token for your own account as shown here: https://blogs.dropbox.com/developers/2014/05/generate-an-access-token-for-your-own-account/ )
Note that getting an access token is a process that involves user intervention, but once you have it, you can store and re-use it for that account without further manual user intervention.
We recommend using an SDK or library as I mentioned before, but if you can't for whatever reason, you can sign the calls using OAuth 1 or OAuth 2. We have blog posts for each:
OAuth 1: https://blogs.dropbox.com/developers/2012/07/using-oauth-1-0-with-the-plaintext-signature-method/OAuth 2: https://blogs.dropbox.com/developers/2013/07/using-oauth-2-0-with-the-core-api/
Hi Greg,Thanks for your patient !I will use the lib "https://github.com/sintaxi/dbox", and try to use my own account (without user interactive).In future, If user can provider dropbox account like 'username/password' , Is there any API to get access token directly without user interaction ?Thanks.
Hi Greg,
Below is another issue:
I created a Dropbox API app, It includes APP key and APP secret now, and generated access token in my APP page. Then I use lib 'dbox' in the browser-client. Below is my JS:
var dboxclient = require('dbox'); var app = dboxclient.app({"app_key": "j4nrv1xhtjj3svq","app_secret":"<REDACTED>"}); var client = app.client("<REDACTED>"); module.exports = DBOX; function DBOX() { } DBOX.prototype.getAccount = function() { client.account(function(status, reply){ console.log(reply) }); } DBOX.prototype.Metadata = function() { // available options... var options = { file_limit : 10000, // optional //hash : ..., // optional list : true, // optional include_deleted : false, // optional //rev : 7, // optional credentials : false, locale : "en", // optional root : "sandbox" // optional }; client.metadata("started.pdf", options, function(status, reply) { console.log(reply); }); } DBOX.prototype.Putfile = function() { client.put("hello.txt", "here is some text", function (status, reply) { console.log(reply); }); }
Then I call it in my another js like this:
var dbox = new DBOX(); dbox.getAccount(); //dbox.Putfile();
It failed with error message in the browser console:
POST https://api.dropbox.com/1/account/info request.js:117 Request.endmain.js:761 Request.endmain.js:345 (anonymous function)browser.js:19 drainQueue localhost/:1 XMLHttpRequest cannot load https://api.dropbox.com/1/account/info. A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'http://localhost:3000' is therefore not allowed access. main.js:422 Uncaught TypeError: Cannot read property 'set-cookie' of undefinedmain.js:422 (anonymous function)events.js:77 EventEmitter.emitrequest.js:61 (anonymous function)events.js:74 EventEmitter.emitresponse.js:92 Response.handlerequest.js:73 xhr.onreadystatechange
Do you any Ideas?
Thank you!
That's a security feature of CORS. I'm not familiar with the dbox library you're using, but you need to make sure you don't use credentials (i.e., the withCredentials XHR field, for example, to set cookies) when making these API calls.
Greg, thanks!I don't familiar with dbox, and don't know how to set config before send request. So I will use the '/files_put' rest. Now The question is how I can attach the access token (generated in my app page) when sending post to '/files_put' ? It seems '/files_put' has not this parameter in Core API document.Thanks.
Just set the Authorization header to Bearer <token>. The documentation doesn't mention this, because it's just how OAuth 2 works.
Authorization
Bearer <token>
BTW, I redacted the access token in your post, but just in case someone else saw it, you should make sure to disable it. Unlinking your app from the https://www.dropbox.com/account#security tab and then relinking (getting a new token) should do the trick.
Thanks, Steve.I will try it when I get chance. Both of you have a nice day.