artur
artur

Reputation: 1

Getting 403 error using Python3

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())

Upvotes: 0

Views: 445

Answers (3)

A. STEFANI
A. STEFANI

Reputation: 6737

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.

Upvotes: 0

artur
artur

Reputation: 1

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())

Upvotes: 0

Hevlastka
Hevlastka

Reputation: 1948

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())

Upvotes: 0

Related Questions