首页 文章

如何解决Android GCM 401错误?

提问于
浏览
14

尝试连接到Google Cloud Message Server时,我收到了很多错误.2407625_ Unauthorized 错误 .

我们的代码在我们的测试服务器上运行了一段时间,但在 生产环境 的代码上它有时会失败 .

API密钥工作正常,推送通知到达(最终),但同时我得到了很多 401 错误 .

我在控制台上验证 server IP addresswhite list 上:

当关注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的请求)行为仍然是相同的,有时它有效地工作了一些时间它没有

我检查了一些参考文献:

任何有关可能发生的事情的建议都将非常感激 .

3 回答

  • 2

    在经过多次检查并且没有找到好的结果之后,我们可以给出的唯一解决方案是使用 new API key 创建一个 new project (第三个,因为我们之前已经做过),这是第三次有效 .

  • 0

    我通过在同一个项目下创建新的 API Key 解决了我的问题 . 按照此

    转到Google开发人员控制台,然后选择项目 Credentials -> Create Credentials -> API Key -> Server Key ,然后添加您的GCM服务器代码托管的服务器IP地址 .

    创建Api密钥后,可能需要5分钟才能工作

  • 4

    可能会发生IPv6用于连接,而只允许使用IPv4 . 确保您拥有'允许任何IP',而不仅仅是IPv4 0.0.0.0/0 .

相关问题