You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.
Forum Discussion
Tech Dev Oldsmar
3 years agoHelpful | Level 6
Net Core 6.0 compatibility? Getting OAuth2Exception: invalid_grant for ProcessCodeFlowAsync...
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.
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.
- Greg-DBDropbox Staff
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 OldsmarHelpful | Level 6
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); } }
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
5,895 PostsLatest Activity: 12 days agoIf 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 or Facebook.
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!