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
CTO1
3 years agoExplorer | Level 3
responseUri C# problem
I am trying to set my dropbox configuration to TokenAccessType Offline on my page, but I am getting erros on the "DropboxOAuth2Helper.ProcessCodeFlowAsync"
I get: Dropbox.Api.OAuth2Exception: 'invalid_grant'
for error description I got: redirect_uri mismatch
after I got the page to enter my credentials I call this method:
public async Task<ActionResult> DropboxRedirect(string code, string state)
{
try
{
var connectState = PantherCache.Get(_identityService.User.Guid + "_dropBoxConnectionState").Replace("\"", "");
if (connectState != state)
{
SessionVars.AddNotification(User.Identity.Name, new Notification
{
Type = NotificationType.error,
Text = "There was an error connecting to Dropbox."
});
return this.RedirectToAction("BoxSync");
}
var appKey = ConfigurationManager.AppSettings["dropbox.clientId"].ToString();
var appSecret = ConfigurationManager.AppSettings["dropbox.clientSecret"].ToString();
string redirectUri = Url.Action("DropboxRedirect", "Settings", new { code = code, state = state }, this.Request.Url.Scheme);
Uri responseUri = new Uri(redirectUri);
var response = await DropboxOAuth2Helper.ProcessCodeFlowAsync(responseUri, appKey, appSecret, redirectUri.ToString(), state: connectState);
// OAuth2Response response = await DropboxOAuth2Helper.ProcessCodeFlowAsync(responseUri, appKey, appSecret, state: connectState);
var tenant = await _unitOfWork.TenantRepository.GetTenantByTenantGuid(_identityService.Tenant.Guid).FirstAsync();
var isDropboxIntegrationEnabled = tenant.IsDropboxIntegrationEnabled;
tenant.DropboxAuthToken = Encryptor.Encrypt(response.AccessToken);
tenant.IsDropboxIntegrationEnabled = true;
var res = await _unitOfWork.TenantRepository.InsertOrUpdateAndSaveAsync(tenant);
//if the tenant was not integrated to drop box prior to the update
if (!isDropboxIntegrationEnabled)
{
QueueService.SendDropboxSyncQueueMessage(tenant.Id, null, null, Model.QueueModels.BoxSyncMessageType.InitSync, _identityService.User.Guid);
}
_identityService.ResetSessionVariablesFromCurrentContext(_identityService.User.Name, true, true);
SessionVars.AddNotification(User.Identity.Name, new Notification
{
Type = NotificationType.success,
Text = "This account is now connected to Dropbox!"
});
return this.RedirectToAction("BoxSync");
}
catch (Exception e)
{
SessionVars.AddNotification(User.Identity.Name, new Notification
{
Type = NotificationType.error,
Text = "DropBox Authentication Error. Please try again later or contact support."
});
ErrorHelper.LogErrorManually(e);
return this.RedirectToAction("BoxSync");
}
}
I got the error on this line:
var response = await DropboxOAuth2Helper.ProcessCodeFlowAsync(responseUri, appKey, appSecret, redirectUri.ToString(), state: connectState);
currently my ResponseUri values are: "https://localhost:44383/Settings/DropboxRedirect?code=qX6NI6vbQh4AAAAAAAAAbCeVf-D8ZK916ZSKjp7oc64&state=afeb873a8cb7437dac1da70a59690dab"
I am creating my responseUri wrong? how should I create it?
The "redirect_uri mismatch" error indicates that this call failed because the redirect_uri used on /oauth2/authorize, which is the redirectUri parameter on GetAuthorizeUri in the .NET SDK, to get that authorization code does not match the redirect_uri used when calling /oauth2/token, which is the redirectUri parameter on ProcessCodeFlowAsync in the .NET SDK. These two values need to match exactly. Or, if you didn't set the redirectUri parameter on GetAuthorizeUri you should likewise omit it when calling ProcessCodeFlowAsync as well.
You didn't show your GetAuthorizeUri, but for your ProcessCodeFlowAsync call you seem to be manually constructing your responseUri from your redirectUri value, which isn't the intended way to do this. The responseUri value should be the redirected URI, containing the code, as triggered on your app by Dropbox redirecting the user back to your app after they authorized it. Dropbox would do so if you set the redirectUri parameter on GetAuthorizeUri.
So, if you are not actually using a redirect URI by setting the redirectUri parameter on GetAuthorizeUri to have the user automatically redirected, you should also not set the redirectUri parameter on ProcessCodeFlowAsync. In that case, you can instead use the form of ProcessCodeFlowAsync that takes the "code" parameter directly instead of the "responseUri" parameter.
Alternatively, if you do want to use a redirect URI, make sure you set it using the redirectUri parameter on GetAuthorizeUri and then pass the same redirectUri parameter to ProcessCodeFlowAsync.
- Greg-DBDropbox Staff
The "redirect_uri mismatch" error indicates that this call failed because the redirect_uri used on /oauth2/authorize, which is the redirectUri parameter on GetAuthorizeUri in the .NET SDK, to get that authorization code does not match the redirect_uri used when calling /oauth2/token, which is the redirectUri parameter on ProcessCodeFlowAsync in the .NET SDK. These two values need to match exactly. Or, if you didn't set the redirectUri parameter on GetAuthorizeUri you should likewise omit it when calling ProcessCodeFlowAsync as well.
You didn't show your GetAuthorizeUri, but for your ProcessCodeFlowAsync call you seem to be manually constructing your responseUri from your redirectUri value, which isn't the intended way to do this. The responseUri value should be the redirected URI, containing the code, as triggered on your app by Dropbox redirecting the user back to your app after they authorized it. Dropbox would do so if you set the redirectUri parameter on GetAuthorizeUri.
So, if you are not actually using a redirect URI by setting the redirectUri parameter on GetAuthorizeUri to have the user automatically redirected, you should also not set the redirectUri parameter on ProcessCodeFlowAsync. In that case, you can instead use the form of ProcessCodeFlowAsync that takes the "code" parameter directly instead of the "responseUri" parameter.
Alternatively, if you do want to use a redirect URI, make sure you set it using the redirectUri parameter on GetAuthorizeUri and then pass the same redirectUri parameter to ProcessCodeFlowAsync.
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
5,910 PostsLatest Activity: 3 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!