Created
September 16, 2024 15:42
-
-
Save mofosyne/e15bb122024ff97ecc707dac68ab92b5 to your computer and use it in GitHub Desktop.
Digikey Basic Product Search Script (Targeting Digikey API v4)
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
#!/usr/bin/env python3 | |
# digikey_api_v4_product_search.py | |
# Digikey Basic Product Search Script (Targeting Digikey API v4) | |
# By Brian Khuu 2024 | |
# This is a digikey api v4 script that is focused only on just the flow needed to get product information. | |
# Recommend Reading: | |
# - https://developer.digikey.com/documentation | |
# - Section "OAuth 20: 2 Legged Flow" has good information on basic authnetic | |
# Design Principle: This script is written to be self contained besides requests and for each callout function to be composable | |
# This lacks much of more advance oauth implementation like token caching. | |
# Also this is restricted to sandboxed api at the moment... | |
# However this is okay as this was done as a learning exercise in addition to being for one-off internal projects. | |
# Other Similar Code: | |
# - https://github.com/alvarop/dkbc/blob/master/dkbc/dkbc.py | |
# - Just found this while coding this up which was very helpful in figuring out what I was missing | |
import os | |
import json | |
import requests | |
DIGIKEY_CLIENT_ID=os.getenv("DIGIKEY_CLIENT_ID") | |
DIGIKEY_CLIENT_SECRET=os.getenv("DIGIKEY_CLIENT_SECRET") | |
DIGIKEY_AUTH_URL_V4 = 'https://sandbox-api.digikey.com/v1/oauth2/authorize' | |
DIGIKEY_TOKEN_URL_V4 = 'https://sandbox-api.digikey.com/v1/oauth2/token' | |
DIGIKEY_PRODUCT_SEARCH_URL_V4 = 'https://sandbox-api.digikey.com/products/v4/search/keyword' | |
# Sanity Check That Authorisation Details Was Provided | |
if not DIGIKEY_CLIENT_ID or not DIGIKEY_CLIENT_SECRET: | |
print("Missing client id or secret") | |
quit() | |
def oauthV2_get_simple_access_token(url, client_id, client_secret): | |
# Get the simple access token required for 2 Legged Authorization OAutV2.0 flow | |
# This is typically used for basic search and retreival of publically avaliable information | |
response = requests.post( | |
url, | |
data={ | |
"client_id": client_id, | |
"client_secret": client_secret, | |
"grant_type": "client_credentials", | |
} | |
) | |
return response.json() | |
def oauthv2_product_search(url, client_id, token, keyword): | |
# Dev Note: Why did I not just place the data? | |
# https://stackoverflow.com/questions/15737434/python-requests-module-urlencoding-json-data | |
data_payload = { | |
"Keywords": str(keyword), | |
} | |
response = requests.post( | |
url, | |
headers = { | |
"X-DIGIKEY-Client-Id": client_id, | |
"authorization": "Bearer {access_token}".format(access_token=token.get("access_token")), | |
"content-type": "application/json", | |
"accept": "application/json", | |
}, | |
data = json.dumps(data_payload) | |
) | |
return response.json() | |
oauth_token = oauthV2_get_simple_access_token(DIGIKEY_TOKEN_URL_V4, DIGIKEY_CLIENT_ID, DIGIKEY_CLIENT_SECRET) | |
print(oauth_token) | |
search_result = oauthv2_product_search(DIGIKEY_PRODUCT_SEARCH_URL_V4, DIGIKEY_CLIENT_ID, oauth_token, "MCP2221A-I/SL-ND") | |
print(json.dumps(search_result, indent=4)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the expected output of this script