Android GCM http 401 授权错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20521459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:58:16  来源:igfitidea点击:

GCM http 401 authorization error

androidpush-notificationgoogle-cloud-messaginghttp-status-code-401

提问by Pawel

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

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

I followed the steps described here:

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

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

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\"}"

I get this:

我明白了:

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

In the troubleshooting it says:

在故障排除它说:

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.

I have questions about that:

我对此有疑问:

  1. Is the header in the curl request correct?
  2. Do they mean "api_key" (AIzaSy…) or the Project number like 8305134….?
  3. How do I whitelist my Server Key IP?
  4. If I'm running the curl command, do I have to whitelist the IP-Address of my computer?
  1. curl 请求中的标头是否正确?
  2. 它们的意思是“api_key”(AIzaSy...)还是像 8305134...这样的项目编号?
  3. 如何将我的服务器密钥 IP 列入白名单?
  4. 如果我正在运行 curl 命令,是否必须将计算机的 IP 地址列入白名单?

回答by Vijay C

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

它们的意思是“api_key”(AIzaSy...)还是像 8305134...这样的项目编号?

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,

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

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

  • Go to https://cloud.google.com/console/project
  • Click your Project
  • Go to APIs & authon 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"
  • 转到https://cloud.google.com/console/project
  • 单击您的项目
  • 转到左侧面板上的API 和身份验证
  • 在左侧面板的子菜单中选择凭据
  • 公共 API 访问下,单击“创建新密钥
  • 选择“服务器”
  • 在下一个对话框中,指定列入白名单的 IP(如果有)。将其留空,请求将来自任何 IP
  • 单击创建
  • 您会发现在“公共 API 访问”下创建了新的服务器密钥

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

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

How do I whitelist my Server Key IP?

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

You can whitelist while creating server key.

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

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

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

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

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

Warningsetting IP to 0.0.0.0/0as mentioned in the Getting Startedguide will allow all IPv4 address, but not IPv6.

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

回答by Maximiliano De Lorenzo

UPDATE:

更新:

Google has changed the console, now you have to:

Google 更改了控制台,现在您必须:

  • Go to https://console.developers.google.com
  • Select your project (dropdown at top)
  • Select Overviewin menu on Left Panel
  • Select Google Cloud Messagingunder Mobile APIstitle
  • Click Enablebutton
  • Then you have to create the credentials
  • 转到https://console.developers.google.com
  • 选择您的项目(顶部的下拉菜单)
  • 在左侧面板的菜单中选择概览
  • Mobile APIs标题下选择Google Cloud Messaging
  • 单击启用按钮
  • 然后你必须创建凭据

DEPRECATED

已弃用

I had the same problem ... then I realized that I had to add the Google Cloud Messaging for Androidtoo.

我遇到了同样的问题……然后我意识到我也必须为 Android添加Google Cloud Messaging

  • 转到https://cloud.google.com/console/project
  • 单击您的项目
  • 转到左侧面板上的API 和身份验证
  • 在左侧面板的子菜单中选择API
  • 搜索适用于 Android(或/和 Chrome)的 Google Cloud Messaging并将其打开

Hope it helps you!

希望对你有帮助!

回答by Darsshan

I solved this error by using the Server Key instead of the API Key. You can get the Server Key from Firebase Console by clicking on the gear and clicking on Project Settings, after that go to the tab that says Cloud Messaging and get the server key, both the normal server key and the legacy server key will work.

我通过使用服务器密钥而不是 API 密钥解决了这个错误。您可以通过单击齿轮并单击项目设置从 Firebase 控制台获取服务器密钥,然后转到显示云消息传递的选项卡并获取服务器密钥,普通服务器密钥和旧服务器密钥都可以使用。

This works on both Firebase and GCM so we do not need to declare two keys separately.

这适用于 Firebase 和 GCM,因此我们不需要分别声明两个键。

回答by sree

Finally found My problem

终于找到我的问题

I made HTTP Authorization Header like this Authorization:[Api key]

Solution

I was missing 'key='+[Your Api key] in the Authorization header,

我制作了这样的 HTTP 授权标头 Authorization:[Api key]

解决方案

我在 Authorization 标题中缺少 'key='+[Your Api key],

so the full HTTP header will be

所以完整的 HTTP 标头将是

