Announcements
Known issues updated. Learn more

Getting "invalid_access_token" after getting DropboxClientsManager.authorizedClient

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: [slightly smiling face, 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

Re: Getting "invalid_access_token" after getting DropboxClientsManager.authorizedClient

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?

Reply
Loading...

Re: Getting "invalid_access_token" after getting DropboxClientsManager.authorizedClient

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.

Reply
Loading...

Re: Getting "invalid_access_token" after getting DropboxClientsManager.authorizedClient

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] = [slightly smiling face) -> 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
    }
Reply
Loading...
Highlighted

Re: Getting "invalid_access_token" after getting DropboxClientsManager.authorizedClient

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: [slightly smiling face, completionHandler: nil) })
Reply
Loading...

Re: Getting "invalid_access_token" after getting DropboxClientsManager.authorizedClient

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).

Reply
Loading...

Re: Getting "invalid_access_token" after getting DropboxClientsManager.authorizedClient

Helpful | Level 6

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

Reply
Loading...
API Support

Have a question? Our Dropbox Community is here to help you find an answer!
You won’t have to wait too long for a reply, but why not learn more about the Community while you’re here.

Not the right discussion for you? Find out what else our Community has to say about API Support.

Or, search our Help Center for even more.