We’re Still Here to Help (Even Over the Holidays!) - find out more here.
Forum Discussion
Melisek
4 years agoExplorer | Level 3
Token refreshing C#
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 fr...
Melisek
4 years agoExplorer | 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.
Здравко
4 years agoLegendary | Level 20
If you mean the missing file, are you certain 'C:\Programming\C#\MelisekGameManager\MelisekGameManager\bin\Debug\net6.0-windows\index.html' exists? 🤔 Take a look.
- Melisek4 years agoExplorer | Level 3
it doesn't exist there.
- Melisek4 years agoExplorer | 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.
I set up some breakpoints to test where the code stops, and it gets stuck on the AcquireAccessToken Task.
- Melisek4 years agoExplorer | 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()'.
- Melisek4 years agoExplorer | 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.
- Melisek4 years agoExplorer | 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-DB4 years ago
Dropbox Community Moderator
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. - Melisek4 years agoExplorer | 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%2F%2F127.0.0.1%3A52475%2Fauthorize&state=XXXXX&token_access_type=offline&scope=files.metadata.read 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.
- Melisek4 years agoExplorer | Level 3
I'm trying to replace the spaces with + but it ignores it. Can't generate a refresh token with the scopes because of that.
RedirectUri.ToString().Replace(" ", "+") - Здравко4 years agoLegendary | Level 20
Easiest workaround would be proper quoting. 😉 (As would be on command line)
- Melisek4 years agoExplorer | Level 3
You mean as a char? If yes then i tried that too.
- Здравко4 years agoLegendary | Level 20
I mean something like:
"\"" + RedirectUri.ToString() + "\""It works for me. 😉 In such a way the space char comes scopes separator only, but not arguments separator.
- Melisek4 years agoExplorer | Level 3
Nvm i did it in the wrong line.
I replaced the chars in the redirectUri for the token result, not where i open the browser and pass in the url.
but now i get this:
Exception thrown: „Dropbox.Api.OAuth2Exception” in Dropbox.Api.dll
Exception thrown: „Dropbox.Api.OAuth2Exception” in System.Private.CoreLib.dll
invalid_grant: Error: {0} - Greg-DB4 years ago
Dropbox Community Moderator
Melisek An 'invalid_grant' error typically indicates an misconfiguration of the parameters for the OAuth flow, such as an incorrect authorization code, mismatched redirect URI, etc. You should be able to get a more specific error message like this:
try { // ... your ProcessCodeFlowAsync call ... } catch (OAuth2Exception ex) { // ... Console.WriteLine(ex.ErrorDescription); // ... } - Melisek4 years agoExplorer | Level 3
It prints out a redirect_uri mismatch
- Greg-DB4 years ago
Dropbox Community Moderator
Melisek That indicates that the redirect URI you're supplying when exchanging the authorization code doesn't match the one used to retrieve it originally. Make sure you're supplying the same redirectUri value to both GetAuthorizeUri and ProcessCodeFlowAsync.
- Melisek4 years agoExplorer | Level 3
I had a mismatch of variables because of letter capitalization. Now i get the refresh token, acces token, uid, expiration date and the scopes returned, yet i still get the 400 Bad request error when calling client functions.
- Melisek4 years agoExplorer | Level 3
I copied the code from the repo.
They are named almost the same
and they will be temporary as i don't need oauth on users for my app.
private static readonly Uri RedirectUri = new Uri(LoopbackHost + "authorize"); var redirectUri = await HandleJSRedirect(http); - Melisek4 years agoExplorer | Level 3
I'm catching an HttpRequestException using this
catch(HttpRequestException ex) { Debug.WriteLine(ex.ToString()); }trying to get acces using this
private async Task<List<string>> GetUpdateList(string dirPath) { List<string> list = new List<string>(); var files = await dbx.Files.ListFolderAsync(dirPath); fileNames.Clear(); foreach (var item in files.Entries.Where(i => i.IsFile)) { fileNames.Add(item.Name); list.Add(item.Name[..^4]); } return list; }And it logs
System.Net.Http.HttpRequestException: Response status code does not indicate success: 400 (Bad Request).
at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
at Dropbox.Api.DropboxRequestHandler.RefreshAccessToken(String[] scopeList)
at Dropbox.Api.DropboxRequestHandler.CheckAndRefreshAccessToken()
at Dropbox.Api.DropboxRequestHandler.RequestJsonStringWithRetry(String host, String routeName, String auth, RouteStyle routeStyle, String requestArg, Stream body)
at Dropbox.Api.DropboxRequestHandler.Dropbox.Api.Stone.ITransport.SendRpcRequestAsync[TRequest,TResponse,TError](TRequest request, String host, String route, String auth, IEncoder`1 requestEncoder, IDecoder`1 responseDecoder, IDecoder`1 errorDecoder)
at MelisekGameManager.RWindow.GetUpdateList(String dirPath) in C:\Programming\C#\MelisekGameManager\MelisekGameManager\RWindow.cs:line 167
at MelisekGameManager.RWindow.gameSelect_SelectionChangeCommitted(Object sender, EventArgs e) in C:\Programming\C#\MelisekGameManager\MelisekGameManager\RWindow.cs:line 231 - Здравко4 years agoLegendary | Level 20
'dbx' object seems not properly initialized.
Take a look how have you initialized it.
- Melisek4 years agoExplorer | Level 3
Yep, i was setting a different refresh token after i requested one from the api. Now everything works perfectly! Thanks for your time.
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
If 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, Facebook or Instagram.
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!