Search code examples
javascriptjqueryapipcloud

Can't Access to API using jQuery


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.


Solution

  • 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>