cancel
Showing results for 
Search instead for 
Did you mean: 
Announcements
Pro tip to secure your Dropbox account! Learn more

DropboxClientsManager.authorizedClient process

SOLVED
1,123 Views
8 Comments
4 Kudos
Highlighted
Level 7

DropboxClientsManager.authorizedClient process

Ok,

 

I am sorry, this is I know the third post in almost as many days, almost got this thing finished, just clearing up the last few bugs. This one the authorization process which seemed to work and yet it doesn't.

 

This is my code... it works and yet it never remembers from one invocation to the next my client is authorized. DropboxClientsManager.authorizedClient always returns "nil".  What am I missing here?

 

@IBAction func getAuth(_ sender: AnyObject) {
  print("authorizedClient \(DropboxClientsManager.authorizedClient)")
  if (DropboxClientsManager.authorizedClient != nil) {
    // client authorized, next page
    self.performSegue(withIdentifier: "blahView", sender:nil);
  } else { 
    DropboxClientsManager.setupWithAppKey("xxxx")
    DropboxClientsManager.authorizeFromController(UIApplication.shared,
    controller: self,
    openURL: { (url: URL) -> Void in
   UIApplication.shared.open(url, options: [slightly smiling face, completionHandler: nil)
  })
}
  • iOS
1 Accepted solution

Accepted Solutions
Solution
Level 7

Re: DropboxClientsManager.authorizedClient process

By Jones you've done it again Jeeves [Greg]. I moved the setupkey to the appDelegate and it works!! Final working code looks like this!! Suspect I don't need the Notification in the appDelegate, but need a third platform to test it which I don't have at hand right now. Later.

 

In AppDelegate

 

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  theApp.isNetworkActivityIndicatorVisible = true

  if (DropboxClientsManager.authorizedClient != nil) {
  sharedDataAccess.fnReset()
  NotificationCenter.default.post(name: Notification.Name("go4it"), object: nil, userInfo: nil)
  } else {
  DropboxClientsManager.setupWithAppKey("ysfn9am0kogqfmh")
  }
  return true
}

 

In ViewController

 

@IBAction func getAuth(_ sender: AnyObject) {
appDelegate.mpcHandler.browser = nil
if (DropboxClientsManager.authorizedClient != nil) {
sharedDataAccess.fnReset()
self.performSegue(withIdentifier: "tableView", sender:nil)
} else {
DropboxClientsManager.authorizeFromController(UIApplication.shared,
controller: self,
openURL: { (url: URL) -> Void in
UIApplication.shared.open(url, options: [slightly smiling face, completionHandler: nil)
})
}

8 Replies
Dropboxer

Re: DropboxClientsManager.authorizedClient process

Did you finish setting up the authorization flow, including the receiving code, as shown here?

 

https://github.com/dropbox/SwiftyDropbox#handle-redirect-back-into-sdk

 

Can you share the rest of your relevant code and any errors you're getting in the console?

Level 7

Re: DropboxClientsManager.authorizedClient process

Yes. Mine looks like this.

 

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [slightly smiling face) -> Bool {

if let authResult = DropboxClientsManager.handleRedirectURL(url) {
theApp.isNetworkActivityIndicatorVisible = false
switch authResult {
case .success:
print("Success! User is logged into Dropbox.")
DispatchQueue.main.async {
NotificationCenter.default.post(name: Notification.Name("good2go"), object: nil, userInfo: nil)
}
case .cancel:
print("Authorization flow was manually canceled by user!")
case .error(_, let description):
print("Error: \(description)")
}
}

return true
}
 

 

Dropboxer

Re: DropboxClientsManager.authorizedClient process

Are you getting any output or errors?
Level 7

Re: DropboxClientsManager.authorizedClient process

No, no errors. It works. But it always returns nil. I never seems to remember I already authorized the client? I am set to your dropbox authorization screen for my app every time I run it, which isn't really the desired bahavior; is this normal?

Level 7

Re: DropboxClientsManager.authorizedClient process

I did manage to get this error at one point.

 

authorizedClient nil
precondition failed: Only call `DropboxClientsManager.setupWithAppKey` or `DropboxClientsManager.setupWithTeamAppKey` once: file /Users/localuser/Carthage/Checkouts/SwiftyDropbox/Source/SwiftyDropbox/PlatformNeutral/DropboxClientsManager.swift, line 22
2017-01-05 20:45:38.644142 blahQueue[3460:838496] precondition failed: Only call `DropboxClientsManager.setupWithAppKey` or `DropboxClientsManager.setupWithTeamAppKey` once: file /Users/localuser/Carthage/Checkouts/SwiftyDropbox/Source/SwiftyDropbox/PlatformNeutral/DropboxClientsManager.swift, line 22

 

After calling DropboxClientsManager.resetClients() before I called anything. I also tried this call. DropboxClientsManager.unlinkClients() to reset things, but to no avail.

 

I also tried an IPad, was running on an iPhone. Both running iOS 10.2 (14C92). Using xCode 8.2.1 obviously coding using Swift 3.0 syntax. That did the same thing, unable to retain the fact that I have autorized the client?

 

I checked the dropbox apps page. Nothing seems out of place for the app I am trying to use. I can give you the name if it is ok to post it here?

Level 7

Re: DropboxClientsManager.authorizedClient process

Out of time tonight. Tried saving the token in a global variable and using it with this code, but got this error message.

 

fcuk02012017 error Error 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}

 

Code I used to get this error message slightly smiling face

 

let client = DropboxClientsManager.authorizedClient?.auth.client.accessToken
print("fcuk0501 authorizedClient \(client)")
sharedDataAccess.authToken = client

 

let client = DropboxClient(accessToken: sharedDataAccess.authToken)

 

_ = client.files.listFolder(path:sourcePath).response(queue: DispatchQueue(label: "MyCustomSerialQueue")) { response, error in
if let response = response {

Dropboxer

Re: DropboxClientsManager.authorizedClient process

Thanks for the additional information. That's not the expected behavior. After you process the app authorization flow, it should get stored and re-used, even across app restarts, accessible via authorizedClient. (That being the case, you shouldn't have to do any manual workarounds like you're trying.) I can't seem to reproduce this issue unfortunately.

 

The error about setupWithAppKey should indicate that you're calling it more than necessary though. The documentation indicates it should be called in didFinishLaunchingWithOptions. Can you try moving that and let me know if it helps?

Solution
Level 7

Re: DropboxClientsManager.authorizedClient process

By Jones you've done it again Jeeves [Greg]. I moved the setupkey to the appDelegate and it works!! Final working code looks like this!! Suspect I don't need the Notification in the appDelegate, but need a third platform to test it which I don't have at hand right now. Later.

 

In AppDelegate

 

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  theApp.isNetworkActivityIndicatorVisible = true

  if (DropboxClientsManager.authorizedClient != nil) {
  sharedDataAccess.fnReset()
  NotificationCenter.default.post(name: Notification.Name("go4it"), object: nil, userInfo: nil)
  } else {
  DropboxClientsManager.setupWithAppKey("ysfn9am0kogqfmh")
  }
  return true
}

 

In ViewController

 

@IBAction func getAuth(_ sender: AnyObject) {
appDelegate.mpcHandler.browser = nil
if (DropboxClientsManager.authorizedClient != nil) {
sharedDataAccess.fnReset()
self.performSegue(withIdentifier: "tableView", sender:nil)
} else {
DropboxClientsManager.authorizeFromController(UIApplication.shared,
controller: self,
openURL: { (url: URL) -> Void in
UIApplication.shared.open(url, options: [slightly smiling face, completionHandler: nil)
})
}