how to use the MTA api key in python?

568 views
Skip to first unread message

Xue lida

unread,
Feb 9, 2021, 2:04:07 PM2/9/21
to mtadeveloperresources
hello guys,
 i am a uk student working on my final year program. Now i got the api key but i didnot find a way to use them in python. i use urllib.request.urlopen to connect the server, but it shows the error message HTTPError: Forbidden.
can everyone help me?
Thanks 

Kevin

unread,
Mar 7, 2021, 4:23:52 PM3/7/21
to mtadeveloperresources
class TokenAuth(AuthBase):
    def __init__(self, token):
        self.token = token

    def __call__(self, r):
        """Attach an API token to a custom auth header."""
        r.headers['x-api-key'] = f'{self.token}'  # Python 3.6+
        return r


url = [url_of_endpoint]
API_KEY = [your_api_key]

response = requests.get(url, auth=TokenAuth(API_KEY))

James Fennell

unread,
Mar 7, 2021, 8:15:16 PM3/7/21
to mtadevelop...@googlegroups.com
The auth boilerplate is not needed strictly speaking. You can just pass in the API key as a regular HTTP header, if that's easier:

response = requests.get(url, headers={"X-API-KEY": API_KEY})

James

--
You received this message because you are subscribed to the Google Groups "mtadeveloperresources" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mtadeveloperreso...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mtadeveloperresources/701352e0-7cf1-48ae-b512-e097aa738f79n%40googlegroups.com.

Anthony Townsend

unread,
Apr 20, 2021, 7:59:03 AM4/20/21
to mtadeveloperresources
you can also pass it as an argument 
?key=

Reply all
Reply to author
Forward
0 new messages