Need to see if your shared folder is taking up space on your dropbox 👨‍💻? Find out how to check here.

Forum Discussion

Zatacke's avatar
Zatacke
Explorer | Level 3
5 years ago
Solved

Is there a PKCE Example for a "A client-side web application (pure Javascript)"?

Hi there,

 

As said here (sorry for google webcache, the original page is a 404) in the bottom table

 

If your app is A client-side web application (pure Javascript) It should Use the OAuth code flow with short-lived access tokens with PKCE (no refresh tokens).

 

So far I have failed to implement this. (I'm using dropbox@9.2.0 from npm)

 

As far as I understand, when opening the url received by calling

auth.getAuthenticationUrl

I can either get a redirect back to my App or a code to copy/paste

At first I tried to open that URL in the same window, but after digging into the code of the auth instance, I found it stores 

this.codeChallenge
this.codeVerifier

internally. These values are lost when the page is being reloaded so from what I understand, it's necessary the page remains open.

Hence I decided to open the authentication URL in a new tab without providing a redirect URL, so that I need to copy/paste the code manually (which is quite unintuitive for an end user)

 

In that case I can call

auth.getAccessTokenFromCode

which sends a request to

https://api.dropboxapi.com/oauth2/token?grant_type=authorization_code&code=<snip>&client_id=<snip>&code_verifier=<snip>

In that case I receive an error response (404)

{
"error_description": "invalid code verifier",
"error": "invalid_grant"
}

 

Could you point me in the direction what I'm doing wrong?

An example on how to implement this would greatly be appreciated!

Also I'd prefer to have the user not being forced to copy/paste the token or forcing them to find the correct opened tab in which he's meant to paste the code.

 

For better understanding here's the relevant part of my code:

import { Dropbox } from 'dropbox';

const dbx = new Dropbox({
clientId: DROPBOX_APP_KEY,
});

dbx.auth.getAuthenticationUrl(
null, // redirectUri,
'',
'code',
'offline',
[
'account_info.read',
'files.content.write',
'files.content.read',
],
'user',
true,
)
.then((authUrl) => {
window.open(authUrl);

// eslint-disable-next-line no-alert
const code = window.prompt('Enter your received code', '');

this.dbx.auth.getAccessTokenFromCode(null, code)
.then(({ result }) => {
// I'd expect to receive something to work with here
console.log(result);
});
});

 

  • Thanks for writing this up! Yes, this is already open on GitHub so I'll follow up with you there.

2 Replies

Replies have been turned off for this discussion
  • Zatacke's avatar
    Zatacke
    Explorer | Level 3
    5 years ago

    I just found there is already a bugreport on this on GitHub since end of January.

    I created a (not-working) standalone example and put it in that bugreport.

     

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    5 years ago

    Thanks for writing this up! Yes, this is already open on GitHub so I'll follow up with you there.

About Dropbox API Support & Feedback

Node avatar for 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!