0

I'm new to Python and to coding in general. I'm trying to request poloniex public API using this simple code but keep getting 403 Error.

Does anyone have any idea what can cause it and how to fix it?

Link to Poloniex API Doc

Thanks

import requests


def public_method():
    url = 'https://poloniex.com/public?command=returnTicker'
    api = requests.get(url)

    return api


print(public_method())
artur
  • 1

3 Answers3

0

403 is a HTTP status code. You can learn more about those here.

Saying that, the code you supplied works. It connects to the api however the api itself returns a Forbidden 403 response.

Your code will return a requests object which is (I believe) almost what you want. If you'd like to retrieve the data from the poloniex api you'll need to call json() method against said object.

import requests


def public_method():
    url = 'https://poloniex.com/public?command=returnTicker'
    api = requests.get(url)

    return api


print(public_method().json())
Hevlastka
  • 1,878
  • 2
  • 18
  • 29
  • Thank you for your reply. I used to call json() method but got a lot of errors. Trying to find the cause of the problem I just reduced code to its minimum. So 403 seems to be the root cause. – artur Feb 01 '18 at 12:48
  • @artur if you open [https://poloniex.com/public?command=returnTicker](https://poloniex.com/public?command=returnTicker) in your browser do you get the data straight away or is it asking you to go through CAPTCHA first? – Hevlastka Feb 01 '18 at 12:53
0

Basically, it requires to have header. This solved the problem.

import requests


def public_method():
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) 
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 
Safari/537.36',
        'Cookie': 
'cf_clearance=1159d2ca806b3ebf2a85a8706f4b8c90ff6abc01-1517488982-1800'
    }

    url = 'https://poloniex.com/public?command=returnTicker'
    api = requests.get(url, headers=headers)

    return api


print(public_method())
artur
  • 1
0

If it has a CAPTCHA when you open from you browser, it is a GeoIp security feature, you may use a VPS or VPN localised inside the Europe or the US zone to avoid this security issue.

A. STEFANI
  • 6,707
  • 1
  • 23
  • 48