mamcx
mamcx

Reputation: 16196

NSURLErrorDomain Code -999 when call SwiftyDropbox method

I have incorporated the Dropbox API V2, with permission of "App Folder".

The call using curl succed:

curl -X POST https://api.dropboxapi.com/2/files/list_folder \
  --header 'Authorization: Bearer vSBSoEs...............' \
  --header 'Content-Type: application/json' \
  --data '{"path":""}'

However, calling using the latest swift api fail with:

Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLKey=https://api.dropbox.com/2/files/list_folder, NSLocalizedDescription=cancelled, NSErrorFailingURLStringKey=https://api.dropbox.com/2/files/list_folder}

I have followed the steps to incorporate the API, and use this to call the client:

func authDropbox() -> DropboxClient {
    let client = DropboxClient(accessToken: DROPBOX_TOKEN)

    return client
}

enum DropBoxListResult {
    case Files(Array<Files.Metadata>)
    case Error(String)
}

enum DropBoxResult {
    case Result(Files.FileMetadata)
    case Error(Error)
    case PathError
}

func listDropbox(name:String, result: @escaping ((DropBoxListResult) -> Void))
{
    Log.info("List:", name)

    let client = authDropbox()

    client.files.listFolder(path: name).response { response, error in

        if let r = response {
            Log.info(response)
            result(DropBoxListResult.Files(r.entries))
        } else if let error = error {
            Log.error(error)
            result(DropBoxListResult.Error(String(describing: error)))
        }
    }
}

Upvotes: 2

Views: 601

Answers (1)

Sudara
Sudara

Reputation: 5029

I had the same problem and found the solution from https://www.dropboxforum.com/t5/API-support/999-quot-cancelled-quot-Error/td-p/192322

The problem is that the scope of client goes out of scope when the response is received. In your case, you store client in the function listDropbox which goes out of scope when the response is received.

The solution is to store the client object as a variable in the class which does not go out-of-scope after the response is received.

class DropboxApi : NSObject {

    let client: DropboxClient = DropboxClient(accessToken: DROPBOX_TOKEN)

    enum DropBoxListResult {
        case Files(Array<Files.Metadata>)
        case Error(String)
    }

    enum DropBoxResult {
        case Result(Files.FileMetadata)
        case Error(Error)
        case PathError
    }

    func listDropbox(name:String, result: @escaping ((DropBoxListResult) -> Void))
    {
        Log.info("List:", name)

        client.files.listFolder(path: name).response { response, error in

            if let r = response {
                Log.info(response)
                result(DropBoxListResult.Files(r.entries))
            } else if let error = error {
                Log.error(error)
                result(DropBoxListResult.Error(String(describing: error)))
            }
        }
    }
}

Upvotes: 0

Related Questions