Last active
December 10, 2020 16:34
-
-
Save CptSpaceToaster/bf7464602aca07260d083a7747aaef97 to your computer and use it in GitHub Desktop.
A very simple demonstration of TCGPlayer's API.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import requests # pip install requests | |
TCGPLAYER_API_URL = 'https://api.tcgplayer.com' | |
TCGPLAYER_API_VERSION = 'v1.39.0' | |
# https://able.bio/rhett/how-to-set-and-get-environment-variables-in-python--274rgt5 | |
TCGPLAYER_PUBLIC_KEY = os.environ.get('TCGPLAYER_PUBLIC_KEY') | |
TCGPLAYER_PRIVATE_KEY = os.environ.get('TCGPLAYER_PRIVATE_KEY') | |
# All code based on the guide from here: https://docs.tcgplayer.com/docs/getting-started | |
if __name__ == '__main__': | |
# Step 1. Get an access token from TCGPlayer. This will usually be good for about 2 weeks, however you should | |
# check the token_response_data for the value 'expires_in' to learn when your token will stop working. | |
payload = { | |
'grant_type': 'client_credentials', | |
'client_id': TCGPLAYER_PUBLIC_KEY, | |
'client_secret': TCGPLAYER_PRIVATE_KEY, | |
} | |
r = requests.post( | |
TCGPLAYER_API_URL + '/token', | |
data=payload, | |
) | |
assert r.status_code == 200 | |
token_response_data = r.json() | |
print('Token Received:') | |
print(token_response_data) | |
# Step 2. Format the value we need to use for the Authorization header | |
access_token = token_response_data['token_type'] + ' ' + token_response_data['access_token'] | |
# Step 3. Use that token in every request to the API from now on | |
r = requests.get( | |
TCGPLAYER_API_URL + '/' + TCGPLAYER_API_VERSION + '/catalog/categories?limit=100', | |
headers={ | |
'Authorization': access_token | |
} | |
) | |
assert r.status_code == 200 | |
categories_response_data = r.json() | |
print('Categories Received:') | |
print(categories_response_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment