Web技術勉強会 20130525 - Google Cloud Messaging入門
- 16. 手順3:Google APIs Console設定
API Access→Create an Oauth 2.0 client ID
branding informationに入力する
Application Typeは「Web Application」を選択
Authrorized Redirect URLs→More optionsを選択
https://developers.google.com/oauthplayground を入力
ClientID、Client secretを取得するClientID、Client secretを取得する
- 27. 手順5:通知プログラム作成
アクセストークンは1時間で使用不可になるので実行ごと
にとるようにすると良い
request = {
'client_id' => client_id,
'client_secret' => client_secret,
'refresh_token' => refresh,
'grant_type' => 'refresh_token''grant_type' => 'refresh_token'
}
client = HTTPClient.new
client.ssl_config.verify_mode = nil
res = client.post(
'https://accounts.google.com/o/oauth2/token',
request
)
access_token = JSON.parse(res.body)['access_token']
- 28. 手順5:通知プログラム作成
Chromeに通知するサンプルコード
data = {
:channelId => channelId,
:subchannelId => "0",
:payload => "hellow!"
}
response = client.post(
'https://www.googleapis.com/gcm_for_chrome/v1/messages',
data.to_json,
{
'Content-Type' => "application/json",
'Authorization' => "Bearer #{access_token}"
}
)