Need to see if your shared folder is taking up space on your dropbox 👨💻? Find out how to check here.
Forum Discussion
solsubzero
7 years agoHelpful | Level 6
How to use oauth2 for dropbox via Javascript/HTML
Currently I am using the below code, but either No 'Access-Control-Allow-Origin' header or error 400 comes up everytime. I have looked for guides about No 'Access-Control-Allow-Origin' header, but ha...
- 7 years ago
To get user information, such as their name or Dropbox account ID, you should use the /2/users/get_current_account API endpoint. (That's usersGetCurrentAccount in the official Dropbox JavaScript SDK.)
In order to make that API call though, you first need an access token for the user. You can get an access token for the user by sending them through the OAuth app authorization flow (details and links in my previous comment). That involves sending them to the Dropbox web page where they can choose whether or not to authorize your app. Unlike actual API calls, such as /2/users/get_current_account, that step on the www.dropbox.com/oauth2/authorize web page cannot be done programmatically via ajax.
Regarding the issues you mentioned with the 'fetch' line, note that that example is for Node.js. If you're running in front-end browser JavaScript, that won't work. (There's more information on setting up different environments here.)
So, the basic flow would be something like this:
- The user arrives at your app's web page.
- Your app sends the user to the Dropbox app authorization web page.
- The user approves your app.
- Dropbox sends the user back to your app's web page.
- Your app receives the resulting access token.
- Your app uses the access token to make Dropbox API calls as needed, e.g., to retrieve the Dropbox account information.
Greg-DB
Dropbox Community Moderator
7 years agoBased on your code here, it looks like you're attempting to access www.dropbox.com/oauth2/authorize via an ajax call, but it's actually a web page, not an API endpoint, so you shouldn't access it via ajax.
You should send the user to the www.dropbox.com/oauth2/authorize page as a URL in their browser. There, they can choose whether or not to allow the app to connect to their account.
You can find more information on how the OAuth flow works in the OAuth guide. You can find the information for using the www.dropbox.com/oauth2/authorize page in particular in the documentation.
Also, for integrating with the Dropbox API via JavaScript, I recommend using the official Dropbox API v2 JavaScript SDK if you can. It comes with an example of using the app authorization flow.
solsubzero
7 years agoHelpful | Level 6
Apologies, I don't think I explained my problem well.
Currently attempting to authenticate and get a users username via a webpage. This username will correspond to a list of links on that webpage.
Recently I created an app that works fine with communicating to dropbox via flutter and swift code.
I have tried many different solutions such as ajax, but they usually come up with No 'Access-Control-Allow-Origin' header or error 400. I have also tried the official Dropbox API for Javascript, but it seems to have issues with
var fetch = require('isomorphic-fetch');The goal is only to authenticate and grab the users username and that username will be a link between my already created app and the webpage that I am working on.
- Greg-DB7 years ago
Dropbox Community Moderator
To get user information, such as their name or Dropbox account ID, you should use the /2/users/get_current_account API endpoint. (That's usersGetCurrentAccount in the official Dropbox JavaScript SDK.)
In order to make that API call though, you first need an access token for the user. You can get an access token for the user by sending them through the OAuth app authorization flow (details and links in my previous comment). That involves sending them to the Dropbox web page where they can choose whether or not to authorize your app. Unlike actual API calls, such as /2/users/get_current_account, that step on the www.dropbox.com/oauth2/authorize web page cannot be done programmatically via ajax.
Regarding the issues you mentioned with the 'fetch' line, note that that example is for Node.js. If you're running in front-end browser JavaScript, that won't work. (There's more information on setting up different environments here.)
So, the basic flow would be something like this:
- The user arrives at your app's web page.
- Your app sends the user to the Dropbox app authorization web page.
- The user approves your app.
- Dropbox sends the user back to your app's web page.
- Your app receives the resulting access token.
- Your app uses the access token to make Dropbox API calls as needed, e.g., to retrieve the Dropbox account information.
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!