1

I have a problem connecting the Dropbox API to my application. Dropbox API documentation is in front of my eyes, I do everything as it is written there. But in some of the methods that are indicated there are errors and I do not know what to replace the entities that are indicated there. I can log in, but I cannot get the token, error instead: "-canOpenURL: failed for URL: "dbapi-2://1/connect" - error: "The operation couldn’t be completed. (OSStatus error -10814.)"

import SwiftyDropbox

class AppDelegate:... {
   func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        //error Use of undeclared type 'DropboxOAuthCompletion'
        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))")
              }
          }
        }
        DropboxClientsManager.handleRedirectURL(url, completion: oauthCompletion)
        return true
    }
}

import SwiftyDropbox

class ViewController:... {
   func openDropboxAutorization() {
        // Legacy authorization flow that grants a long-lived token.
        DropboxClientsManager.authorizeFromController(UIApplication.shared,
                                                      controller: self,
                                                      openURL: { (url: URL) -> Void in
                                                        UIApplication.shared.open(url, options: [:], completionHandler: nil)
        })
        
        //New: OAuth 2 code flow with PKCE that grants a short-lived token with scopes.
        
        //error Use of unresolved identifier 'ScopeRequest'
        let scopeRequest = ScopeRequest(scopeType: .user, scopes: ["account_info.read"], includeGrantedScopes: false)
        DropboxClientsManager.authorizeFromControllerV2(
            UIApplication.shared,
            controller: self,
            loadingStatusDelegate: nil,
            openURL: { (url: URL) -> Void in UIApplication.shared.openURL(url) },
            scopeRequest: scopeRequest
        )
    }
}

Both of these methods are copied from the DropboxAppi documentation, but they don't work and I can't find the right solution.

VyacheslavB
  • 181
  • 9
  • is SwiftyDropbox a cocoapod library? if not how have you added it to the project. if it is, are you opening the workspace file or the project file. – Scriptable Aug 14 '20 at 08:48
  • Of course it's connected, I did everything step by step – VyacheslavB Aug 14 '20 at 08:52
  • something is obviously wrong somewhere and without further information we have little chance of finding out what is causing it – Scriptable Aug 14 '20 at 12:30
  • After installing pod the Xcode say that Conversion to Swift 5 is available, and if I don't do it, then there are no errors, but I still cannot go beyond authorization. After it, a window appears: "Application name" would like to: View basic information...bla-bla-bla. But if I press any button - nothing heppenes – VyacheslavB Aug 14 '20 at 12:43
  • In console I see error: 2020-08-14 15:38:52.477148+0300 MusicPlayer[14334:982728] -canOpenURL: failed for URL: "dbapi-8-emm://1/connect?k=5nski8h24edy1h6&s=" - error: "The operation couldn’t be completed. (OSStatus error -10814.)" – VyacheslavB Aug 14 '20 at 12:44
  • you might need a newer version of the pod that is already swift 5 – Scriptable Aug 14 '20 at 13:46
  • [Cross-linking for reference: https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/I-have-a-problem-with-connecting-Dropbox-API-to-my-iOS-app-with/td-p/445201 ] – Greg Aug 14 '20 at 14:20
  • Yes, it's my question to the Dropbox )) – VyacheslavB Aug 14 '20 at 15:12

1 Answers1

1

For SwiftyDropBox

within AppDelegate.swift

import SwiftyDropbox

      func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        /// Add Dropbox
        DropboxClientsManager.setupWithAppKey("<YOUR_APP_KEY>")
        
        return true
      }


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

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

          }
        }
      canHandleUrl = DropboxClientsManager.handleRedirectURL(url, completion: oauthCompletion)
    }
    
    return canHandleUrl
  }

Now for the ViewController where you want to start the authentication Process. In this code, I am using a UISwitch

import SwiftyDropBox

     @IBAction func connectDropboxSwitchClicked(_ sender: Any) {
        if connectDropboxSwitch.isOn {
          
          /// https://stackoverflow.com/a/39546950/14414215
          /// openURL deprecated
          let scopeRequest = ScopeRequest(scopeType: .user, scopes: ["account_info.read","files.content.write"], includeGrantedScopes: false)
          DropboxClientsManager.authorizeFromControllerV2(
            UIApplication.shared,
            controller: self,
            loadingStatusDelegate: nil,
            //          openURL: { (url: URL) -> Void in UIApplication.shared.openURL(url) },
            openURL: { (url: URL) -> Void in UIApplication.shared.open( url, options: [:])},
            scopeRequest: scopeRequest
          )
          
        } else {
          print(" connectDropbox Switch:\(connectDropbox)")
        }
      }

your Info.plist should contain these for the redirect to come back to your app

<key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleIdentifier</key>
            <string></string>
            <key>CFBundleTypeRole</key>
            <string>Editor</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string><YOUR API KEY></string>
            </array>
        </dict>
    </array>
    <key>LSApplicationQueriesSchemes</key>
    <array>
    <string>dbapi-8-emm</string>
    <string>dbapi-2</string>
    </array>
app4g
  • 670
  • 4
  • 24