cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox 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!");

        }
}
3 Replies 3

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
Dropbox 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
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! 

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Huezzer Explorer | Level 4
  • User avatar
    Greg-DB Dropbox Staff
  • User avatar
    AGreenTejada Helpful | Level 5
What do Dropbox user levels mean?