cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Are you interested in learning how media industry leaders use Dropbox Replay? Register for our webinar here.

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: 

Re: Getting invalid request for PKCEOAuthFlow.ProcessCodeFlowAsync

Getting invalid request for PKCEOAuthFlow.ProcessCodeFlowAsync

donaldp
Collaborator | Level 9

Hi,

 

   I'm implementing PKCE now, but getting an invalid request exception. I can't see anything that I'm doing wrong from the doco (it says everything is optional except code and appkey). This is code that was working when I was using DropboxOAuth2Helper.ProcessCodeFlowAsync (but I want to convert to not sending the secret - using a C# dotnet desktop app).

 

My initial code (using a code authorised in the browser, and the same appkey) is as follows...

```

if (code is object) {
    PKCEOAuthFlow pKCEFlow=new PKCEOAuthFlow();
// OAuth2Response dxResponse=await DropboxOAuth2Helper.ProcessCodeFlowAsync(code,APIKEY,APISECRET); note THIS CODE WAS WORKING
    OAuth2Response dxResponse=await pKCEFlow.ProcessCodeFlowAsync(code,APIKEY);

```

Then I hit the exception...

********************************** UNHANDLED EXCEPTION! Details: Dropbox.Api.OAuth2Exception: invalid_request
at Dropbox.Api.DropboxOAuth2Helper.ProcessCodeFlowAsync(String code, String appKey, String appSecret, String redirectUri, HttpClient client, String codeVerifier)

 

   Do I need to use a different authoriseURI if I'm using PKCE or something? I'm using the same one I was using with DropboxOAuth2Helper.ProcessCodeFlowAsync. Otherwise I don't know what it's not happy about. 😕

 

thanks,

  Donald.

16 Replies 16

donaldp
Collaborator | Level 9

Ah ok. Yes I didn't see that because doco says "Example: Auth URL for code flow with offline token access type", which is what I want - offline token. Needs a qualifier added "except for PKCE".

 

So does the PKCE URL change each time, or I only need to generate it once?

Здравко
Legendary | Level 20

@donaldp wrote:

...

So does the PKCE URL change each time, or I only need to generate it once?


Technically it's possible to be a constant, but as I said before, why are you using PKCE in this case? 🤷 It becomes meaningless and I wouldn't give you such advice.

donaldp
Collaborator | Level 9

I'm using PKCE so that I don't have to send the secret, and is what the doco says to do.

Greg-DB
Dropbox Staff

The PKCE flow is the right choice for a client-side app, such as a desktop app. The PKCE flow can be used with or without a redirect URI.

 

The PKCE flow eliminates the need for the app secret by instead using a code challenge/verifier. That's essentially a temporary secret generated by the app. A redirect URI can be used to return the authorization code to the app, but that's unrelated to the use of a code challenge/verifier.

 

In order for the PKCE flow to be successful, the code challenge/verifier needs to be validated by the Dropbox server. If it's invalid, the process will fail. Specifically, the code_challenge set on the /oauth2/authorize URL used when retrieving the authorization code needs to correspond to the code_verifier sent to /oauth2/token when attempting to exchange that particular authorization code. You can use the PKCEOAuthFlow in the official Dropbox .NET SDK, which will do most of the work for you.

 

When using PKCEOAuthFlow in the Dropbox .NET SDK, you need to make sure you use the same instance for both the GetAuthorizeUri step as well as the ProcessCodeFlowAsync step, as shown in that example. That's because the PKCEOAuthFlow class generates the code challenge/verifier when the instance is created.

donaldp
Collaborator | Level 9

Hi @Greg-DB ,

 

   Thanks for your polite-as-always explanation. 🙂 That wasn't clear to me from the doco that the authorisation URL is different every time (since it's always been the same with non-PKCE), so might be worth highlighting that in the doco for others like me who are converting.

 

   I got it working now with an access token (wanted to try that first so that I can test my code handles it properly when it expires), but I did try to save a refresh token at the same time, as that's what I'll use later, and various info was telling me I would get one with a code authorisation flow, but I didn't get a refresh token, only the access token.  Is there another parameter or something I need to specify, because I thought I'd done everything I needed to get the refresh token too (for offline processing).

 

This is what I have so far...

```

PKCEOAuthFlow=new PKCEOAuthFlow();
AuthorisationURLText=PKCEOAuthFlow.GetAuthorizeUri(OAuthResponseType.Code,APIKEY).ToString(); (plus an option to copy this to the clipboard)

 

OAuth2Response dxResponse=await PKCEOAuthFlow.ProcessCodeFlowAsync(code,APIKEY);
DxAccessToken=dxResponse.AccessToken;
Trace.Write($"********************************** DxAccessToken is {DxAccessToken}\r\n");
if (dxResponse.ExpiresAt is DateTime) {
    DxAccessTokenExpiry=(DateTime)dxResponse.ExpiresAt;
} else {
    DxAccessTokenExpiry=DateTime.Now;
    }
Trace.Write($"********************************** DxAccessTokenExpiry is {DxAccessTokenExpiry}\r\n");
DxRefreshToken=dxResponse.RefreshToken;
Trace.Write($"********************************** DxRefreshToken is {DxRefreshToken}\r\n");
Authorised=true;

```

But as I said, the refresh token came up blank.

 

thanks,

  Donald.

Здравко
Legendary | Level 20

@donaldp wrote:

..., but I didn't get a refresh token, only the access token.  Is there another parameter or something I need to specify, because I thought I'd done everything I needed to get the refresh token too (for offline processing).

...
AuthorisationURLText=PKCEOAuthFlow.GetAuthorizeUri(OAuthResponseType.Code,APIKEY).ToString();

...


Hi @donaldp,

Yes there is such parameter that you skipped. Refresh token is "in pair" with offline processing, but where you are specifying that? There is such a parameter 'tokenAccessType' (set to Legacy by default). Whenever some example uses refresh token the parameter is set to Offline. 😉 Just do the same.

There is a mistake in code comment and documentation that Legacy means long lived token. Sometime ago - Yes, but now it's obsolete.

Greg-DB
Dropbox Staff

@donaldp Здравко is correct, in order to receive a refresh token, you need to specify "offline" access. With the .NET SDK, you would do so in your GetAuthorizeUri call, by setting tokenAccessType: TokenAccessType.Offline, like in the example here. It looks like that's missing in the GetAuthorizeUri call in the latest code you shared.

Need more support?