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: 

Token refreshing C#

Token refreshing C#

Melisek
Explorer | Level 3

I'm building an app in .net and accessing the app's folder for resources. So far everything is going smoothly, but i have no idea how to refresh the token in my app. I tried using the example code from OauthBasic from github, but i have a vague understanding of it and doesn't work for me. Would appreciate some help and guidance with it as i have no idea how to set it up. Thanks in advance.

35 Replies 35

Здравко
Legendary | Level 20

Seems improper argument quoting. Space chars are used as a scopes separator, but the same chars are arguments separators too. Try following version:

System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(authorizeUri.ToString()) {UseShellExecute = true});

I believe, now it's gonna work. :winking_face:

Good luck.

Melisek
Explorer | Level 3

Replaced it and it's still the same (i added the path to the browser). Checked it on another browser just in case, and it persists on there too.

Здравко
Legendary | Level 20

If you mean the missing file, are you certain 'C:\Programming\C#\MelisekGameManager\MelisekGameManager\bin\Debug\net6.0-windows\index.html' exists? :thinking_face: Take a look.

Melisek
Explorer | Level 3

it doesn't exist there.

Melisek
Explorer | Level 3

Ok so i created the index.html in the project and included it into the build folder, and as of now opening still opens that 2 additional tabs for no reason, after completing oauth it opens the site, but the app window doesn't open.

obraz_2022-09-07_200720675.png

I set up some breakpoints to test where the code stops, and it gets stuck on the AcquireAccessToken Task.

Melisek
Explorer | Level 3

Realized there was and index.html in the github repo so copied the code from it and now i get this exception on task.Wait:

System.AggregateException: „One or more errors occurred. (Method not found: 'Void System.Net.Http.HttpClientHandler.CheckDisposedOrStarted()'.)”

InternalException

MissingMethodException: Method not found: 'Void System.Net.Http.HttpClientHandler.CheckDisposedOrStarted()'.

Melisek
Explorer | Level 3

After some tinkering around i finally managed to get everything working! I removed the HttpClient and some lines that were not needed/unwanted and the app is running without any problems. I got the refresh token from the returned message and everything works fine without needing to authorize in the browser. Thank you for your time helping me.

Melisek
Explorer | Level 3

Actually i have one (probably) last question. Why does it throw out a AuthException: missing_scope/.. exception when downloading a file using dbx.Files.DownloadAsync. I'm refreshing for a new token with file.content.read permission as shown below, yet i can't download it. The result of RefreshAccessToken returns true.

I can also read file info like its name.

static async Task Run()
        {
            DropboxCertHelper.InitializeCertPinning();

            Settings.Default.RefreshToken = refreshToken;

            string[] scopeList = new string[3] { "files.metadata.read", "files.content.read", "account_info.read" };

            var config = new DropboxClientConfig("SimplePKCEOAuthApp");

            dbx = new DropboxClient(Settings.Default.AccessToken, Settings.Default.RefreshToken, ApiKey, ApiSecret, config);
            Debug.WriteLine(await dbx.RefreshAccessToken(scopeList));
        }

 

Greg-DB
Dropbox Staff

A 'missing_scope' error indicates that while the app is permitted to use that scope, the particular access token you're using to make the API call does not have that scope granted. Also, be aware that just adding a scope to your app via the App Console does not retroactively grant that scope to existing access tokens.

That being the case, to make any API calls that require that scope, you'll need to get a new access token/refresh token with that scope. That is, you'll need to process the app authorization flow again, starting from GetAuthorizeUri, with the necessary scope specified. Just calling RefreshAccessToken with the added scope won't grant that scope, since you can't automatically get an access token with a scope not actually authorized by the user when creating that refresh token originally.

Refer to the OAuth Guide and authorization documentation for more information.

Melisek
Explorer | Level 3

Now i found out why two additional tabs were opening.

https://www.dropbox.com/oauth2/authorize?response_type=code&client_id=XXXXXXXX&redirect_uri=http%3A%... files.content.read account_info.read&code_challenge_method=S256&code_challenge=XXXX

 

The OauthFlow.GetauthorizeUri returned the url with spaces instead of + in the scopes.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Melisek Explorer | Level 3
  • User avatar
    Здравко Legendary | Level 20
  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?