Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

COTOHA アクセストークンの取得 (Ruby)

More than 1 year has passed since last update.

COTOHA API Portal の使用例です。

access_token.rb
#! /usr/bin/ruby
# -*- encoding: utf-8 -*-
#
#   access_token.rb
#
#                   Feb/27/2020
#
# ---------------------------------------------------------------------
require 'faraday'
require 'json'
require 'dotenv'
#
# ---------------------------------------------------------------------
STDERR.puts "*** 開始 ***"
#
Dotenv.load
#
data = {
    "grantType": "client_credentials",
    "clientId": ENV["CLIENT_ID"],
    "clientSecret": ENV["CLIENT_SECRET"]
    }
#
str_json = JSON.generate(data)
#
url = ENV['ACCESS_TOKEN_PUBLISH_URL']

headers={
    "Content-Type": "application/json"
    }
#
con = Faraday.new 
res = con.post do |req|
    req.url url
    req.headers['Content-Type'] = 'application/json'
    req.body = str_json
end

puts    res.status
puts    res.body
puts    "-----------" 

dict_aa=JSON.parse(res.body)
puts    dict_aa['access_token']
#
STDERR.puts "*** 終了 ***"
# ---------------------------------------------------------------------

実行結果

$ ./access_token.rb 
*** 開始 ***
201

          {
            "access_token": "oleleUiGwUIVjbvH*******", 
            "token_type": "bearer",
            "expires_in": "86399" ,
            "scope": "" ,    
            "issued_at": "1582767850104"           
           }

-----------
oleleUiGwUIVjbvHLonJD2OA3I8q
*** 終了 ***

使ったバージョン

$ ruby --version
ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux]
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away