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

AGreenTejada's avatar
AGreenTejada
Helpful | Level 5
6 years ago

C# ProcessCodeFlowAsync() invalid_grant error

Hello everyone,

I'm a year young to C# and brand new to the concept of API's. Been having sometime struggling with the OAuth process using the OAuth2Helper class.

While I already have the AppKey and AppSecret from App Console, everytime I use the authorization code from GetAuthorizeURI (no redirectURI, I just copy/paste the authorization code into a WinForm), and push it into ProcessCodeFlowAsync(AuthCode, AppKey, AppSecret); I get an OAuth2Exception invalid_grant error. Anyone knows what's causing this?

Here's the code I'm working on.

 public class Info {
 public static DropboxTeamClient teamClient = new DropboxTeamClient(AccessToken);
        public static DropboxClient admin = new DropboxClient(AccessToken);

        public static string AuthorizationCode { get; set; }
        public static async Task Authorize()
        {
            Uri redirect = new Uri("https://www.dropbox.com/oauth2/");
            var getURL = DropboxOAuth2Helper.GetAuthorizeUri(AppKey);
            System.Diagnostics.Process.Start(getURL.ToString());

            var token = new GetAuthToken();
            token.BringToFront();
            Application.Run(token);

            while (string.IsNullOrEmpty(AuthorizationCode))
            {
                MessageBox.Show("BEEP! NO PARAMETERS GIVEN", "Null Parameter");
                token = new GetAuthToken();
                Application.Run(token);
            }

            OAuth2Response AuthFlow;
            bool i = true;

            while (i)
            {
                try
                {
                    AuthFlow = await DropboxOAuth2Helper.ProcessCodeFlowAsync(AuthorizationCode, AppKey, AppSecret);
                    break;
                }
                catch (OAuth2Exception e)
                {
                    MessageBox.Show("BEEP! INCORRECT CODE GIVEN.", "Wrong Authorization Code");
                    token = new GetAuthToken();
                    Application.Run(token);
                }
            }

            AuthFlow = await DropboxOAuth2Helper.ProcessCodeFlowAsync(AuthorizationCode, AppKey, AppSecret, null,null) ;
            AccessToken = AuthFlow.AccessToken;
            MessageBox.Show(string.Format("The access token is {0}.", AccessToken), "SUCCESS!");

        }
}
  • AGreenTejada's avatar
    AGreenTejada
    Helpful | Level 5

    Hey all,

    Alright, I think I just solved my own problem. Since I was testing ProcessCodeFlow() using try/catch, I couldn't use the same auth code twice in the same process. Eliminating that loop allowed me to change the access code.

    • Greg-DB's avatar
      Greg-DB
      Icon for Dropbox Staff rankDropbox Staff

      Thanks for following up. I'm glad to hear you already sorted this out. Yes, the Dropbox API OAuth 2 authorization codes can only be used once each. 

  • Huezzer's avatar
    Huezzer
    Explorer | Level 4

    Hey!

     

    I know this is an old thread but this may help someone. I was using the API "DropboxOAuth2Helper.ProcessCodeFlowAsync" and got the same error -  "invalid_grant". The solution was to make sure that the redirectUri passed into "GetAuthorizeUri", matches the one passed into "ProcessCodeFlowAsync" if you're doing the whole flow. Then it worked just fine!

     

    Have a nice day everyone!