LoginSignup
1
1

More than 3 years have passed since last update.

google-api-ruby-clientを0.8.3からバージョンアップ

Last updated at Posted at 2020-05-21

障害対応時にgoogle-api-ruby-clientをバージョンアップして解決できたのでメモしておく。

2020/5/11-12の間を境にGoogle Custom Search APIの呼び出しが失敗するようになったので調査。

Rubyのバージョン

ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-linux]

google-api-clientのバージョン

google-api-client (0.8.3)

APIサーバー側で問題になっていそうな箇所を抜き出して
検証用コードを作成

#!/usr/bin/env ruby

require 'google/api_client'

API_KEY = 'XXXXX'
CSE_ID = 'xxxxx' # カスタム検索エンジンID

def google_api_client
  client ||= Google::APIClient.new.tap do |cli|
      cli.authorization = nil
  end
end

def custom_search_api
  api ||= google_api_client.discovered_api('customsearch', 'v1')
end

query = {
  num: 3,
  start: 1,
  total_num: 25,
  q: '検索ワード',
  cx: CSE_ID
}

json = google_api_client.execute(
  api_method: custom_search_api.cse.list,
  key: API_KEY,
  parameters: query
).body

puts json

実行結果。404が返っている。

W, [2020-05-21T12:31:15.200344 #19166]  WARN -- : Google::APIClient - Please provide :application_name and :application_version when initializing the client
W, [2020-05-21T12:31:15.236307 #19166]  WARN -- : Google::APIClient - Please provide :application_name and :application_version when initializing the client
<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 404 (Not Found)!!1</title>
  <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
  </style>
  <a href=//www.google.com/><span id=logo aria-label=Google></span></a>
  <p><b>404.</b> <ins>That’s an error.</ins>
  <p>The requested URL <code>/discovery/v1/apis/customsearch/v1/customsearch/v1?cx=012429992898212448897%3Aqfb0vzczcjq&amp;key=AIzaSyDUnb5XYQ4ziNU6Li0o2_8oKgMMTIWhCFg&amp;num=3&amp;q=%E6%A4%9C%E7%B4%A2%E3%83%AF%E3%83%BC%E3%83%89&amp;start=1&amp;total_num=25</code> was not found on this server.  <ins>That’s all we know.</ins>

バージョンアップして正しく動くか確認。
以下を参考に0.8.3から最新の0.32.1にバージョンアップして検証。
https://github.com/googleapis/google-api-ruby-client/blob/master/MIGRATING.md
https://github.com/googleapis/google-api-ruby-client/blob/master/generated/google/apis/customsearch_v1/service.rb#L284

#!/usr/bin/env ruby

require 'google/apis/customsearch_v1'

API_KEY = 'XXXXX'
CSE_ID = 'xxxxx'

custom_search_api = Google::Apis::CustomsearchV1::CustomsearchService.new
custom_search_api.key = API_KEY

query = {
  num: 3,
  start: 1,
  cr: 25,
  cx: CSE_ID
}

json = custom_search_api.list_cses('検索ワード', query).to_json

puts json

これはjsonが返ってきた。
旧バージョンで動いていたときの出力結果がもはやわからないがツラいところだが、正しく動いてそう。

{"context":{"title":"
... 
json","type":"application/json"}}

Google APIのv0.8.3がサポートされなくなった?

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