Hi,
I'm trying out SwiftyDropbox and following the code given in the blogpost (https://blogs.dropbox.com/developers/2015/05/try-out-swiftydropbox-the-new-swift-sdk-for-dropbox-api-v2/).
When I try and do something (e.g. client.usersGetCurrentAccount()) I get an HTTP error: invalid_access_token
client.usersGetCurrentAccount()
invalid_access_token
Any help would be appreciated.
-Peter
Moved to API forum.
Are you sure the user has completed the auth flow? Maybe check that you have a valid access token with DropboxAuthManager.sharedAuthManager.hasStoredAccessTokens() or by printing DropboxAuthManager.sharedAuthManager.getAllAccessTokens()?
DropboxAuthManager.sharedAuthManager.hasStoredAccessTokens()
DropboxAuthManager.sharedAuthManager.getAllAccessTokens()
Thanks Leah. Yes the user has completed the auth flow. DropboxAuthManager.sharedAuthManager.hasStoredAccessTokens() is trueAnd DropboxAuthManager.sharedAuthManager.getAllAccessTokens() contains my token.
Hmmm... this could also happen if the access token has been revoked. Maybe try Dropbox.unlinkClient() and re-authorizing?
Dropbox.unlinkClient()
That worked. Is there any way to guard/check for a revoked token?
There's no way to know if a token has been revoked until you make an API call. You'll need to look for a 401 error response and then call Dropbox.unlinkClient() to clear the bad tokens.
Hi Leah,I am just following up on this again as I've had a couple of issues along the way. The main issue I've had is not being able to call Dropbox after auth. It happens if it's the first time I auth or if I call Dropbox.unlinkClient() then re-auth. The problem seems to be the same - Dropbox.authorizedClient and DropboxClient.sharedClient are nil.I also didn't like that the 401 was embedded in the error string. I added an extension to make the http code more easily accessible and to handle the unlinking. I've ended up wrapping SwiftyDropbox (https://gist.github.com/p15martin/82cbae8188729f26fbbb).Interested in your thoughts?-Peter
Dropbox.authorizedClient
DropboxClient.sharedClient
nil
Just to clarify, Dropbox a user is authorized by clicking a link in your app which calls Dropbox.authorizeFromController(controller). This presents a web view for the user to login and afterwards, you're calling Dropbox.handleRedirectURL(url) which handles storing the access token for the user. There's no need to set or get access tokens manually - just be sure to call Dropbox.handleRedirectURL(url) in your AppDelegate's <BR /> - application:openURL:sourceApplication:annotation: method (this isn't shown in your gist but I'm assuming you're doing this??). Does that help clear things up?
Dropbox.authorizeFromController(controller)
Dropbox.handleRedirectURL(url)
<BR /> - application:openURL:sourceApplication:annotation:
I'll pass along the feedback about the error object to the team. Thanks!
Thanks. Yes I am calling Dropbox.handleRedirectURL(url) in my app delegate.
Could you also include the code from the view controller where you're calling the DropboxAPI methods? Maybe also include your AppDelegate code? I tested the code from the blog post this morning and it appears to be working okay. Are you doing anything differently for your app?
Here's the code: https://github.com/p15martin/DropboxStreamI can re-create the issue consistently. Grab the token. Call https://api.dropbox.com/1/disable_access_token. Restart the app.
Thanks for the code!
This is odd but I'm seeing nothing wrong. I'm able to link to Dropbox successfully. Here's the console output: https://www.dropbox.com/s/mttaf63iotmhan7/Screenshot%202015-06-17%2011.01.49.png?dl=0
Each time I re-open the app, everything works fine too: https://www.dropbox.com/s/zhvvoj39dup7z4z/Screenshot%202015-06-17%2011.02.11.png?dl=0
If I call /disable_access_token and re-open the app, I'm prompted to log in again, but then everything works as expected. Here's the console output:Init dropboxHas clientToken has expired(note, at this point I'm prompted to re-auth)Success! User is logged into Dropbox.Reconnecting the accountHas clientLeah
/disable_access_token
I wonder if it's an issue with the Dropbox account that you're using to test the app. Are you testing the app with the same account you used to create the app in the Dropbox app console?
In the code I shared I have a fix. In AppDelegate under Dropbox.handleRedirectURL(url), I am calling self.dropbox.verifyConnectionAfterAuth() which reconnects Dropbox.authorizedClient and DropboxClient.sharedClient. Try commenting it out and see if you get the problem.
AppDelegate
self.dropbox.verifyConnectionAfterAuth()
One thing you could do to improve this is in your AppDelegate's - application:openURL:sourceApplication:annotation: try using Dropbox.handleRedirectURL(url) instead of the lower-level method DropboxAuthManager.sharedAuthManager.handleRedirectURL(url). This will set the Dropbox.authorizedClient for you, so you don't need to do it yourself: https://github.com/dropbox/SwiftyDropbox/blob/master/Source/DropboxClient.swift#L67
- application:openURL:sourceApplication:annotation:
DropboxAuthManager.sharedAuthManager.handleRedirectURL(url)
The Dropbox class is really just a helpful wrapper around the DropboxAuthManager for the case where you only expect one account to be authorized at a time.
Dropbox
DropboxAuthManager
That resolves the problem! Thanks!
Oh good. Thanks so much for your feedback too!