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

Boneless's avatar
Boneless
Helpful | Level 6
4 years ago

How to get team root folder by SwiftyDropbox?

Hi Dropbox Forum,

 

I want to get team root folder by SwiftyDropbox. but fail.

this is what I do

 

1.Set .plist file

 

2.Set AppKey in AppDelegate

 

DropboxClientsManager.setupWithAppKey("xxxxxxx")

 

 

3.authorization(use team account login)

 

        if let _ = DropboxClientsManager.authorizedTeamClient {
            DropboxClientsManager.unlinkClients()
        }
        
        if let _ = DropboxClientsManager.authorizedClient {
            DropboxClientsManager.unlinkClients()
        }

DropboxClientsManager.authorizeFromController(UIApplication.shared,
                                                      controller: self,
                                                      openURL: { (url: URL) -> Void in
                                                        UIApplication.shared.open(url, options: [:], completionHandler: {success in
                                                            if success{
                                                                print("Open URL success")
                                                            }else{
                                                                print("Open URL fail")
                                                            }
                                                        })
        })

 

 

4. then in AppDelegate

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any]) -> Bool

 

        let oauthCompletion: DropboxOAuthCompletion = {
            
            if let authResult = $0 {
                switch authResult {
                case .success:
                    print("Success! User is logged into DropboxClientsManager.")
                    
                    let a = client?.users.getCurrentAccount()
                    a?.response(completionHandler: { user, error in
                        print("user:\(user), error: \(error)")
                        print("accountType:\(user?.accountType)")
                        print("rootNamespaceId:\(user?.rootInfo.rootNamespaceId)")
                    })
                    
                case .cancel:
                    print("Authorization flow was manually canceled by user!")
                    
                case .error(_, let description):
                    print("Error: \(String(describing: description))")
                    
                    let hud = MBProgressHUD.showAdded(to: UIApplication.shared.keyWindow!, animated: true)
                    hud.label.text = "Login failed"
                    hud.hide(animated: true, afterDelay: 1)
                    
                }
            }
        }

DropboxClientsManager.handleRedirectURL(url, completion: oauthCompletion)

 

 

5. Set a button to listFolder

I see this post  "Using the Dropbox-API-Path-Root Header", but don't know how to set it in SwiftyDropbox.

https://www.dropbox.com/lp/developers/reference/dbx-team-files-guide#namespaces

 

func getList(){
let client = DropboxClientsManager.authorizedClient?.withPathRoot(.namespaceId("xxxxxx"))//root_namespace_id get from oauthCompletion client?.users.getCurrentAccount().response(completionHandler: {user, error in print("user:\(user) error:\(error)")})
client?.files.listFolder(path: filePath ,includeMountedFolders: true).response(completionHandler: {response, error in if response?.entries != nil{ self.parseDropboxEntries(entries: response!.entries) } })
}

it show error and can't list folder:

Spoiler

user:nil error:Optional(Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLStringKey=https://api.dropbox.com/2/users/get_current_account, NSErrorFailingURLKey=https://api.dropbox.com/2/users/get_current_account, _NSURLErrorRelatedURLSessionTaskErrorKey=(

    "LocalDataTask <xxxxxxxxxxxxxxxxxxxxxxxxxxx>.<1>"

), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <xxxxxxxxxxxxxxxxxxxxxxxxxxx>.<1>, NSLocalizedDescription=cancelled})

 

Can anyone help me ? thank you

 

 
  • Thank you!

    We solved the problem of setting "withPathRoot" by SwiftyDropbox.

    For other people has the same problem, I record what I do here.

     

    I set class variable as you said(Most important).

    var dropboxClient:DropboxClient?

     

    login then do those things

            dropboxClient = DropboxClientsManager.authorizedClient
            dropboxClient?.users.getCurrentAccount().response(completionHandler: {user, error in
                
                let rootNamespaceId = user?.rootInfo.rootNamespaceId
                if rootNamespaceId != nil{
                    self.dropboxClient = DropboxClientsManager.authorizedClient?.withPathRoot(.namespaceId(user?.rootInfo.rootNamespaceId ?? ""))
                }
                
                self.dropboxClient?.files.listFolder(path: self.filePath).response(completionHandler: {response, error in 
                    
                    //Parse
                    if response?.entries != nil{
                        self.parseDropboxEntries(entries: response!.entries)
                    }
                    
                })
            })

     

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

    I see you're using a 'client' object defined in your 'getList' method. It's possible this client object is going out of scope before the request finishes, which could cause this kind of failure. (API calls are made asynchronously, as they require network calls.) You should keep that client object in scope instead. For instance, you can save it as a variable in the relevant class itself.

     

    For reference, using the withPathRoot method is the right way to set the "Dropbox-API-Path-Root" header in the Dropbox Swift SDK.

    • Boneless's avatar
      Boneless
      Helpful | Level 6

      Thank for your reply.

       

      If I open any Team Scopes and use DropboxClientsManager.authorizeFromController, When my app jump to the Dropbox app forauthorize, it will show error message.

       

      Spoiler

      Error loading app info

      An unknow error occurred

       

      If I close all Team Scopes and don't set withPathRoot.

      let client = DropboxClientsManager.authorizedClient?.withPathRoot(.namespaceId("xxxxxx"))

      I can successful get "member folder" (purple) ,but I want to get "team folders" (blue)

       

      Please tell me how to solve this error, thank you :sob:

       

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

        The "Error loading app info" can occur if you attempt to request scopes that are not enabled for your app. It sounds like your app does not have team scopes enabled, so you should not request them.

         

        You should use withPathRoot to access the team space though. Can you check that you are doing so, and that your "xxxxxx" value is the root namespace ID for the team space? What error/output are you getting for that now?

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,909 PostsLatest Activity: 2 hours ago
333 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!