cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox 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: Net Core 6.0 compatibility? Getting OAuth2Exception: invalid_grant for ProcessCodeFlowAsync...

Net Core 6.0 compatibility? Getting OAuth2Exception: invalid_grant for ProcessCodeFlowAsync...

Tech Dev Oldsmar
Helpful | Level 5
Go to solution

Trying to port a Windows .NET Standard to .NET Core 6.0 using Blazor Server.  I've got the old application running just fine and I'm able to get the initial Authorization Code.  Trying to pass that along with the key and secret, etc... and I get:

 

 

OAuth2Exception: invalid_grant
Dropbox.Api.DropboxOAuth2Helper.ProcessCodeFlowAsync(string code, string appKey, string appSecret, string redirectUri, HttpClient client, string codeVerifier)
docket2022.Pages.AuthDbxToken.ProcessDropboxCallback(string AuthCode) in AuthDbxToken.razor

 

 

 I've made sure all the variables are passing by checking a console....

 

 

 Uri uri = new Uri(NavManager.Uri);
        Console.WriteLine("URI Absolute: " + uri.AbsoluteUri);
        Console.WriteLine("AuthCode: " + AuthCode);
        Console.WriteLine("ApiKeyString: " + ApiKeyString);
        Console.WriteLine("ApiSecretString: " + ApiSecretString);
        Console.WriteLine("Return URL: " + theState);
        State.dbxToken = "Auth Code: " + AuthCode.ToString();
        Console.WriteLine("-------------------------DropboxOAuth2Helper AuthDbxToken.razor--------------------");
        OAuth2Response response = await DropboxOAuth2Helper.ProcessCodeFlowAsync(AuthCode, ApiKeyString, ApiSecretString, theState, null, null);

 

 

Of course I added a localhost redirect URI for my new project.  What is strange is that scope, keys, secrets, etc.... is the same but the .NET Core version keeps getting this error.  I've looked everywhere but I can't see up or down if there has been success or failure with .NET Core 6.0.  

 

1 Accepted Solution

Accepted Solutions

Greg-DB
Dropbox Staff
Go to solution

What does your GetAuthorizeUri call look like? Your GetAuthorizeUri configuration needs to match your ProcessCodeFlowAsync configuration. For example, if you passed a redirectUri to GetAuthorizeUri, you need to also pass that same redirectUri to ProcessCodeFlowAsync. See this sample code as an example.

 

You mentioned using a redirect URI, but if you're not passing it to ProcessCodeFlowAsync in this code, that could cause this error.

View solution in original post

2 Replies 2

Greg-DB
Dropbox Staff
Go to solution

What does your GetAuthorizeUri call look like? Your GetAuthorizeUri configuration needs to match your ProcessCodeFlowAsync configuration. For example, if you passed a redirectUri to GetAuthorizeUri, you need to also pass that same redirectUri to ProcessCodeFlowAsync. See this sample code as an example.

 

You mentioned using a redirect URI, but if you're not passing it to ProcessCodeFlowAsync in this code, that could cause this error.

Tech Dev Oldsmar
Helpful | Level 5
Go to solution

That was it.  Thank you! For reference if anyone is doing this in Blazor-Server (code thinned out to pertinent issue):

 

@page "/dbxAuth"
@using System.Runtime.InteropServices
@using System.Threading.Tasks
@using Dropbox.Api
@inject NavigationManager NavManager

@code
{
    public async Task ProcessDropboxCallback(string AuthCode)
{
// HERE IS WHERE I FIXED THE ISSUE
        var uriBuilder = new UriBuilder(NavManager.Uri);
        uriBuilder.Query = uri.Query.ToString();
        uriBuilder.Fragment = null;
        uriBuilder.Query = null;

 OAuth2Response response = await DropboxOAuth2Helper.ProcessCodeFlowAsync(AuthCode, ApiKeyString, ApiSecretString, uriBuilder.Uri.AbsoluteUri, null, null);
}

}

 

Need more support?