API key authorisation in Python

Hi everyone,

I’m trying to scrape data in python using the requests package, but it’s ending on 'statusCode': 429, 'message': 'Rate limit is exceeded... after only 50 calls (I’ve set a counter).

I feel like the issue is centred on incorrect authentication of the API key. I set up the 500 requests / min access yesterday and (try to) submit it each call

import requests
from requests.auth import HTTPBasicAuth    

class TfLAPI:
    """

    """
    def __init__(self, app_id, app_key):
        self.app_id = app_id
        self.app_key = app_key
        self.auth = HTTPBasicAuth('apikey', app_key)
        self.features = []
        self.request_count = 0
        self.headers = {"Accept": "application/json"}

    def get_station_info(self, station_ID):
        """
        Request and return station name, ID, longitude, latitude

        :param station_ID:
            str: station naptan ID
        :return:
        """
        self.request_count += 1

        try:
            request_url = f"https://api.tfl.gov.uk/StopPoint/{station_ID}"
            r = requests.get(request_url, auth=self.auth, headers=self.headers)
            results = r.json()
        except Exception as e:
            print(f"Error querying station for {station_ID}")
            return

        return [results['commonName'], results['lon'], results['lat'],
                results['modes']]

Is a different authentication method needed? Adding the key to this particular URL doesn’t seem to work. I’ve also tried the TfL API Python package but it throws up errors, so decided to use the requests package instead.

Thanks.

I seem to have fixed the issue by using some functions from the tfl Python package, posting here for anyone who might have the same issue:

import requests
from urllib.parse import urlencode

class TfLAPI:
    def __init__(self, app_id, app_key):
        self.app_id = app_id
        self.app_key = app_key
        self.features = []
        self.request_count = 0
        self.api_token = {"app_id": app_id, "app_key": app_key}

def get_query_strings(self, params):
    if params is None:
        params = {}
    if self.api_token is not None:
        params.update(self.api_token)
    return urlencode(params)

def get_station_info(self, station_ID):
    """
    Request and return station name, ID, longitude, latitude

    :param station_ID:
        str: station naptan ID
    :return:
    """
    self.request_count += 1

    try:
        request_url = f"https://api.tfl.gov.uk/StopPoint/{station_ID}"

        r = requests.get(request_url + "?" +
                         self.get_query_strings(params=None))
        results = r.json()
    except Exception as e:
        print(f"Error querying station for {station_ID}")
        return

    return [results['commonName'], results['lon'], results['lat'],
            results['modes']]