Forum Discussion

Kirill's avatar
Kirill
New member | Level 2
8 years ago

Sign In via access token without login page

I created new account and uploaded some pictures. I also created Access Token. Users haven't dropbox account and I want to display these pictures in my application. Can I do this without login page?

It works good with login page:

DropboxClientsManager.authorizeFromController(UIApplication.shared,                                                      controller: self,                                                      openURL: { (url: URL) -> Void in
                                                      UIApplication.shared.openURL(url)
        })
let client = DropboxClientsManager.authorizedClient

 

But doesn't work with access token only:

let client = DropboxClient.init(accessToken: "...")

 

client?.files.download(path: "/pics")
            .response { response, error in
                if let response = response {
                    let responseMetadata = response.0
                    print(responseMetadata)
                    let fileContents = response.1
                    print(fileContents)
                } else if let error = error {
                    print(error)
                }
            }
            .progress { progressData in
                print(progressData)
        }

 

I got error: Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLKey=https://api-content.dropbox.com/2/files/download, NSErrorFailingURLStringKey=https://api-content.dropbox.com/2/files/download, NSLocalizedDescription=cancelled}

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    [Cross-linking for reference: https://stackoverflow.com/questions/42622883/swifty-dropbox-sign-in-via-access-token ]

     

    The API was designed with the intention that each user would link their own Dropbox account, in order to interact with their own files. However, it is technically possible to connect to just one account. The SDKs don't offer explicit support for it and we don't recommend doing so, for various technical and security reasons.

     

    However if you did want to go this route, instead of kicking off the authorization flow, you would manually use an existing access token for your account and app, as you're trying to here. (Just be careful not to revoke it, e.g. via https://www.dropbox.com/account/security .)

     

    As far as the actual issue you're running in to, I suspect the issue is that you're constructing the client object and making the call, but then the client object is going out of scope before the response can come back. Make sure you keep your client object around. For example, you can use authorizedClient to store it, like this:

     

    DropboxClientsManager.authorizedClient = DropboxClient.init(accessToken: "...")

     

    And then use the client via DropboxClientsManager.authorizedClient.

    • TotteAlm's avatar
      TotteAlm
      New member | Level 2

      This one helped me a lot (my test code worked but when I moved it into the application, I trippde and fell like the OP. Thanks!

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.5,941 PostsLatest Activity: 12 hours ago
351 Following

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!