Skip to content

Instantly share code, notes, and snippets.

@tyjeon24
Last active July 8, 2020 15:10
Show Gist options
  • Save tyjeon24/5697d5f9b3063e12b9d695f64ce07a1e to your computer and use it in GitHub Desktop.
Save tyjeon24/5697d5f9b3063e12b9d695f64ce07a1e to your computer and use it in GitHub Desktop.
Python : Naver Ad API
import base64
import hashlib
import hmac
import json
import requests
import time
def main():
BASE_URL = "https://api.naver.com" # 네이버 광고 API의 기본 URL이다.
uri = "/keywordstool"
request_url = BASE_URL + uri
headers = get_headers()
params = {"hintKeywords":"마스크,마스크시트"}
response = requests.get(request_url,headers=headers,params=params)
keyword_json = json.loads(response.text)
print(keyword_json)
def get_headers():
# X-API-KEY : 네이버 광고 - 도구 - API 사용 관리 메뉴에서 "엑세스라이선스"의 값.
x_api_key = "0100000000d817b1f8a64b1c18f78dde25efae0a54bb6d57fa232383543b2be447a8a5291"
# X-CUSTOMER : 네이버 광고 - 도구 - API 사용 관리 메뉴에서 "CUSTOMER_ID"의 값.
x_customer = "1840710"
# X-Timestamp : 현재 시간 * 1000을 반올림한 자연수 값.
x_timestamp = str(round(time.time() * 1000))
# secret_key : 네이버 광고 - 도구 - API 사용 관리 메뉴에서 "비밀키"의 값.
# 각 API 문서 상단에는 HTTP request가 있고, 이 때의 요청방식이 method이다.
# GET /keywordstool에서 GET은 method, /keywrodstool은 uri이다.
secret_key = "AQAAAADYF7H4pkscGPeN3iXvrgpUh9c200UYeou7zgDSOwmqkdam=="
method = "GET"
uri = "/keywordstool"
# X-Signature
# : X-Timestamp, method, uri 값을 점(.)으로 연결한 뒤 HmacSHA256 알고리즘으로 암호화한 후 Base64로 인코딩한 값.
sign = "%s.%s.%s" % (x_timestamp, method, uri)
signature_encrypted = hmac.new(
secret_key.encode(), sign.encode(), hashlib.sha256
).digest()
x_signature = base64.b64encode(signature_encrypted).decode()
headers = {
"X-API-KEY": x_api_key,
"X-CUSTOMER": x_customer,
"X-Timestamp": x_timestamp,
"X-Signature": x_signature,
}
return headers
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment