简体   繁体   English

GCM http 401授权错误

[英]GCM http 401 authorization error

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….? 他们的意思是“api_key”(AIzaSy ......)或项目编号如8305134 ....
  3. How do I whitelist my Server Key IP? 如何将服务器密钥IP列入白名单?
  4. If I'm running the curl command, do I have to whitelist the IP-Address of my computer? 如果我正在运行curl命令,是否必须将计算机的IP地址列入白名单?

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. 它表示应在curl脚本中添加的服务器密钥。 The project number should not be used for sending message on server side. 项目编号不应用于在服务器端发送消息。

To create the new server key, follow the steps, 要创建新的服务器密钥,请按照步骤操作,

  • Go to https://cloud.google.com/console/project 转到https://cloud.google.com/console/project
  • Click your Project 单击您的项目
  • Go to APIs & auth on Left Panel 转到左侧面板上的API和auth
  • Select Credentials in submenu on Left Panel 在左侧面板的子菜单中选择凭据
  • Under Public API access , click on " Create New Key " 公共API访问下 ,单击“ 创建新密钥
  • Choose 'Server' 选择“服务器”
  • On next dialog, Specify the whitelisted IPs, if you have any. 在下一个对话框中,指定列入白名单的IP(如果有)。 Leaving it blank, the requests will be from any IP 留空,请求将来自任何IP
  • Click Create . 单击“ 创建”
  • You will find the new server key is created under " Public API Access " 您会发现新的服务器密钥是在“ 公共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. 出于测试目的,您不必指定IP。 For commercial purpose, you may want to. 出于商业目的,您可能想要。

Warning setting IP to 0.0.0.0/0 as mentioned in the Getting Started guide will allow all IPv4 address, but not IPv6. 如“ 入门指南”中所述, 警告设置IP为0.0.0.0/0将允许所有IPv4地址,但不允许IPv6。

UPDATE: 更新:

Google has changed the console, now you have to: 谷歌已经改变了控制台,现在你必须:

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

DEPRECATED 弃用

I had the same problem ... then I realized that I had to add the Google Cloud Messaging for Android too. 我遇到了同样的问题......然后我意识到我还必须为Android添加Google Cloud Messaging

  • Go to https://cloud.google.com/console/project 转到https://cloud.google.com/console/project
  • Click your Project 单击您的项目
  • Go to APIs & auth on Left Panel 转到左侧面板上的API和auth
  • Select APIs in submenu on Left Panel 在左侧面板的子菜单中选择API
  • Search Google Cloud Messaging for Android (or/and Chrome) and put it ON 搜索适用于Android(或/和Chrome)的Google Cloud Messaging并将其启用

Hope it helps you! 希望它能帮到你!

I solved this error by using the Server Key instead of the API Key. 我使用服务器密钥而不是API密钥解决了这个错误。 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. 您可以通过单击齿轮并单击“项目设置”从Firebase控制台获取服务器密钥,然后转到显示Cloud Messaging的选项卡并获取服务器密钥,普通服务器密钥和旧服务器密钥都将起作用。

This works on both Firebase and GCM so we do not need to declare two keys separately. 这适用于Firebase和GCM,因此我们不需要单独声明两个键。

Finally found My problem 终于找到了我的问题

I made HTTP Authorization Header like this Authorization:[Api key] 我做了像这个Authorization:[Api key] HTTP授权标题Authorization:[Api key]

Solution

I was missing 'key='+[Your Api key] in the Authorization header, 我在Authorization标头中缺少'key ='+ [你的Api密钥],

so the full HTTP header will be 所以完整的HTTP标头将是

Authorization:key=Apikey

