【发布时间】:2014-04-08 11:17:24
【问题描述】:
我在尝试连接到 Google Cloud Message Server 时收到许多 401 未经授权错误。
我们的代码在我们的测试服务器上运行了一段时间,但在生产服务器上它有时会失败。
API 密钥工作正常,推送通知到达(最终),但同时我收到很多 401 错误。
我已在控制台上验证了服务器 IP 地址在白名单中:
当关注Google Troublesooting guide 时,我得到了令人惊讶的结果:
me@myserver:~$ :curl --header "Authorization: key=AIzaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxFFU" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send -d "{\"registration_ids\":[\"ABC\"]}"
<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
me@myserver:~$ curl --header "Authorization: key=AIzaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxFFU" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send -d "{\"registration_ids\":[\"ABC\"]}"
{"multicast_id":7560936933053964665,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
me@myserver:~$ curl --header "Authorization: key=AIzaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxFFU" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send -d "{\"registration_ids\":[\"ABC\"]}"
<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
有时,相同的curl 命令可以工作,有时它没有任何更改,也没有在控制台或命令行中......因为有时它可以工作,我认为密钥没问题(我已经仔细检查过密钥和允许的 IP 数次)。
我检查了所有 stackoverflow 问题并测试了所有建议,但即使从白名单中删除所有 IP(以允许来自任何 IP 的请求),行为仍然相同,有时它有效,有时它不t
我检查过的一些参考资料:
- GCM with PHP (Google Cloud Messaging)
- GCM http 401 authorization error
- Unreliable 401 errors with GCM services
- GCM send message fails with 401 (Unauthorized)
非常感谢任何关于可能发生的事情的建议。
【问题讨论】:
标签: authentication google-api google-cloud-messaging