Created
April 1, 2016 14:24
-
-
Save mizucoffee/f5983b524b55446ead698a5ec84e7dc6 to your computer and use it in GitHub Desktop.
GoogleAPIをRubyでPostで叩いてトークンとか取得する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra' | |
require 'sinatra/reloader' | |
require 'net/http' | |
require 'uri' | |
require 'json' | |
#この方法だと1人しか管理できない | |
client_id = 'XXX.googleusercontent.comみたいなclient_id' | |
client_secret = "client_secret" | |
redirect_uri = 'http://localhost:4567/redirect'#リダイレクト先 | |
scope = 'https://www.googleapis.com/auth/calendar' | |
access_token = '' | |
refresh_token = '' | |
token_type = '' | |
get '/login' do | |
if refresh_token != "" | |
"既に認証しています" | |
else | |
oauth_url = "https://accounts.google.com/o/oauth2/auth?client_id=#{client_id}&redirect_uri=#{redirect_uri}&scope=#{scope}&response_type=code&approval_prompt=force&access_type=offline" | |
redirect oauth_url.to_s | |
end | |
end | |
get '/redirect' do | |
uri = URI.parse('https://accounts.google.com/o/oauth2/token') | |
response = nil | |
request = Net::HTTP::Post.new(uri.request_uri) | |
request.body = "client_secret=#{client_secret}&grant_type=authorization_code&redirect_uri=#{redirect_uri}&client_id=#{client_id}&code=#{params['code']}" | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
res = http.start do | |
http.request(request) | |
end | |
if res.code == '200' | |
json = JSON.parse(res.body) | |
access_token = json['access_token'] | |
refresh_token = json['refresh_token'] | |
token_type = json['token_type'] | |
else | |
puts "Error #{res.code} #{res.message}" | |
"エラー" | |
end | |
"認証完了" | |
end | |
get '/refreshtoken' do | |
if refresh_token != "" | |
uri = URI.parse('https://accounts.google.com/o/oauth2/token') | |
response = nil | |
request = Net::HTTP::Post.new(uri.request_uri) | |
request.body = "client_secret=#{client_secret}&client_id=#{client_id}&refresh_token=#{refresh_token}&grant_type=refresh_token" | |
print request.body | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
res = http.start do | |
http.request(request) | |
end | |
if res.code == '200' | |
json = JSON.parse(res.body) | |
access_token = json['access_token'] | |
token_type = json['token_type'] | |
else | |
puts "Error #{res.code} #{res.message}" | |
"エラー" | |
end | |
"認証更新完了" | |
else | |
"認証されていません" | |
end | |
end | |
#カレンダーのポストについて | |
# https://www.googleapis.com/calendar/v3/calendars/#{カレンダーID}/events?key=#{APIキー} | |
# に対して | |
# Authorization: #{token_type} #{access_token} | |
# ↑のスペース重要! | |
# のヘッダーを付けて | |
# https://developers.google.com/google-apps/calendar/v3/reference/events/insert#try-it | |
# を参考にしてPOSTのbodyを作って | |
# POSTすればカレンダーをいじれる |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment