LoginSignup
2
0

More than 3 years have passed since last update.

COTOHA アクセストークンの取得

Last updated at Posted at 2020-02-20

COTOHA API Portal の使用例です。

仕様

アクセストークン取得

access_token.py
#! /usr/bin/python
# -*- coding:utf-8 -*-
#
#   access_token.py
#
# ----------------------------------------------------------------------
import sys
import os
import json
from dotenv import load_dotenv
import  requests
# ----------------------------------------------------------------------
dotenv_path = '.env'
load_dotenv(dotenv_path)
CLIENT_ID = os.environ.get("CLIENT_ID")
CLIENT_SECRET = os.environ.get("CLIENT_SECRET")
DEVELOPER_API_BASE_URL = os.environ.get("DEVELOPER_API_BASE_URL")
ACCESS_TOKEN_PUBLISH_URL = os.environ.get("ACCESS_TOKEN_PUBLISH_URL")
#
data = {
    "grantType": "client_credentials",
    "clientId": CLIENT_ID,
    "clientSecret": CLIENT_SECRET
    }
str_json = json.dumps(data)

url = ACCESS_TOKEN_PUBLISH_URL
headers={
    "Content-Type": "application/json"
    }

try:
    rr=requests.post(url,headers=headers,data=str_json)
    print(rr.text)
except Exception as ee:
    sys.stderr.write("*** error *** in requests.post ***\n")
    sys.stderr.write(str(ee) + "\n")
# ----------------------------------------------------------------------
.env
LIENT_ID = AAAAAAAAAAAAAAAAAAAAAAAAAA
CLIENT_SECRET = aaaaaaaaaaaaaaaaaaaaaaaaaa
DEVELOPER_API_BASE_URL = https://api.ce-cotoha.com/api/dev/nlp/
ACCESS_TOKEN_PUBLISH_URL = https://api.ce-cotoha.com/v1/oauth/accesstokens

実行結果

$ ./access_token.py 

          {
            "access_token": "VAIET9p2UVvb*******", 
            "token_type": "bearer",
            "expires_in": "86399" ,
            "scope": "" ,    
            "issued_at": "1582265896034"           
           }

使ったバージョン

$ python --version
Python 3.8.1
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0