We Want to Hear From You! What Do You Want to See on the Community? Tell us here!

Forum Discussion

Michael-jamf's avatar
Michael-jamf
Helpful | Level 6
3 years ago
Solved

When using upload/uploadsession I get a sessionDeinitialized from alamofire in Swift

When I try to send a test file using either upload or uploadSession I get a sessionDeinitialized error.

I edit the code to generate a file that is larger or smaller than the chunksize to test both of the conditions. This is all the code I have for dropbox. the testDropbox function is referenced in the ContentView where I pass the refreshtoken and appkeysecret. My team has this working in python, all with the same basic functionality.

 

Here is the code

 

import Foundation
import SwiftyDropbox

func testDropbox(token: String, secret: String){
    let session = DropboxManager(refreshToken: token, appKeySecret: secret)
    session.upload(file: NSHomeDirectory() + "/tmp/test2.txt", to: "/uploads")
}

func createDropboxToken(refreshToken:String, appKey:String) -> String {
    
    // Create the command for refreshing the token
    let dbRefreshCMD = "curl https://api.dropbox.com/oauth2/token -s -d grant_type=refresh_token -d refresh_token=\"\(refreshToken)\" -u \"\(appKey)\""
    
    // Execute the command and parse the response
    let task = Process()
    let outputPipe = Pipe()
    task.launchPath = "/bin/sh"
    task.arguments = ["-c", dbRefreshCMD]
    task.standardOutput = outputPipe
    task.launch()
    let outputData = outputPipe.fileHandleForReading.readDataToEndOfFile()
    let res = try! JSONSerialization.jsonObject(with: outputData, options: []) as! [String: Any]
    print(res)
    
    // Print a message indicating that the Dropbox access token has been updated
    print("\nUpdated Dropbox Access Token")
    
    // Create a DropboxClient using the new access token and return it
    let dropboxToken = res["access_token"] as! String
    print(dropboxToken)
    return dropboxToken
}

struct DropboxManager {
    let dbxClient: DropboxClient
    let accessToken: String
    let chunkSize: UInt64
    
    init(refreshToken dbToken: String, appKeySecret dbKey: String) {
        chunkSize = (4 * (1024 * 1024))
        accessToken = createDropboxToken(refreshToken: dbToken, appKey: dbKey)
//        dbxClient = DropboxClient(accessToken: self.accessToken)
        dbxClient = DropboxClient(accessToken: self.accessToken)
        
    }
    
