CoinCapDev / CoinCap.io

API tools and documentation
734 stars 151 forks source link

HttpURLConnection returns "Forbidden" #92

Open marfraTUC opened 6 years ago

marfraTUC commented 6 years ago

Hi,

I try to use CoinCap API whitin a Java Project.

private String getCurrencyAsHTML(Currencys cur, Currencys target) throws Exception {
        StringBuilder result = new StringBuilder();
        URL url = new URL(buildURL(cur));
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }
        rd.close();
        JSONObject jsonObj = new JSONObject(result.toString());
        return jsonObj.getString(getTragetStringFor(target));
    }

I tried the same method with other API's, what works. However, in this case I get the result:

Server returned HTTP response code: 403 for URL: http://coincap.io/page/ETH/ Forbidden

For any ideas I would be grateful.

sam0829 commented 6 years ago

Seems like an issue because of Captcha. From my home wifi, if I try to access the API, I need to first pass captcha(Forbidden Status) but from my office wifi, I can access the page directly. It would be helpful if the team respond to the issue.

ItsSkynet commented 6 years ago

i have this same problem, but from a php endpoint on a server. any way we can contact coincap for them to address this issue where... ironically a cloudflare firewall is activated on a api endpoint url... where non human interactions are supposed to happen.

jontis commented 6 years ago

I think I got this too trying to acces the api with python: http://coincap.io/history/BTC Accessing it with browser works fine, but that's not what I need.

jontis commented 6 years ago

Here's the explanation and workaround. The API only accepts certain user agents. If you let your agent present itself like a browser, it will be accepted. I did this by modifying the sent user_agent string to look like a Mozilla broswer.

code in python:

# start by getting a list of coins that are available on coincap.io
opener = urllib.request.build_opener()
opener.addheaders = [('User-Agent', 'Mozilla/5.0')]
response = opener.open('http://coincap.io/coins')
list_coincap_io_coins = sorted(json.loads(response.read().decode()))
Barrya42 commented 5 years ago

Have the same, i trying over the RestTamplate on Java "https://api.coincap.io/v2/rates/bitcoin" and i got 403. Then did it over postman, telerik with no any headers and it works. What's wrong?

CoinCapDev commented 5 years ago

Hello Barrya42,

We are looking into the issue you are having with our V2 API. Could you please include the headers you are using for your request? It will help us understand the 403 issue you are having.

Laura ShapeShift

CoinCap Product Manager, ShapeShift AG laura@shapeshift.io mailto:laura@shapeshift.io | shapeshift.io http://shapeshift.io/ http://www.facebook.com/shapeshiftexchange/ http://twitter.com/ShapeShift_io http://www.youtube.com/channel/UCu3maYVeb18l2b1gqjO5KeQ http://www.reddit.com/r/shapeshiftio/ http://www.instagram.com/shapeshift_io/ https://shapeshift.io/gpg/.asc

On Dec 25, 2018, at 7:38 PM, Barrya42 notifications@github.com wrote:

Have the same, i trying over the RestTamplate on Java "https://api.coincap.io/v2/rates/bitcoin https://api.coincap.io/v2/rates/bitcoin" and i got 403. Then did it over postman, telerik with no any headers and it works. What's wrong?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/CoinCapDev/CoinCap.io/issues/92#issuecomment-449891288, or mute the thread https://github.com/notifications/unsubscribe-auth/AMV2DDBr07Dgb9rbh5Xy5H_GjoGPLbczks5u8uElgaJpZM4RbEK4.