cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Tell us what you want to see on the Community here!
Close

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Can't get access token uses javascript fetch request

Highlighted

Can't get access token uses javascript fetch request

Explorer | Level 3

Hi everyone, l try to get access token uses javascript fetch in the next way:

method: 'POST',
code: AUTHORIZATION_CODE,
grant_type: 'authorization_code',
redirect_uri: `${REDIRECT_URI}${window.location.pathname}`,
headers: {
'Content-Type': "application/x-www-form-urlencoded"
},
CLIENT_ID: CLIENT_SECRET
})
.then(res => res.json())
.then(data => console.log('access data =>', data))
.catch(err => console.log('access err =>',err));

I receive AUTHORIZATION_CODE after successful redirecting from 
"https://www.dropbox.com/oauth2/authorize..." domain.

But I have an err like
{"error_description": "No auth function available for given request", "error": "invalid_request"}

What am I doing wrong? Because I follow curl request example from here:
https://www.dropbox.com/developers/documentation/http/documentation#oauth2-token

curl https://api.dropbox.com/oauth2/token \
    -d code=<AUTHORIZATION_CODE> \
    -d grant_type=authorization_code \
    -d redirect_uri=<REDIRECT_URI> \
    -u <APP_KEY>:<APP_SECRET>
Thanks!

9 Replies 9
Highlighted

Re: Can't get access token uses javascript fetch request

Dropboxer

[Cross-linking for reference: https://stackoverflow.com/questions/64178976/cant-get-access-token-in-dropbox-uses-javascript-fetch-... ]

 

It looks like there's a few issues with how you're formatting your request. For instance, it looks like you're not using the right encoding, and you're not properly labeling your client ID and secret. Here's a fixed up example of how you can do this:

const params = new URLSearchParams({
    code: AUTHORIZATION_CODE,
    grant_type: 'authorization_code',
    redirect_uri: REDIRECT_URI,
    client_id: CLIENT_ID,
    client_secret: CLIENT_SECRET
});

fetch('https://api.dropboxapi.com/oauth2/token', {
    method: 'POST',
    body: params
})
.then(res => res.json())
.then(data => console.log('access data =>', data))
.catch(err => console.log('access err =>',err));
Highlighted

Re: Can't get access token uses javascript fetch request

Explorer | Level 4

Hey Greg!

I thought I'd just join the discussion here since I have a very similar question. Can you provide an example of how to format a fetch request for creating a file request (documentation's here: https://www.dropbox.com/developers/documentation/http/documentation#file_requests-create). Thanks!

Highlighted

Re: Can't get access token uses javascript fetch request

Dropboxer

@cascadeCommons The /2/file_requests/create endpoint is an "RPC" style endpoint, so it does work a bit differently than the /oauth2/token endpoint. 

 

Calling /2/file_requests/create with fetch, for instance, would look like this, again building on the original example here:

var headers = {
    'Authorization': "Bearer ACCESS_TOKEN",
    'Content-Type': "application/json"
};
var params = JSON.stringify({"title": "file request title", "destination": "/file request destination"});

fetch('https://api.dropboxapi.com/2/file_requests/create', {
    method: 'POST',
    body: params,
    headers: headers,
})
.then(res => res.json())
.then(data => console.log('access data =>', data))
.catch(err => console.log('access err =>', err));

 

 

Highlighted

Re: Can't get access token uses javascript fetch request

Explorer | Level 4

Thank you so much Greg for your quick reply, I really appreciate it.

It seems that I had formatted my fetch request correctly, but the issue I was having was working with the response data. I checked my code using the  .ok property and I can confirm that the request is successful. In the example you provided, there were 2 .then() statements. I was wondering if you could provide some guidance on how to access properties of the response, such as the file request URL. For refernce, this is what the HTTP Documentation says should be returned.

dbxFileReq.JPG

Highlighted

Re: Can't get access token uses javascript fetch request

Dropboxer

@cascadeCommons Yes, I was just following the basic result/error handling pattern from the original post here, but you can handle the results/errors however you want. For reference, the API itself will return the result or error for an RPC style call in the HTTPS response body. A successful call will return the result as JSON in the response body, for instance. (And, speaking of which, the original code here is technically incomplete as it doesn't account for error scenarios where the result isn't JSON. Apps should check the type of the response to handle it accordingly.)

 

But, anyway, to answer your actual question, you can access the 'url' property from the parsed JSON like this:

.then(data => console.log('created file request URL:', data.url))
Highlighted

Re: Can't get access token uses javascript fetch request

Explorer | Level 4

 

let options = {
	method: 'POST',
	headers: headers,
	body: params
};
fetch('https://api.dropboxapi.com/2/file_requests/create', options)
	.then(res => res.json())
	.then(data => {
		generated = data.url;
	})

Hey @Greg-DB, I've already initialized the `generated` variable. This fetch is part of a normal Javascript function, and that function returns the value of generated. i know that the fetch response goes through (as mentioned before), but I'm still unable to get the request URL. Is there any mistake you can see here?

Highlighted

Re: Can't get access token uses javascript fetch request

Dropboxer

@cascadeCommons I don't see anything obviously wrong here, but I don't see where you're actually returning or reading the 'generated' value, or are handling any potential errors, so there may be an issue in the parts of the code that were omitted.

Highlighted

Re: Can't get access token uses javascript fetch request

Explorer | Level 4
let requestURL = generateSubmission();

Hey @Greg-DB. I've included some more information.

 

The function I provided earlier is generateSubmission(). I've included how I called it above. My plan was for generateSubmission() to return the URL in string form so I could store it in the newly declared variable `requestURL`. Please let me know if you see anything wrong, any help would be greatly appreciated.

 

Thank you so much!

Highlighted

Re: Can't get access token uses javascript fetch request

Dropboxer

@cascadeCommons It's still not clear where the value actually gets returned. In any case, we've strayed pretty far from the original topic here though, so to avoid spamming the original poster, please open a new thread with all of the relevant code needed to reproduce the issue if you still need help with it.

Work Smarter with Dropbox

The way we work is changing. Share and discover new ways to work smarter with Dropbox in our community.

Sound good? Let's get started.
Who's talking

Top contributors to this post

What do Dropbox user levels mean?
Need more support?