Search code examples
swiftoauth-2.0tokendropbox

How to Call the /oauth2/token endpoint with our app’s client_secret to exchange the code for an access token for DropBox in Swift?


I am using SwiftyDropbox to integrate Dropbox with my iOS App. I have completed all the configurations and implemented the code as in the documentation. User need to access the files in their dropbox account through my app. When I tried with 'generated access token' (got from dropbox console), I was able to access the files in my own account only. I understood that we will need to use the standard OAuth flow to obtain access tokens for other users.

enter image description here

Followed the steps as explained in https://developers.dropbox.com/oauth-guide

  1. Constructed a Dropbox authorization URL, with my application’s client_id and redirect_uri.

  2. Next step is to Call the /oauth2/token endpoint with my app’s client_secret to exchange the code for an access token. I am not sure how to implement this. If any of you have solved this before, please advise. Some portions of my code is given below.

    override func viewDidLoad() { super.viewDidLoad()

         self.actionDropboxLogin()
     }
    
     func actionDropboxLogin() {
         if (DropboxClientsManager.authorizedClient != nil) {
    
             //User is already authorized
             //Fetch images from user's DropBox folder
             self.getImageFromDropbox(path: "", isBack: false)
         } else {
    
             //User not authorized
             //So we go for authorizing user first.
    
             guard let rwURL = URL(string: "https://www.dropbox.com/oauth2/authorize?client_id=myclientid&redirect_uri=https://myapp/callback&response_type=code") else { return }
    
    
             let scopeRequest = ScopeRequest(scopeType: .user, scopes: ["account_info.read files.content.read files.content.write files.metadata.read file_requests.read"], includeGrantedScopes: false)
               DropboxClientsManager.authorizeFromControllerV2(
                   UIApplication.shared,
                   controller: self,
                   loadingStatusDelegate: nil,
                   openURL: { (url: URL) -> Void in UIApplication.shared.openURL(rwURL) },
                   scopeRequest: scopeRequest
               )
             self.getImageFromDropbox(path: "", isBack: false)
    
         }
     }`
    
     func getImageFromDropbox(path:String, isBack:Bool) {
         let client = DropboxClientsManager.authorizedClient
         if client != nil {
        //code for getting list of folder, images or videos here.
    

    } self.tableView.reloadData() }

When I print the value of client in console it is nil. So the condition 'if client != nil' is false and I am not able to get the files. Which means I am not getting an access token.


Solution

  • When using the SwiftyDropbox SDK, you don't need to handle any of the /oauth2/authorize or /oauth2/token details directly. The SDK implements the app authorization flow for you. You should only implement the flow as documented here:

    https://github.com/dropbox/SwiftyDropbox#configure-your-project