cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to know more about how Community ideas impact Dropbox development? Check it out 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: 

C# ProcessCodeFlowAsync() invalid_grant error

C# ProcessCodeFlowAsync() invalid_grant error

AGreenTejada
Helpful | Level 5

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!");

        }
}
2 Replies 2

Re: C# ProcessCodeFlowAsync() invalid_grant error

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.

Re: C# ProcessCodeFlowAsync() invalid_grant error

Greg-DB
Dropboxer

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. 

Who's talking

Top contributors to this post

  • User avatar
    Greg-DB Dropboxer
  • User avatar
    AGreenTejada Helpful | Level 5
What do Dropbox user levels mean?
Need more support?