I had the same problem. 我有同样的问题。 I was using the Android Key as mentioned in the instructions at http://developer.android.com/google/gcm/gs.html But I actually had to use the Server Key to get authenticated from my Fiddler window and PushSharp example. 我正在使用http://developer.android.com/google/gcm/gs.html上的说明中提到的Android Key但我实际上必须使用服务器密钥从我的Fiddler窗口和PushSharp示例进行身份验证。

  1. I don't know the syntax of the curl command, but it seems strange that one header you specify as --header "key: value" while the other is specified as --header key:"value" . 我不知道curl命令的语法,但看起来很奇怪,你指定的一个头是--header "key: value"而另一个头指定为--header key:"value" Perhaps one of them is incorrect. 也许其中一个是不正确的。 In addition, you should change registration_id:ABC to "{\\"registration_ids\\":[\\"ABC\\"]}" . 此外,您应该将registration_id:ABC更改为"{\\"registration_ids\\":[\\"ABC\\"]}"

  2. They mean the API Key. 它们指的是API密钥。

  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. 如果在创建API密钥时未定义白名单的IP列表,则默认情况下应列为白名单。

I had same issue. 我有同样的问题。 What I did is: 我做的是:

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

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. 如果您最近尝试在GCM中创建新项目并添加服务器密钥,则无论如何都不会成功授权,因为Google停止授权新创建的服务器密钥以推广Firebase云消息传递。 Currently the only option to create a new key is to first migrate the existing project to Firebase here . 目前,创建一个新的密钥的唯一的选择就是先迁移现有的项目,以火力地堡这里

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

Unauthorised issue will happen while there is a mismatch in API key. 如果API密钥不匹配,则会发生未经授权的问题。 Clear the server key and regenerate a new one. 清除服务器密钥并重新生成新密钥。 Use the server API key in server side coding. 在服务器端编码中使用服务器API密钥。 And properly use the sender id in android client 并正确使用Android客户端中的发件人ID

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. 这个问题与授权有关,但是当我试图以旧的方式做事时我遇到了这个错误,我知道这是一个老问题,但是让我把50美分放在上面。 First there is a new kid in town called "Firebase Cloud Messaging FCM". 首先,镇上有一个名为“Firebase Cloud Messaging FCM”的新孩子。 Allow me to quote google: 请允许我引用谷歌:

FCM is the new version of GCM under the Firebase brand. FCM是Firebase品牌下的新版GCM。 It inherits GCM's core infrastructure, with new SDKs to make Cloud Messaging development easier. 它继承了GCM的核心基础架构,使用新的SDK来简化云消息传递开发。

Benefits of upgrading to FCM SDK include: 升级到FCM SDK的好处包括:

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. 您可以使用Firebase Notifications,这是一种带有Web控制台的无服务器通知解决方案,可让任何人根据Firebase Analytics洞察信息向目标特定受众群体发送通知。 More information about FCM Configuration For android studio 有关FCM 配置的 更多信息 对于android studio

I hope this helps someone who is trying to do things in old way 我希望这可以帮助那些试图以旧方式做事的人

For using Firestore you should use Web API key firebase console gives you, which you can find at https://console.firebase.google.com/project/<yourProjectName>/settings/general/<yourAppPckageName> . 要使用Firestore您应该使用Web API key firebase控制台,您可以在https://console.firebase.google.com/project/<yourProjectName>/settings/general/<yourAppPckageName>找到该控制台。

but, for using Firebase to push notification your mobile device clients you should use Server key developer console gives you, which you can find at https://console.developers.google.com/apis/credentials?project=<yourProjectName> 但是,要使用Firebase推送您的移动设备客户端通知,您应该使用Server key开发人员控制台为您提供,您可以在https://console.developers.google.com/apis/credentials?project=<yourProjectName>找到

The above things are all done then check curl init operation the function 以上所有事情都完成了然后检查curl init操作的功能
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
is included or not if it is not then include the function 是否包括但如果不包含该功能

I had the same issue. 我遇到过同样的问题。 What I was doing is, in place of API key I was sending message and instead of message I was sending API key. 我正在做的是,代替API密钥,我发送消息而不是消息,我发送API密钥。 Just check if you are making mistake the same mistake. 只要检查你是否犯了同样的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM