cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Share your feedback on the Document Scanning Experience in the Dropbox App right 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: 

Facing problems getting the Access Token from a user on Windows Form

Facing problems getting the Access Token from a user on Windows Form

Gonzo345
Helpful | Level 5
Go to solution

Hi there:

 

I actually asked this on StackOverflow but I got no answer yet. The thing is that I'm trying to generate the access token from the user but I can't "capture" the generated token and I'm afraid that asking for permission generates a new access token everytime, so the previous one remains invalid.

 

The point is that my code is not even entering on the try catch part. I'm still guessing what am I doing wrong.

 

private void Start(string appKey, WebBrowser w)
{
this.oauth2State = Guid.NewGuid().ToString("N");
//This one finally sha token on the WebClient
Uri authorizeUri2 = DropboxOAuth2Helper.GetAuthorizeUri(appKey);
//This one just shown a blank screen (I guess it stucks on the redirect)
//Uri authorizeUri = DropboxOAuth2Helper.GetAuthorizeUri(OauthResponseType.Token, appKey, redirectUrl, state: oauth2State);
w.Navigate(authorizeUri2);
}

private void Browser_Navigating(object sender, WebBrowserNavigatingEventArgs e) { if (!e.Url.ToString().StartsWith(redirectUrl, StringComparison.InvariantCultureIgnoreCase)) { // we need to ignore all navigation that isn't to the redirect uri. return; } try { OAuth2Response result = DropboxOAuth2Helper.ParseTokenFragment(e.Url); if (result.State != this.oauth2State) { // The state in the response doesn't match the state in the request. return; } this.AccessToken = result.AccessToken; this.Result = true; } catch (ArgumentException) { // There was an error in the URI passed to ParseTokenFragment } finally { e.Cancel = true; this.Close(); }
}

The complete code is on StackOverflow. Sorry for disturbing, but I would really appreciate some help here understanding what's happening. I'm getting too much headaches with this problems related with OAuth2 :disappointed_relieved:

 

Thanks in advance,

Gonzo345.

1 Accepted Solution

Accepted Solutions

Greg-DB
Dropbox Staff
Go to solution

You can retrieve an access token from the OAuth flow either using or not using a redirect URL, as you saw, but we recommend using a redirect URL when you can, as it streamlines the process.

 

(For reference, if you do copy/paste the code, note that that's an "authorization code" and not an "access token". You need to exchange it for an access token.)

 

Anyway, for the redirect version, it looks like you're seeing the event for the load of the /authorize page itself. That one should be ignored, as you're actually looking for the load after the user clicks to authorize the app, and is sent to the redirect URI. Is that event not firing at all for you?

 

Also, I notice you're using the "Navigating" event. You may want to try the "Navigated" event instead to see if that works better in your case. (We had a blog post about this here, although it wasn't written for use with the newer .NET SDK itself.)

View solution in original post

5 Replies 5

Greg-DB
Dropbox Staff
Go to solution
Where does it fail exactly? You mentioned it doesn't get to the try/catch, so a good next step in debugging that would be to check if the Browser_Navigating method gets called at all. If it does, check what's happening with that if condition at the beginning of it.

Greg-DB
Dropbox Staff
Go to solution
(By the way, you should switch back to the original authorizeUri line to test the desired redirect flow.)

Gonzo345
Helpful | Level 5
Go to solution

Hi Greg:

 

I used the overloaded DropboxOAuth2Helper.GetAuthorizeUri(appKey) 

Uri authorizeUri2 = DropboxOAuth2Helper.GetAuthorizeUri(appKey);

and I got a token which I manually copied but the DropboxClient constructor says there is an AuthException.

 

At this point I'm manually going to the 

https://www.dropbox.com/1/oauth2/authorize?response_type=code&client_id=appKey

on the main browser and granting the access, copying and pasting the showing token as well, with same ending: AuthException. At the Applications section on the user's account I can see it says "pending".

 

I guess is here

OAuth2Response resultTest = DropboxOAuth2Helper.ParseTokenFragment(webBrowser1.Url);
this.AccessToken = resultTest.AccessToken;

where magic happens, but then... what is that token which is being shown on screen used for?

 

It's not a problem for me asking the user for manually copying and pasting the token, but I hope I can get a working one lol:sob:

 

Thanks for all in advance. There must be something stupid that I'm doing wrong...

Gonzo345.

 

P.S: the appKey is "2ehnws5uxtu4unm" so I use the

https://www.dropbox.com/1/oauth2/authorize?response_type=code&client_id=2ehnws5uxtu4unm 

link, grant access and get a useless token:disappointed_relieved:

 

EDIT: Switching back to the original AuthorizeUri as you stated, I've noted that if I access to that link I posted with the appKey, it doesn't get registered as a development user... and one more thing: the Enum "OauthResponseType.Token" on the GetAuthorizeUri method... is it probablly misspeled? Since I needed to change it to OAuthResponseType.Token :thinking:

 

Debugging the conditional on Browser_Navigating after logging in:

 

if (!webBrowser1.Url.ToString().StartsWith(redirectUrl, StringComparison.InvariantCultureIgnoreCase))
{
        // we need to ignore all navigation that isn't to the redirect uri.
        return;
}
e.Url = {https://www.dropbox.com/1/oauth2/authorize?client_id=2ehnws5uxtu4unm&redirect_uri=https:%2F%2Fwww.dropbox.com%2F1%2Foauth2%2Fredirect_receiver&response_type=token&state=b58e5da520cc481e879900c1d6ffdeb6}
webBrowser1.Url = {https://www.dropbox.com/1/oauth2/authorize?response_type=token&client_id=2ehnws5uxtu4unm&redirect_uri=https:%2F%2Fwww.dropbox.com%2F1%2Foauth2%2Fredirect_receiver&state=b58e5da520cc481e879900c1d6ffdeb6}

So of course none of them StartsWith(redirectUrl), which is https://www.dropbox.com/1/oauth2/redirect_receiver and all I get is a blank screen and no way to apply the DropboxOAuth2Helper.ParseTokenFragment method. If I manually try to apply the ParseTokenFragment debugging it, of course throws an ArgumentException.

 

Anyway, debugging looks like the authorizeUri is correctly formed:

authorizeUri = https://www.dropbox.com/1/oauth2/authorize?response_type=token&client_id=2ehnws5uxtu4unm&redirect_uri=https:%2F%2Fwww.dropbox.com%2F1%2Foauth2%2Fredirect_receiver&state=155fb42d3bef4908b5ae6cff14b5cc0f

 

 

EDIT 2: This is getting totally mindblowing... I've tried the previous token that it was invalid (this one)

https://www.dropbox.com/1/oauth2/authorize?response_type=code&client_id=2ehnws5uxtu4unm

and IT'S WORKING on this project, which uses DotNetBox but not with the official Dropbox API, which basically says "invalid_access_token" . Somebody please throw some light here because I'm getting kinda' crazy with all this Token things.

 

 

 

I've already tried the WPF "Simple_test" and it's working, but is there any Windows Forms example working? I can see that my redirect_uri is not working, basically. It gets stuck and says "page not found 404". Could it be something related with the WebBrowser? I've tried modifying some options with no luck...

Greg-DB
Dropbox Staff
Go to solution

You can retrieve an access token from the OAuth flow either using or not using a redirect URL, as you saw, but we recommend using a redirect URL when you can, as it streamlines the process.

 

(For reference, if you do copy/paste the code, note that that's an "authorization code" and not an "access token". You need to exchange it for an access token.)

 

Anyway, for the redirect version, it looks like you're seeing the event for the load of the /authorize page itself. That one should be ignored, as you're actually looking for the load after the user clicks to authorize the app, and is sent to the redirect URI. Is that event not firing at all for you?

 

Also, I notice you're using the "Navigating" event. You may want to try the "Navigated" event instead to see if that works better in your case. (We had a blog post about this here, although it wasn't written for use with the newer .NET SDK itself.)

Gonzo345
Helpful | Level 5
Go to solution

Holy s***, that was it. I changed the Navigated event and it was like magic...

 

I feel like a dumb, but it's finally working. It feels really weird, but it was worthy. I guess I wasn't thinking clear due to working all the time on this glitchy thing.

 

Thanks Greg!

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Gonzo345 Helpful | Level 5
  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?