    func upload(file filePath: String, to dbxLocation: String) {
        let fileURL = URL(filePath: filePath)
        let fileName = fileURL.lastPathComponent
        let randomSize = 5 * 1024 * 1024
        if (FileManager.default.createFile(atPath: filePath, contents: Data(count: randomSize), attributes: nil)) {
            print("File created successfully.")
        } else {
            print("File not created.")
        }
        let fileSize = try? FileManager.default.attributesOfItem(atPath: filePath)[.size] as? uint64
//        Check to see if file is smaller than chunksize
//        FIXME: now getting this error ---- Only get this error if I add user to dropboxclient creation in init
//        [request-id 5e4b4893ca1b4ace944138a9a1f4f399] Bad Input: Error in call to API function "files/upload_session/start": Unexpected select user header. Your app does not have permission to use this feature
        if fileSize! < chunkSize {
            dbxClient.files.upload(path: "\(dbxLocation)/\(fileName)", input: fileURL).response(completionHandler: { response, error in
                if let response = response {
                    print("File uploaded: \(response)")
                } else {
                    print("Error upload session: \(error!)")
                }
            })
            .progress { progressData in print(progressData) }
            print("small file")
        } else {
//            start the upload session
            let session = dbxClient.files.uploadSessionStart(input: fileURL)
                .response(completionHandler: { response, error in
                    if let result = response {
                        print(result)
                        var offset: UInt64 = 0
//                        Append chunks to file
                        while (offset <= (fileSize! - self.chunkSize)) {
                            //FIXME: sessionDeinitialized
                            self.dbxClient.files.uploadSessionAppendV2(cursor: Files.UploadSessionCursor(sessionId: result.sessionId, offset: offset), input: fileURL)
                                .response {response , error in
                                    if let response = response {
                                        print("File appended: \(response)")
                                    } else {
                                        print("Error appending data to file upload session: \(error!)")
                                    }
                                }
                                .progress { progressData in print(progressData) }
                            offset += self.chunkSize
                        }
//                        Finish upload with last chunk
                        self.dbxClient.files.uploadSessionFinish(cursor: Files.UploadSessionCursor(sessionId: result.sessionId, offset: fileSize!), commit: Files.CommitInfo(path: "\(dbxLocation)/\(fileName)"), input: fileURL)
                            .response { response, error in
                                if let result = response {
                                    print(result)
                                } else {
                                    print(error!)
                                }
                            }
                            .progress { progressData in print(progressData) }
                    } else {
                        // the call failed
                        print(error!)
                    }
                })
        }
    }
}

 

  • Michael-jamf's avatar
    Michael-jamf
    3 years ago

    Okay managed to get it working with the examples that Dropbox has on their github. With no duplicate views. I think that was something on my end where the app wasnt actually closed or something.

    This is for MacOS. If you want to use iOS I would recommend looking here. It worked out of the box for iOS.

    ContentView.swift

     

    import SwiftUI
    import SwiftyDropbox
    import AppKit
    
    struct ContentView: View {
    
        func myButtonInControllerPressed() {
            // OAuth 2 code flow with PKCE that grants a short-lived token with scopes, and performs refreshes of the token automatically.
            let scopeRequest = ScopeRequest(scopeType: .user, scopes: ["account_info.read"], includeGrantedScopes: false)
            DropboxClientsManager.authorizeFromControllerV2(
                sharedApplication: NSApplication.shared,
                controller: nil,
                loadingStatusDelegate: nil,
                openURL: {(url: URL) -> Void in NSWorkspace.shared.open(url)},
                scopeRequest: scopeRequest
            )
        }
        
        var body: some View {
            VStack {
                Button {
                    myButtonInControllerPressed()
                } label: {
                    Text("Test Dropbox Auth")
                }
            }.frame(maxWidth: .infinity, maxHeight: .infinity)
    
        }
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }

     

    YOURAPPNAMEapp.swift

     

    import SwiftUI
    import SwiftyDropbox
    
    @main
    struct dropboxClientApp: App {
        
        @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
        
        var body: some Scene {
            WindowGroup {
                ContentView()
            }
        }
    }
    
    class AppDelegate: NSObject, NSApplicationDelegate {
        func applicationDidFinishLaunching(_ aNotification: Notification) {
            DropboxClientsManager.setupWithAppKeyDesktop("app-key")
            NSAppleEventManager.shared().setEventHandler(self,
                                                             andSelector: #selector(handleGetURLEvent),
                                                             forEventClass: AEEventClass(kInternetEventClass),
                                                             andEventID: AEEventID(kAEGetURL))
        }
        
        @objc
        func handleGetURLEvent(_ event: NSAppleEventDescriptor?, replyEvent: NSAppleEventDescriptor?) {
            if let aeEventDescriptor = event?.paramDescriptor(forKeyword: AEKeyword(keyDirectObject)) {
                if let urlStr = aeEventDescriptor.stringValue {
                    let url = URL(string: urlStr)!
                    let oauthCompletion: DropboxOAuthCompletion = {
                        if let authResult = $0 {
                            switch authResult {
                            case .success:
                                print("Success! User is logged into Dropbox.")
                            case .cancel:
                                print("Authorization flow was manually canceled by user!")
                            case .error(_, let description):
                                print("Error: \(String(describing: description))")
                            }
                        }
                    }
                    DropboxClientsManager.handleRedirectURL(url, completion: oauthCompletion)
                    // this brings your application back the foreground on redirect
                    NSApp.activate(ignoringOtherApps: true)
                }
            }
        }
    }

     

     

