LoginSignup
0
1

More than 3 years have passed since last update.

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

Last updated at Posted at 2020-02-27

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]
0
1
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
0
1