0

When picking files from the dropbox (V2), I am trying to get the shared link url using this below code.

  internal inner class GetDropboxSharableURLTask(file_path: String, pathLower: String) : AsyncTask<String, Void, Void>() {
    private var pDialog: ProgressDialog? = null
    private var dropbox_url: String? = file_path
    private var db_pathLower: String? = pathLower



    override fun onPreExecute() {
        super.onPreExecute()

        pDialog = ProgressDialog(context)
        pDialog!!.setCancelable(false)
        pDialog!!.setMessage(context?.resources?.getString(R.string.please_wait))
        pDialog!!.show()
    }

    override fun doInBackground(vararg urls: String): Void? {
        try {

            val shareLink =   DropboxClientFactory.getClient().sharing().getSharedLinkMetadata(db_pathLower)
            dropbox_url = getShareURL(shareLink.url)!!.replaceFirst("https://www".toRegex(), "https://dl")

            return null
        } catch (e: Exception) {
            e.printStackTrace()

            return null
        }

    }


    fun getShareURL(strURL: String?): String? {
        var conn: URLConnection? = null
        var redirectedUrl: String? = null
        try {

            val inputURL = URL(strURL)
            conn = inputURL.openConnection()
            conn!!.connect()

            val `is` = conn.getInputStream()
            println("Redirected URL: " + conn.url)
            redirectedUrl = conn.url.toString()
            `is`.close()

        } catch (e: MalformedURLException) {
            Log.d("", "Please input a valid URL")
        } catch (ioe: IOException) {
            Log.d("", "Can not connect to the URL")
        }

        return redirectedUrl
    }

    override fun onPostExecute(feed: Void?) {
        // TODO: check this.exception
        // TODO: do something with the feed


        pDialog!!.hide()
        Log.d("url", dropbox_url)
        val intent = Intent()

        intent.putExtra(Constant.KEY_DROPBOX_PICKER, dropbox_url)

        context?.setResult(Activity.RESULT_OK, intent)

        context?.finish()

    }
}

I am getting the error as below -

com.dropbox.core.v2.sharing.SharedLinkErrorException: Exception in 2/sharing/get_shared_link_metadata: SHARED_LINK_NOT_FOUND

This above error is thrown when calling getSharedLinkMetadata(db_pathLower)

Though, earlier, I was using the similar code in dropbox V1, but as soon as I switched to newer version of the dropbox API, i.e, dropbox V2, I started getting this error when trying to get the actual sharing url of dropbox, (the one which must contain the file extension as well).

The expected url should be like this - https://dl.dropbox.com/s/hpe1gjfb4aqsv3e/property_1523965639_img.jpeg?dl=0

but, what I am getting is something like https://dl.dropboxusercontent.com/apitl/1/AAD62_lYN5n_cYyRQWhg_rmXGJPFzqF5m8OJp‌​Nt_SIIxG7bVvmO6X5d1pKg7uulM1vEBWx_X9PZ9i3vFy-jb3eBC-M_q3YCWRmPrdAwpQ7kqSFGCIPrZaH‌​NC44YRjwXGXYTbnqMO1hPhKb-G5matDzTABUQOssB-LIN4qWoJmPnuhNgzpL9FO4ibet4uBPoef_SLZLj‌​upsOV9PKYUhtPxY_NY7HjymZSHsQh67m4HoBN4YgEAPot0KMAsV1eE3WCjK0XbD1YfGqdsVI9H40KUQ_9‌​R-nmAouoqdbA37G5CXjQKYPC8cENTvN2pjHKwCnHvgI just because of that error.

Please, let me the know the thing, which I need to modify in order to get the dropbox sharing url (along with file extension), and to avoid the above error, as soon as the file is selected / clicked.

Narendra Singh
  • 3,990
  • 5
  • 37
  • 78
  • What is the value of `db_pathLower`? It should be an active Dropbox shared link, and if so `getSharedLinkMetadata` will return its metadata. You'll get this`SHARED_LINK_NOT_FOUND` error if the link is not valid. If you want to create a shared link for a particular file path, you should use `createSharedLinkWithSettings` instead. – Greg Apr 17 '18 at 16:09
  • @Greg I tried using `createSharedLinkWithSettings` but it gave me some error, something like, *Link already Exists* or *Shared Link already created* – Narendra Singh Apr 17 '18 at 16:52
  • If the shared link already exists, you can use `listSharedLinks` to retrieve it. – Greg Apr 17 '18 at 17:14
  • @Greg thanks a lot, finally resolved using `listSharedLinksBuilder`. Your hint somehow gave me a good sort of hint. Thanks :) – Narendra Singh Apr 18 '18 at 12:20

1 Answers1

0

Finally, resolved with the help of this, as listSharedLinksBuilder finally worked for me.

It got resolved by modifying the code to -

      val sharedLinksResults = DropboxClientFactory.getClient().sharing().listSharedLinksBuilder().withPath(db_pathLower).withDirectOnly(true).start()
        var url = ""
        if (sharedLinksResults.links.isEmpty()) {
            val shareLink = DropboxClientFactory.getClient().sharing().createSharedLinkWithSettings(db_pathLower) //.getClient().sharing().getSharedLinkMetadata(db_pathLower)
            url = shareLink.url

        } else {
            url = sharedLinksResults.links[0].url


        }
        dropbox_url = getShareURL(url)!!.replaceFirst("https://www".toRegex(), "https://dl")
Narendra Singh
  • 3,990
  • 5
  • 37
  • 78