5 Replies

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    3 years ago

    According to the Alamofire documentation, sessionDeinitialized means:

    Session which issued the Request was deinitialized, most likely because its reference went out of scope.

    There's also a description about it here:

    Session was invalidated without error, so it was likely deinitialized unexpectedly. \
    Be sure to retain a reference to your Session for the duration of your requests.

    So, it sounds like your client object was released before the request could be completed; to fix that you'd need to update your code to keep your client object around while the request(s) complete. (Note that the requests are performed asynchronously.)

  • Michael-jamf's avatar
    Michael-jamf
    Helpful | Level 6
    3 years ago

    All the examples I have seen so far do not mention keeping a session alive.

    An example from 2015.

    The github docs to try out the api.

    A github gist.

     

    When I try to add `await` to the uploadstart all I get is "No 'async' operations occur within 'await' expression"  or when I add it inside for the appendV2 I get "Cannot pass function of type '(Files.UploadSessionStartResult?, CallError<Files.UploadSessionStartError>?) async -> Void' to parameter expecting synchronous function type"

     

    Even tried the create folder and download

    func createFolder(path: String) {
            dbxClient.files.createFolderV2(path: path).response { response, error in
                if let response = response {
                    print(response)
                } else if let error = error {
                    print(error)
                }
            }
        }
        
        func downloadFile(path: String) {
            dbxClient.files.download(path: path).response { response, error in
                if let response = response {
                    print(response)
                } else if let error = error {
                    print(error)
                }
            }
        }

    What am I missing?

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    3 years ago

    This isn't a matter of changing your code to use 'async'/'await' operations. This issue occurs when the client variable itself is deinitialized, which severs the network connection performing the API call.

     

    In typical cases, the app would use the authorizeFromControllerV2 method and get the client from the authorizedClient singleton like in the readme, which would make this easier. I see you're not using that flow though; you can certainly create and manage your own client object as you do, but you'll need to make sure you keep a reference to it for the entire lifetime of the requests so that they can complete successfully.

  • Michael-jamf's avatar
    Michael-jamf
    Helpful | Level 6
    3 years ago

    Ah gotcha. I was reading the doc and it seemed like if I just used DropboxClient with a shortlived token, it would do that. I have since rewritten the code, different from how the docs have it since I was not having any luck getting that to work. Edited this code to be macOS.

    Now to figure out why it creates a duplicate window. Think it has something to do with the NSView but not sure.

     

    For anyone that comes  across this. Here is my code, it works but has that duplicate app window I mentioned earlier.

     

     

    //ContentView.swift
    import SwiftUI
    import SwiftyDropbox
    import AppKit
    
    struct ContentView: View {
        
        @State var isShown = false
        
        var body: some View {
            HStack {
                VStack {
                        
                        Button(action: {
                            self.isShown.toggle()
                        }) {
                            Text("Login to Dropbox")
                                .padding(.top)
                        }
    
                        DropboxView(isShown: $isShown)
                        
                        Button {
                            if let client = DropboxClientsManager.authorizedClient {
                                print("successful login")
                            } else {
                                print("Error")
                            }
                        } label: {
                            Text("Test Login")
                                .padding(.bottom)
                                
                        }
                        
                    }
                    .onOpenURL { url in
                        let oauthCompletion: DropboxOAuthCompletion = {
                            if let authResult = $0 {
                                switch authResult {
                                case .success:
                                    print("Success! User is logged into DropboxClientsManager.")
                                    
                                case .cancel:
                                    print("Authorization flow was manually canceled by user!")
                                case .error(_, let description):
                                    print("Error: \(String(describing: description))")
                                }
                                NSApplication.shared.setActivationPolicy(.prohibited)
                            }
                        }
                        DropboxClientsManager.handleRedirectURL(url, completion: oauthCompletion)
                }
            }
        }
    }
    
    struct DropboxView: NSViewControllerRepresentable {
        
        typealias NSViewControllerType = NSViewController
        
        @Binding var isShown: Bool
        
        func updateNSViewController(_ nsViewController: NSViewController, context: Context) {
            if isShown {
                let scopeRequest = ScopeRequest(scopeType: .user, scopes: ["account_info.read", "files.metadata.write", "files.metadata.read", "files.content.write", "files.content.read"], includeGrantedScopes: false)
                
                DropboxClientsManager.authorizeFromControllerV2(
                    sharedApplication: NSApplication.shared,
                    controller: nsViewController,
                    loadingStatusDelegate: nil,
                    openURL: { (url: URL) -> Void in NSWorkspace.shared.open(url) },
                    scopeRequest: scopeRequest)
            }
        }
        
        func makeNSViewController(context _: Self.Context) -> NSViewController {
            return NSViewController(nibName: "DBViewController", bundle: nil)
        }
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }

     

     

     

     

     

    //YOURApp.swift
    import SwiftUI
    import SwiftyDropbox
    
    @main
    struct dropboxClientApp: App {
        
        init() {
            DropboxClientsManager.setupWithAppKeyDesktop("app_key")
        }
        var body: some Scene {
            WindowGroup {
                ContentView()
            }
        }
    }

     

     

    Then you need to make a file type of View. I called mine "DBViewController" in the contentview file at the bottom. After that follow these instructions

    1. Make sure that you have a nib file named DBViewController.xib in your Xcode project.

    2. Open the nib file, select the File's Owner object, and set its class to NSViewController in the Identity Inspector.

    3. Connect the view outlet of the File's Owner to the view object in the nib file.

    4. In the makeNSViewController function of DropboxView, instantiate the DBViewController from the nib file using the init?(nibName:bundle:) initializer.

  • Michael-jamf's avatar
    Michael-jamf
    Helpful | Level 6
    3 years ago

    Okay managed to get it working with the examples that Dropbox has on their github. With no duplicate views. I think that was something on my end where the app wasnt actually closed or something.

    This is for MacOS. If you want to use iOS I would recommend looking here. It worked out of the box for iOS.

    ContentView.swift

     

    import SwiftUI
    import SwiftyDropbox
    import AppKit
    
    struct ContentView: View {
    
        func myButtonInControllerPressed() {
            // OAuth 2 code flow with PKCE that grants a short-lived token with scopes, and performs refreshes of the token automatically.
            let scopeRequest = ScopeRequest(scopeType: .user, scopes: ["account_info.read"], includeGrantedScopes: false)
            DropboxClientsManager.authorizeFromControllerV2(
                sharedApplication: NSApplication.shared,
                controller: nil,
                loadingStatusDelegate: nil,
                openURL: {(url: URL) -> Void in NSWorkspace.shared.open(url)},
                scopeRequest: scopeRequest
            )
        }
        
        var body: some View {
            VStack {
                Button {
                    myButtonInControllerPressed()
                } label: {
                    Text("Test Dropbox Auth")
                }
            }.frame(maxWidth: .infinity, maxHeight: .infinity)
    
        }
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }

     

    YOURAPPNAMEapp.swift

     

    import SwiftUI
    import SwiftyDropbox
    
    @main
    struct dropboxClientApp: App {
        
        @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
        
        var body: some Scene {
            WindowGroup {
                ContentView()
            }
        }
    }
    
    class AppDelegate: NSObject, NSApplicationDelegate {
        func applicationDidFinishLaunching(_ aNotification: Notification) {
            DropboxClientsManager.setupWithAppKeyDesktop("app-key")
            NSAppleEventManager.shared().setEventHandler(self,
                                                             andSelector: #selector(handleGetURLEvent),
                                                             forEventClass: AEEventClass(kInternetEventClass),
                                                             andEventID: AEEventID(kAEGetURL))
        }
        
        @objc
        func handleGetURLEvent(_ event: NSAppleEventDescriptor?, replyEvent: NSAppleEventDescriptor?) {
            if let aeEventDescriptor = event?.paramDescriptor(forKeyword: AEKeyword(keyDirectObject)) {
                if let urlStr = aeEventDescriptor.stringValue {
                    let url = URL(string: urlStr)!
                    let oauthCompletion: DropboxOAuthCompletion = {
                        if let authResult = $0 {
                            switch authResult {
                            case .success:
                                print("Success! User is logged into Dropbox.")
                            case .cancel:
                                print("Authorization flow was manually canceled by user!")
                            case .error(_, let description):
                                print("Error: \(String(describing: description))")
                            }
                        }
                    }
                    DropboxClientsManager.handleRedirectURL(url, completion: oauthCompletion)
                    // this brings your application back the foreground on redirect
                    NSApp.activate(ignoringOtherApps: true)
                }
            }
        }
    }

     

     

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.6,036 PostsLatest Activity: 3 days ago
411 Following

The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.

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!