cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox 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: 

How to get team root folder by SwiftyDropbox?

How to get team root folder by SwiftyDropbox?

Boneless
Helpful | Level 6
Go to solution

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

 

 
1 Accepted Solution

Accepted Solutions

Boneless
Helpful | Level 6
Go to solution

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

 

View solution in original post

10 Replies 10

Greg-DB
Dropbox Staff
Go to solution

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
Helpful | Level 6
Go to solution

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
Dropbox Staff
Go to solution

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?

Boneless
Helpful | Level 6
Go to solution

Let focus on the first question “team scopes”

 

I  set a new app in App Console and create a simple test project for it.

You can see the code here

https://github.com/hanpo/XenBoxTest

 

You also can see my setting in App Console by images.

Screen Shot 2021-02-03 at 10.06.29 PM.png

Screen Shot 2021-02-03 at 10.06.40 PM.png

 

Screen Shot 2021-02-03 at 10.06.49 PM.png

If I use ScopeRequest(scopeType: .user, scopes: ["account_info.read"], includeGrantedScopes: false) or DropboxClientsManager.authorizeFromController, it will show "Error loading app info" 

 

The test project may let you clearly point where I am doing wrong for authoriztion.

 

Thank you!

Greg-DB
Dropbox Staff
Go to solution

I see, thanks! That's helpful. I see in this sample project that you're requesting a team scope "team_info.read", and it sounds like you have the official Dropbox iOS app installed. When you have the official Dropbox mobile app installed, it will handle the app authorization flow (since the user is likely already signed in there anyway). Unfortunately, it looks like the authorization flow in the official mobile apps doesn't currently work with team scopes in particular though. (I'll bring this up with the team to see if we can get that working.)

 

If you just want to list the contents of any folders (including team folders/spaces) in the user's account though, you don't actually need any team scopes. You just need 'files.metadata.read' (a user scope) to be able to access the metadata of any folder/space in the connected account.

 

So, please just use the user scopes (such as 'files.metadata.read') and let me know what trouble, if any, you're still having with listFolder/listFolderContinue. Thanks!

Boneless
Helpful | Level 6
Go to solution

OK, Team Scopes is all close and only open Individual Scopes.

Screen Shot 2021-02-04 at 12.10.06 PM.png

 

 

Then I modify code and commit it for getting team root folders and files.

You can see the code here
https://github.com/hanpo/XenBoxTest

 

Operation is use method loginAction then call method listAction

 

In method listAction

let clientWithPathRoot = DropboxClientsManager.authorizedClient?.withPathRoot(.namespaceId(rootNamespaceIdString ?? ""))

clientWithPathRoot?.files.listFolder(path: "" ,includeMountedFolders: true).response(completionHandler: {response, error in

                print("response:\(response),error:\(error) ")

            })

I think clientWithPathRoot maybe not get correct object.

So, when listFolder will show error

Spoiler

2021-02-04 12:08:26.656055+0800 XenBoxTest[2036:1742155] Task <845DD768-5EDB-4121-A43B-96302CBCE75F>.<1> load failed with error Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLStringKey=https://api.dropbox.com/2/files/list_folder, NSErrorFailingURLKey=https://api.dropbox.com/2/files/list_folder, _NSURLErrorRelatedURLSessionTaskErrorKey=(

    "LocalDataTask <845DD768-5EDB-4121-A43B-96302CBCE75F>.<1>"

), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <845DD768-5EDB-4121-A43B-96302CBCE75F>.<1>, NSLocalizedDescription=cancelled} [-999]

2021-02-04 12:08:26.666504+0800 XenBoxTest[2036:1742152] Task <845DD768-5EDB-4121-A43B-96302CBCE75F>.<1> finished with error - code: -999

2021-02-04 12:08:26.670393+0800 XenBoxTest[2036:1742153] Task <845DD768-5EDB-4121-A43B-96302CBCE75F>.<1> HTTP load failed (error code: -999 [1:89])

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

    "LocalDataTask <845DD768-5EDB-4121-A43B-96302CBCE75F>.<1>"

), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <845DD768-5EDB-4121-A43B-96302CBCE75F>.<1>, NSLocalizedDescription=cancelled}) 

Any suggestion? thank you

 

 

Greg-DB
Dropbox Staff
Go to solution

This appears to be the same sort of error you were getting earlier, which I commented on in this reply. That is, it sounds like your clientWithPathRoot variable is getting removed before the call can complete. You'll need to make sure you keep that defined for the lifetime of the call.

Boneless
Helpful | Level 6
Go to solution

I try many way to keep client variable and set withPathRoot, but still has error. Is the method "withPathRoot" really work or it has bug?

Thank you!

 

Greg-DB
Dropbox Staff
Go to solution

Yes, the withPathRoot method is the right way to access different roots. I just tried it and it is working for me.

 

Can you clarify what you mean when you say you tried many ways to keep the client variable? What did you use exactly?

 

In my own testing I am able to reproduce the "-999" error you're getting when I only define the client locally. It's fixed when I define it as a class variable though, e.g., as `var client : DropboxClient?`.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Boneless Helpful | Level 6
  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?