코딩 연습/코딩배우기
API를 잘 모르면서 해본 '쿠팡 파트너스 API' 실습
good4me
2021. 8. 23. 20:09
API를 잘 모르면서 해본 '쿠팡 파트너스 API' 실습
쿠팡 파트너스 사용자의 수익 활동에 도움이 된다는 '쿠팡 파트너스 API'를 받았다.
쿠팡 파트너스 API를 통해 사용자의 블로그(워드프레스 등)나 웹 사이트 등에 바로 적용 할 수 있는 다수의 상품 정보를 제공받을 수 있다고 한다.
처음해보는 거라 쉽지는 않았는데 구글링 덕분에 마무리 했고, 하고보니 참 편리한 기능이네~
그리고 API 연습삼아 상품베스트 리스트를 추출해봤다.
쿠팡 파트너스 API
import hmac
import hashlib
import binascii
import os
import time
import requests
import json
from urllib.parse import urljoin
def coupang_api(categoryId, limit):
REQUEST_METHOD = "GET"
DOMAIN = "https://api-gateway.coupang.com"
URL = f"/v2/providers/affiliate_open_api/apis/openapi/products/bestcategories/{str(categoryId)}?limit={str(limit)}"
ACCESS_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx"
SECRET_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
REQUEST = { "coupangUrls": [
"https://www.coupang.com/np/search?component=&q=good&channel=user",
"https://www.coupang.com/np/coupangglobal"
]}
def generateHmac(method, url, secretKey, accessKey):
path, *query = url.split("?")
os.environ["TZ"] = "GMT+0"
datetime = time.strftime('%y%m%d')+'T'+time.strftime('%H%M%S')+'Z'
message = datetime + method + path + (query[0] if query else "")
signature = hmac.new(bytes(secretKey, "utf-8"),
message.encode("utf-8"),
hashlib.sha256).hexdigest()
return "CEA algorithm=HmacSHA256, access-key={}, signed-date={}, signature={}".format(accessKey, datetime, signature)
authorization = generateHmac(REQUEST_METHOD, URL, SECRET_KEY, ACCESS_KEY)
url = "{}{}".format(DOMAIN, URL)
response = requests.request(method=REQUEST_METHOD, url=url,
headers={
"Authorization": authorization,
"Content-Type": "application/json"
},
data=json.dumps(REQUEST)
)
return response.json()
상품 리스트 추출해보기
def coupang_html():
result = coupang_api(1016, 50)
print('<table style="width:500;">')
for key, data in result.items():
if key == 'data':
for lst in data:
print('<tr><td style="height:60px;">')
print('[' + lst['categoryName'] + ']<br><b><h3><a href="' + lst['productUrl'] + '" target="_blank">' + lst['productName'] + '</a></h3></b></td></tr>')
print('<tr><td><a href="' + lst['productUrl'] + '" target="_blank"><img src="' + lst['productImage'] + '" width="400" alt="' + lst['productName'] + '"></a></td></tr>')
print('<tr><td style="height:20px;font-size: 8px;"></td></tr>')
print('</table>')
coupang_html()
