cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
What’s new: end-to-end encryption, Replay and Dash updates. Find out more about these updates, new features and more 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: 

responseUri C# problem

responseUri C# problem

CTO1
Explorer | Level 3
Go to solution

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&st...

I am creating my responseUri wrong? how should I create it?

 

 

1 Accepted Solution

Accepted Solutions

Greg-DB
Dropbox Staff
Go to solution

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.

View solution in original post

1 Reply 1

Greg-DB
Dropbox Staff
Go to solution

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.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?