728x90

네이버 API - 단축 URL

import os
import sys
import urllib.request
import json

client_id = "" # 개발자센터에서 발급받은 Client ID 값
client_secret = "" # 개발자센터에서 발급받은 Client Secret 값
encText = urllib.parse.quote("https://clanguage.tistory.com") # 단축 시킬 URL 주소
data = "url=" + encText
url = "https://openapi.naver.com/v1/util/shorturl"
request = urllib.request.Request(url)
request.add_header("X-Naver-Client-Id",client_id)
request.add_header("X-Naver-Client-Secret",client_secret)
response = urllib.request.urlopen(request, data=data.encode("utf-8"))
rescode = response.getcode()
if(rescode==200):
    response_body = response.read()

    response = response_body.decode('utf-8')
    responseJson = json.loads(response)

    print("단축된 URL 주소 : " + responseJson.get("result").get("url"))
else:
    print("Error Code:" + rescode)

 

https://developers.naver.com/docs/utils/shortenurl/

https://developers.naver.com/docs/utils/shortenurl/#python

 

 

 

+ Recent posts