You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.

Forum Discussion

RJKinsman's avatar
RJKinsman
Helpful | Level 6
6 years ago

Getting "invalid_access_token" after getting DropboxClientsManager.authorizedClient

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!

 

 

  • RJKinsman's avatar
    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?

    • Greg-DB's avatar
      Greg-DB
      Icon for Dropbox Staff rankDropbox Staff

      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.

      • RJKinsman's avatar
        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
            }

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.5,917 PostsLatest Activity: 11 days ago
334 Following

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 or Facebook.

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!