GCM http 401 授权错误 [英] GCM http 401 authorization error

查看:22
本文介绍了GCM http 401 授权错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的后端服务器向 GCM 服务器发送 post 请求时,我收到 HTTP 401 授权错误.

When my backend server sends post requests to GCM servers I get an authorization error HTTP 401.

我按照此处描述的步骤操作:

I followed the steps described here:

http://developer.android.com/google/gcm/http.html#auth_error

>> api_key=AIzaSyDEy3...

>> curl --header "Authorization: key=$api_key" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send  -d "{"registration_id":"ABC"}"

我明白了:

<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>

在故障排除中它说:

Authentication Error
The sender account that you're trying to use to send a message couldn't be authenticated. Possible causes are:
    1. Authorization header missing or with invalid syntax.
    2. Invalid project number sent as key.
    3. Key valid but with GCM service disabled.
    4. Request originated from a server not whitelisted in the Server Key IPs.

我对此有疑问:

  1. curl 请求中的标头是否正确?
  2. 它们是指api_key"(AIzaSy...)还是项目编号,例如 8305134....
  3. 如何将我的服务器密钥 IP 列入白名单?
  4. 如果我正在运行 curl 命令,是否必须将计算机的 IP 地址列入白名单?

推荐答案

它们是指api_key"(AIzaSy...)还是项目编号,例如 8305134....

Do they mean "api_key" (AIzaSy…) or the Project number like 8305134….?

这意味着应该添加到您的 curl 脚本中的服务器密钥.项目编号不应用于服务器端发送消息.

要创建新的服务器密钥,请按照以下步骤操作

It means Server key which should added in your curl script. The project number should not be used for sending message on server side.

To create the new server key, follow the steps,

  • 转到https://cloud.google.com/console/project
  • 点击您的项目
  • 转到APIs &左侧面板上的身份验证
  • 在左侧面板的子菜单中选择凭据
  • 公共 API 访问下,点击创建新密钥"
  • 选择服务器"
  • 在下一个对话框中,指定列入白名单的 IP(如果有).将其留空,请求将来自任何 IP
  • 点击创建.
  • 您会发现在公共 API 访问"下创建了新的服务器密钥
  • Go to https://cloud.google.com/console/project
  • Click your Project
  • Go to APIs & auth on Left Panel
  • Select Credentials in submenu on Left Panel
  • Under Public API access, click on "Create New Key"
  • Choose 'Server'
  • On next dialog, Specify the whitelisted IPs, if you have any. Leaving it blank, the requests will be from any IP
  • Click Create.
  • You will find the new server key is created under "Public API Access"

此密钥用于在服务器端的脚本中发送消息.

This key is to be used for sending message in your script on server side.

如何将我的服务器密钥 IP 列入白名单?

How do I whitelist my Server Key IP?

您可以在创建服务器密钥时加入白名单.

You can whitelist while creating server key.

如果我正在运行 curl 命令,我是否必须将计算机的 IP 地址列入白名单?

If I'm running the curl command, do I have to whitelist the IP-Address of my computer?

出于测试目的,您不必指定 IP.出于商业目的,您可能需要.

For testing purpose, you dont have to specify the IP. For commercial purpose, you may want to.

警告入门指南中所述,将 IP 设置为 0.0.0.0/0 将允许所有 IPv4 地址,但不允许 IPv6 地址.

Warning setting IP to 0.0.0.0/0 as mentioned in the Getting Started guide will allow all IPv4 address, but not IPv6.

这篇关于GCM http 401 授权错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