cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Back again with another edition of 'How we use Dropbox', so find out how Emma uses to-do lists to get it all done 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: 

Getting "invalid_access_token" after getting DropboxClientsManager.authorizedClient

Getting "invalid_access_token" after getting DropboxClientsManager.authorizedClient

RJKinsman
Helpful | Level 6

I'm doing something wrong in the Auth flow for my iOS app, but after looking at the photo watch sample app, I can't figure out where I'm going wrong. The problem is, I always get an invalid_access_token when trying to access a file, even though I always get an authorized client.,

I have this in my AppDelegate:

 

 DropboxClientsManager.setupWithAppKey("fawy4f14021rypr")
.
.
.
if DropboxClientsManager.authorizedClient != nil { print("We have an authorized client. Now let's see if we can get the file attributes")

After that, I try to access a file, like this...

 

             client!.files.getMetadata(path: "/Nvelopes.csv")
                .response {response, error in
                    if let (_) = response {
                        dropboxGo = true
                        self.dispatchGroup.leave()
                                        }
                    else if let error = error {
                        print("Did not find Nvelopes.csv in Dropbox:")
                        print(error.description)
                        self.dispatchGroup.leave()
                                            }
             }

 

... which always results in an invalid_access_token, which looks like this:

 

 

[request-id 04ea10d76b6ff63d66aedb01b3d761be] API auth error - {
    ".tag" = "invalid_access_token";
}

So I then go through the auth flow like this...

 

 

        // Begin the Dropbopx authorization flow
        DropboxClientsManager.authorizeFromController(UIApplication.shared, controller: self, openURL: { (url: URL) -> Void in UIApplication.shared.open(url, options: [:], completionHandler: nil) })

I understand this is a bit disjointed.  If it helps, I will be glad to post all the code in context.

Thanks!

 

 

6 Replies 6

Re: Getting "invalid_access_token" after getting DropboxClientsManager.authorizedClient

RJKinsman
Helpful | Level 6

I have an update.  Suddenly the attempt to get file attributes after getting the client started working.  I don't know how to explain it.

Do I need to save and use the same access token?  If yes, how do I pass it back into the API?

Re: Getting "invalid_access_token" after getting DropboxClientsManager.authorizedClient

Greg-DB
Dropboxer

It looks like you're using the SwiftyDropbox SDK. You should save and re-use an access token once you get one, but the SDK does this for you automatically.

Can you confirm you configured your project and implemented the authorization flow exactly as documented? You didn't share your code for the "Handle redirect back into SDK" step in particular.

In any case, note that while access tokens currently don't expire on their own, they can be revoked by the app or user at any time. For that reason, you should make sure your app is able to catch an AuthError at any time. If the access token is invalid, you should throw it away and prompt the user to authorize your app again, if they still want to use the integration.

Re: Getting "invalid_access_token" after getting DropboxClientsManager.authorizedClient

RJKinsman
Helpful | Level 6

Thanks, Greg.  I implemented the authorization flow as documented, to the best of my ability.  Here is my application function where I handle the redirect back into the SDK:

    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        // Set up the viewcontroller variable
        
        var nvelopesViewController: UIViewController
        self.window = UIWindow(frame: UIScreen.main.bounds)
        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        if let authResult = DropboxClientsManager.handleRedirectURL(url) {
            switch authResult {
            case .success(let token):
                print("Success! User is logged into Dropbox with token: \(token)")

                // Set the regular ViewController
                nvelopesViewController = mainStoryboard.instantiateViewController(withIdentifier: "sbNvelopes") as UIViewController
                
                // Display the selected View
                self.window?.rootViewController = nvelopesViewController
                self.window?.makeKeyAndVisible()
            case .cancel:
                print("Authorization flow was manually canceled by user!")
            case .error(_, let description):
                print("Error: \(description)")
            }
        }
        return true
    }

Re: Getting "invalid_access_token" after getting DropboxClientsManager.authorizedClient

RJKinsman
Helpful | Level 6

Also, here is my code in my ConnectToDropboxViewController where I begin the authorization flow:

    @IBAction func connectToDropboxButtonTapped(_ sender: Any) {
        // Begin the Dropbopx authorization flow
        DropboxClientsManager.authorizeFromController(UIApplication.shared, controller: self, openURL: { (url: URL) -> Void in UIApplication.shared.open(url, options: [:], completionHandler: nil) })

Re: Getting "invalid_access_token" after getting DropboxClientsManager.authorizedClient

Greg-DB
Dropboxer

Thanks! I don't see anything clearly wrong here. 

It sounds like it's working now, but let me know if you are able to reproduce the issue again unexpectedly. 

For reference, you should be able to intentionally cause that by e.g., unlinking the app, or deleting the app folder (if you're using an app with the app folder permission).

Re: Getting "invalid_access_token" after getting DropboxClientsManager.authorizedClient

RJKinsman
Helpful | Level 6

Great, thanks for the advice.  Yes, working is a good thing.

Poll
We love to learn from the educators who use Dropbox. Whether you teach kids, teens, adults or a combination of all three, we want to know what apps and integrations you use with Dropbox to help with teaching. Which of the ones below is your favorite, or most used tool?
Who's talking

Top contributors to this post

  • User avatar
    RJKinsman Helpful | Level 6
  • User avatar
    Greg-DB Dropboxer
What do Dropbox user levels mean?
Need more support?