1

I am using pCloud Api to get download link form the request. It is a GET request. When I request form browser I can get a response. But when I use jQuery I get a response code result : 7010

Api Request URL : https://api.pcloud.com/getpublinkdownload?code=8eM7
I get this response when requesting from browser:

{
    "result": 0,
    "expires": "Mon, 07 Aug 2017 00:12:50 +0000",
    "dwltag": "aftsTab2SLkC4MDXRdp6Am",
    "path": "\/cBZkvG2cXZNPjykVZZZChTDE7ZNVZZj5JZkZSqfRZIXZqkZmVZR7Zd7Z4ZfkZIZyVZokZbXZ3VZFkZ77ZIgCcZ14l5zXbx6p4GwdeEPdF1707nIPm7\/image%20%286%29.jpg",
    "hosts": [
        "p-def2.pcloud.com",
        "c166.pcloud.com"
    ]
}

I need this hosts and path to generate the download link. I just need this -https://c166.pcloud.com/cBZkvG2cXZNPjykVZZZChTDE7ZNVZZj5JZkZSqfRZIXZqkZmVZR7Zd7Z4ZfkZIZyVZokZbXZ3VZFkZ77ZIgCcZ14l5zXbx6p4GwdeEPdF1707nIPm7/image%20%286%29.jpg

I have to use jQuery/JavaScript to get this response. I tried PHP file_get_contents(); it works but this link will work only form the ip address you request for. So, I must use JQ/JS.

My Code:

$(document).ready(function(){

        function httpGet(theUrl){
            var xmlHttp = new XMLHttpRequest();
            xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
            xmlHttp.send( null );
            return xmlHttp.responseText;
        }

        console.log(httpGet("https://api.pcloud.com/getpublinkdownload?code=8eM7"));

});

Thanks for trying to help me.

Jakub Muda
  • 6,008
  • 10
  • 37
  • 56
JaTurna
  • 194
  • 14

1 Answers1

1

It seems pCloud server is checking referrer. In most case, servers will refuse the accesss not coming from itself.

Only web browsers arriving from a small set of approved (login) pages are given access; this facilitates the sharing of materials among a group of cooperating paysites from https://en.wikipedia.org/wiki/HTTP_referer

In following html, script ran and got image url successfully, but browser raised error when it was trying to load image.

  <html>
    <head>
    </head>
    <script
    src="http://code.jquery.com/jquery-3.2.1.js"
    integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
    crossorigin="anonymous"></script>
  <body>

  <h1>Load Image from pCloud </h1>
  <img class="loading">


  <script>
    $(document).ready(function() {

      var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
          if (this.responseText){
            var host = JSON.parse(this.responseText).hosts[0];
            var path = JSON.parse(this.responseText).path;
          }
          $(".loading").attr("src", "https://" + host + path);
        }
      };
      xhttp.open("GET", "https://api.pcloud.com/getpublinkdownload?code=8eM7", true);
      xhttp.send();
    });

  </script>

  </body>
  </html>
Evelyn Ma
  • 494
  • 2
  • 4
  • So, Why they provide API where it doesn't work at all? Please help me here is pCloud api documentation : https://docs.pcloud.com – JaTurna Aug 06 '17 at 19:48
  • 1
    In my html and debugging API worked fine and it got image url successfully, but browser raised error when it was trying to load image. – Evelyn Ma Aug 06 '17 at 19:54
  • can you please send your html and debugging API codes? – JaTurna Aug 06 '17 at 20:00
  • ups I didn't saw that. sorry. It will work... if you delete extra /\ (slashes) from the url. I don't know why this api has many problems. You can see there are 2 extra /\ in `path` – JaTurna Aug 06 '17 at 20:00