https://clovastudio.ncloud.com/tuning/api에서 주는 python 예제를 받아 수행하면 잘 됩니다.
그러나 https://api.ncloud-docs.com/docs/clovastudio-gettasks는 예제가 없습니다. 그래서 이 예제를 고쳐서 수행 했습니다.
# -*- coding: utf-8 -*-
import uuid
import base64
import hashlib
import time
import hmac
import requests
class ListTaskExecutor:
def __init__(self, host, uri, method, iam_access_key, secret_key):
self._host = host
self._uri = uri
self._method = method
self._api_gw_time = str(int(time.time() * 1000))
self._iam_access_key = iam_access_key
self._secret_key = secret_key
self._request_id = str(uuid.uuid4())
def _make_signature(self):
secret_key = bytes(self._secret_key, 'UTF-8')
message = self._method + " " + self._uri + "\n" + self._api_gw_time + "\n" + self._iam_access_key
message = bytes(message, 'UTF-8')
signing_key = base64.b64encode(hmac.new(secret_key, message, digestmod=hashlib.sha256).digest())
return signing_key
def _send_request(self, size_cnt=20, page_no=0):
headers = {
'Content-Type': 'application/json; charset=utf-8',
'X-NCP-APIGW-TIMESTAMP': self._api_gw_time,
'X-NCP-IAM-ACCESS-KEY': self._iam_access_key,
'X-NCP-APIGW-SIGNATURE-V2': self._make_signature(),
'X-NCP-CLOVASTUDIO-REQUEST-ID': self._request_id
}
result = requests.get(self._host + self._uri + f"?page={page_no}&size={size_cnt}", headers=headers).json()
return result
def execute(self, size_cnt, page_no):
res = self._send_request(size_cnt, page_no)
if 'status' in res and res['status']['code'] == '20000':
return res['result']
else:
return res
if __name__ == '__main__':
completion_executor = ListTaskExecutor(
host='https://clovastudio.apigw.ntruss.com',
uri='/tuning/v2/tasks',
method="GET",
iam_access_key='....',
secret_key='....',
)
response_text = completion_executor.execute(size_cnt=20, page_no=0)
print(response_text)
이렇게 수행하면 인증오류가 납니다.
{'error': {'errorCode': '200', 'message': 'Authentication Failed', 'details': 'Invalid authentication information.'}}
무었이 문제인지 확인 가능할 가요 ?