質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

Q&A

1回答

3243閲覧

GoogleカレンダーAPIにおいてアクセストークンがないというエラーがでます。

qaz3330

総合スコア113

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

0グッド

0クリップ

投稿2016/02/02 11:36

編集2022/01/12 10:55

GoogleカレンダーAPIを使用したく以下のようなスクリプトを使用してます。

require 'rubygems' require 'google/api_client' require 'google/api_client/client_secrets' require 'google/api_client/auth/file_storage' require 'google/api_client/auth/installed_app' require 'pp' require 'json' # 認証情報ファイルは確かにあります。 CREDENTIAL_STORE_FILE = "client_secret.json" def setup() client = Google::APIClient.new(:application_name => 'Ruby hogehoge sample', :application_version => '1.0.0') # Oauth認証:google developers consoleの秘密鍵jsonを利用 file_storage = Google::APIClient::FileStorage.new(CREDENTIAL_STORE_FILE) if file_storage.authorization.nil? client_secrets = Google::APIClient::ClientSecrets.load flow = Google::APIClient::InstalledAppFlow.new( :client_id => client_secrets.client_id, :client_secret => client_secrets.client_secret, :access_token => client_secrets.authorization.access_token, :scope => %w( https://www.googleapis.com/auth/calendar.readonly https://docs.google.com/feeds/ https://docs.googleusercontent.com/ https://spreadsheets.google.com/feeds/ ), ) client.authorization = flow.authorize(file_storage) else client.authorization = file_storage.authorization end return client end client =setup() cal = client.discovered_api('calendar', 'v3') event = { 'summary' => 'テスト予定タイトル', 'start' => { 'dateTime' => '2015-09-24T10:00:00+09:00', 'timeZone' => 'Asia/Tokyo', }, 'end' => { 'dateTime' => '2015-09-24T12:00:00+09:00', 'timeZone' => 'Asia/Tokyo', } } p event p "=================================================" result = client.execute(:api_method => cal.events.insert, :parameters => {'calendarId' => 'primary'}, :body => JSON.dump(event), :headers => {'Content-Type' => 'application/json'}) p result.status

ただ、これを実行すると以下のエラーがでます。
エラー内容を見る限りアクセストークンがないということはわかるのですが、このスプリクトのどこが悪いのかがわからなく困っております。

尚、他の方の実装例などを見ている際に、ブラウザが開いて、認証して・・・みたいな流れがあったみたいですが、私のケースではありませんでした。
おそらくそこが原因なのかなと思ってます。

助言を頂けると幸いです。
宜しくお願いします。

{"summary"=>"テスト予定タイトル", "start"=>{"dateTime"=>"2015-09-24T10:00:00+09:00", "timeZone"=>"Asia/Tokyo"}, "end"=>{"dateTime"=>"2015-09-24T12:00:00+09:00", "timeZone"=>"Asia/Tokyo"}} "=================================================" /Users/vendor/bundle/ruby/2.2.0/gems/signet-0.7.2/lib/signet/oauth_2/client.rb:1042:in `generate_authenticated_request': Missing access token. (ArgumentError) from /Users/vendor/bundle/ruby/2.2.0/gems/google-api-client-0.8.2/lib/google/api_client/request.rb:241:in `to_env' from /Users/vendor/bundle/ruby/2.2.0/gems/google-api-client-0.8.2/lib/google/api_client/request.rb:161:in `send' from /Users/vendor/bundle/ruby/2.2.0/gems/google-api-client-0.8.2/lib/google/api_client.rb:638:in `block (2 levels) in execute!' from /Users/vendor/bundle/ruby/2.2.0/gems/retriable-1.4.1/lib/retriable/retry.rb:27:in `perform' from /Users/vendor/bundle/ruby/2.2.0/gems/retriable-1.4.1/lib/retriable.rb:15:in `retriable' from /Users/vendor/bundle/ruby/2.2.0/gems/google-api-client-0.8.2/lib/google/api_client.rb:635:in `block in execute!' from /Users/vendor/bundle/ruby/2.2.0/gems/retriable-1.4.1/lib/retriable/retry.rb:27:in `perform' from /Users/vendor/bundle/ruby/2.2.0/gems/retriable-1.4.1/lib/retriable.rb:15:in `retriable' from /Users/vendor/bundle/ruby/2.2.0/gems/google-api-client-0.8.2/lib/google/api_client.rb:626:in `execute!' from /Users/vendor/bundle/ruby/2.2.0/gems/google-api-client-0.8.2/lib/google/api_client.rb:669:in `execute' from get_yaml_2.rb:66:in `<main>'

こちらだとブラウザで許可リクエストなどが出てきて、.google-api.yamlが生成されるのですが、中身が空っぽのため、その後の認証(コメントアウトしてる部分)に失敗します・・・。

require 'google/api_client' require 'google/api_client/client_secrets' require 'google/api_client/auth/installed_app' require 'json' require 'yaml' client = Google::APIClient.new( :application_name => 'Example Ruby application', :application_version => '1.0.0' ) plus = client.discovered_api('plus') flow = Google::APIClient::InstalledAppFlow.new( :client_id => 'キーは実際には入れてます', :client_secret => 'キーは実際には入れてます', :scope => ['https://www.googleapis.com/auth/calendar'] ) #File.open("./.google-api.yaml", "w"){|f| # x = { # "mechanism" => "oauth_2", # "scope" => "https://www.googleapis.com/auth/calendar", # "client_id" => client_secrets.client_id, # "client_secret" => client_secrets.client_secret, # "access_token" => client.authorization.access_token, # "refresh_token" => client.authorization.refresh_token # } # f.puts(x.to_yaml) #}

また、今後、他のユーザーがauth認証をする際にはこれらのスクリプトでできるのでしょうか?

現時点であまりイメージが湧いていなく、大丈夫か心配です。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

アクセストークンがないというエラーです。
GoogleのOAuth2.0認証では、ユーザーが認証許可すると、"code"という名前でauthorization codeが返ってきます。この、"code"を、"access_token"と"refresh_token"に交換します。このaccess_tokenを用いて、APIにアクセスすることで、データを取得できます。
パッと見たところ、

ruby

1:access_token => client_secrets.authorization.access_token,

の部分が怪しいと思います。
どうやってaccess_tokenを所得していますか?

以下の公式ドキュメントがとても参考になると思います。
https://developers.google.com/api-client-library/ruby/auth/web-app?hl=ja

投稿2016/02/12 01:49

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問