We are aware of the issue with the badge emails resending to everyone, we apologise for the inconvenience - learn more here.
Forum Discussion
AGreenTejada
6 years agoHelpful | Level 5
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!"); } }
- AGreenTejadaHelpful | 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-DBDropbox 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.
- HuezzerExplorer | 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!
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
5,877 PostsLatest Activity: 12 months 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!