Authorization:key=Apikey

Authorization:key=Apikey

回答by Vijay Vepakomma

I had the same problem. I was using the Android Keyas mentioned in the instructions at http://developer.android.com/google/gcm/gs.htmlBut I actually had to use the Server Keyto get authenticated from my Fiddler window and PushSharp example.

我有同样的问题。我正在使用http://developer.android.com/google/gcm/gs.html上的说明中提到的Android 密钥, 但实际上我必须使用服务器密钥才能从我的 Fiddler 窗口和 PushSharp 示例中进行身份验证。

回答by Eran

  1. I don't know the syntax of the curlcommand, but it seems strange that one header you specify as --header "key: value"while the other is specified as --header key:"value". Perhaps one of them is incorrect. In addition, you should change registration_id:ABCto "{\"registration_ids\":[\"ABC\"]}".

  2. They mean the API Key.

  3. If you didn't define a list of white listed IPs when you created your API Key, it should be white listed by default.

  1. 我不知道curl命令的语法,但是您指定的一个标头为,--header "key: value"而另一个标头指定为--header key:"value". 也许其中之一是不正确的。此外,您应该更改registration_id:ABC"{\"registration_ids\":[\"ABC\"]}".

  2. 它们的意思是 API 密钥。

  3. 如果您在创建 API Key 时未定义白名单 IP,则默认情况下应将其列入白名单。

回答by ENSATE

I had same issue. What I did is:

我有同样的问题。我所做的是:

  1. Clear IP Whitelist that will be set to Any IP automatically.
  2. Regenerate Server Key and use the new generated Key.
  1. 清除将自动设置为任何 IP 的 IP 白名单。
  2. 重新生成服务器密钥并使用新生成的密钥。

回答by Pelister

If you have recently tried to create a new project in GCM and add a server key, it won't succeed authorization anyway because Google stopped authorizing newly created server keys in order to promote Firebase Cloud Messaging. Currently the only option to create a new key is to first migrate the existing project to Firebase here.

如果您最近尝试在 GCM 中创建新项目并添加服务器密钥,则无论如何都不会成功授权,因为 Google 停止授权新创建的服务器密钥以推广 Firebase 云消息传递。目前,创建新密钥的唯一选择是首先将现有项目迁移到 Firebase此处

回答by Matoeil

if error 401: the GCM has to be imported in FCM to work Then the server and sender IDs can be found at https://console.firebase.google.com/project/my_projectID/settings/cloudmessaging

如果错误 401:必须在 FCM 中导入 GCM 才能工作然后可以在https://console.firebase.google.com/project/my_projectID/settings/cloudmessaging找到服务器和发件人 ID

回答by abdimuna

The problem is related to authorization, but I came across this, error while I was trying to do things in old way, I know this is an old question, but let me put my 50 cents on it. First there is a new kid in town called "Firebase Cloud Messaging FCM". Allow me to quote google:

问题与授权有关,但我在尝试以旧方式做事时遇到了这个错误,我知道这是一个老问题,但让我把 50 美分放在上面。首先,镇上有一个名为“Firebase Cloud Messaging FCM”的新孩子。请允许我引用谷歌:

FCM is the new version of GCM under the Firebase brand. It inherits GCM's core infrastructure, with new SDKs to make Cloud Messaging development easier.

Benefits of upgrading to FCM SDK include:

Simpler client development. You no longer have to write your own registration or subscription retry logic. An out-of-the-box notification solution. You can use Firebase Notifications, a serverless notifications solution with a web console that lets anyone send notifications to target specific audiences based on Firebase Analytics insights. More information about FCMConfiguration For android studio

FCM 是 Firebase 品牌下 GCM 的新版本。它继承了 GCM 的核心基础架构,带有新的 SDK,使 Cloud Messaging 开发更容易。

升级到 FCM SDK 的好处包括:

更简单的客户端开发。您不再需要编写自己的注册或订阅重试逻辑。开箱即用的通知解决方案。您可以使用 Firebase Notifications,这是一种带有 Web 控制台的无服务器通知解决方案,任何人都可以根据 Firebase Analytics 洞察力向目标特定受众发送通知。 有关 android studio 的FCM配置的更多信息

I hope this helps someone who is trying to do things in old way

我希望这可以帮助那些试图以旧方式做事的人