LoginSignup
3
2

More than 5 years have passed since last update.

Railsでwebpush(vapid)しようと思ったら、"Unauthorized"エラーが出た話

Last updated at Posted at 2018-02-12

Railsでwebpush通知をしたかったので、この記事(Rails4におけるブラウザプッシュ通知(Web Push Notifications)実装)が超分かりやすいんで、参考に作っていたら....

Chromeでこういうエラーが出て、なかなか通知することができませんでした。
#<Net::HTTPBadRequest 400 UnauthorizedRegistration readbody=true>

調べてもいまいち原因がわからなかったので、Firefoxでも試してみると相変わらずエラー:cry:

{"code":401,"errno":109,"error":"Unauthorized","more_info":"http://autopush.readthedocs.io/en/latest/http.html#error-codes", "message": "Request did not validate Invalid bearer token: Auth > 24 hours in the future"}

Chromeで試したよりエラーが詳しかったのでぐぐってみると、どうやらexpiresionを設定しなければ駄目なそうです。参考記事

Webpush.payload_send(
  message: payload,
  endpoint: endpoint,
  p256dh: p256dh,
  auth: auth,
  vapid: {
    subject: "mailto:sender@example.com",
    public_key: '...',
    private_key: '...',
    expiration: 12 * 60 * 60 #これ
  }
end

expirationを設定するとChromeでもFirefoxでも無事通知することができました。

他にも悩んでいる方の解決の糸口になれば幸いです:smile:

3
2
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
3
2