Forum Discussion

ABDUL Salam's avatar
ABDUL Salam
Explorer | Level 3
2 years ago

I am getting invalid_url again and again with /save_url, https://api.dropboxapi.com/2/files/save_url

Hi,
I have an app that uses https://api.dropboxapi.com/2/files/save_url/save_url  endpoint to save urls from my ecommerce store to my dropbox. It was working fine early on locally bu as soon as I have deployed my script and check the logs after execution then this endpoint returns me a async_job_id which I use to check job status from https://api.dropboxapi.com/2/files/save_url/check_job_status and I get

{
".tag": "failed",
"failed": {
".tag": "invalid_url"
}
}
this error as response again and again.
I am using PHP as development language and curl for making requests to /save_url endpoint.
Although I am sure my URL's are not invalid.

 

 

 

 

 

function saveCustomilyOrders($dropboxURL, $accessToken, $customilyOrders)
{
    $apiURL = $dropboxURL . '/2/files/save_url';
    $headers = [
        'Authorization: Bearer ' . $accessToken,
        'Content-Type: application/json',
    ];
    foreach ($customilyOrders as $customilyOrder) {
        $path = $customilyOrder['path'];
        $url = $customilyOrder['url'];
        $payload = [
            'path' => $path,
            'url' => $url,
        ];
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $apiURL);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        if ($httpCode == 200) {
//Do some logs
        } elseif ($httpCode != 200) {
            throw new Exception('something went wrong while saving customily orders to dropbox: ' . $httpCode . ' ' . $response);
        }
        sleep(5);
    }
}

 

 

 

 

 

this is the snippet of function which I am using to /save_url enpoint.
Any suggestion in this issue will be highly appreciated as this is getting so much annyoying that it sometimes works and some times not and now its giving invalid_url again and again.
my json_encoded body looks like this in my curl request:
"{"path":"\/2023\/November\/#1123\/GP\/#1123_GK.eps","url":"https:\/\/cdn.customily.com\/ExportFile\/pf-dev-3\/b462fef1-aff5-42fa-aa77-f4eb0a6c7cce.eps"}" 
Greg-DB 
if you can look into this.
Thanks.

  • Здравко's avatar
    Здравко
    Legendary | Level 20

    Hi ABDUL Salam,

    You don't need to tag any slash! The backslashes you added make your link invalid (and not only - the next error would be that your path in invalid too). Trace where those data are coming from, in your code, and fix whatever needed.

    By the way it's not a good practice to pass access token directly. Better build one 'opaque' structure holding all authentication data and pass that structure instead. You can get access token as result of dedicated function that get as param the same 'opaque' structure. Inside such a function you can trace whether access token is going to expire (let's say there are less that 5 or 3 mins) or already expired and if so perform a refresh. In such a way you won't need to do it in every API call (or another place), but instead in every API call, you will call to that function (that's responsible to all common things of authentication, including refresh whenever needed - there implement that last step we talked about previous). 😉

    Also keep in mind that validity period is NOT something fixed! It's denoted in seconds when you receive access token and in spite typically 4 hours that's NOT something mandatory - can be less in some cases. Once you receive access token calculate expiration moment (based on current moment and validity period) and store that moment for future reference.

    Hope this gives directions.

    • ABDUL Salam's avatar
      ABDUL Salam
      Explorer | Level 3

      Hi Здравко  
      Firstly would like to appreciate your suggestion related to coding practices.
      Basically,  this body with slashes is the output  

      json_encode($payload);

      that I have sent in my curl request.
      Also, the main issue is if I manually run my script from the terminal locally using php script.php
      It works perfectly with the same code.
      But when I schedule it as a cron job on my server this script does not run as it runs locally, it does complete code flow and return me async_job_id in response but when I check using this async_job_id it always gives me this 

      {
      ".tag": "failed",
      "failed": {
      ".tag": "invalid_url"
      }
      }

       I wonder what could be the reason.

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

        ABDUL Salam As Здравко said, the backslashes you have in the output you shared here are not expected or accepted in the URL, so make sure those aren't in the actual data you're sending to the API.

         

        As for why this works locally but not on your server, does your server have a different version of any libraries you're using? Or are you perhaps using a different URL value on the server?

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.5,950 PostsLatest Activity: 20 hours ago
352 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!