简体   繁体   English

GCM的API密钥突然无效? 未经授权(401)错误

[英]API key for GCM is suddenly invalid? Unauthorized (401) error

I created an Android API key for GCM Push Notification for my Android application.From the last two days, GCM server returns Unauthorized (401) response in PHP. 我为我的Android应用程序创建了一个用于GCM推送通知的Android API密钥。从过去两天开始,GCM服务器在PHP中返回Unauthorized(401)响应。

Is there any reason for Android API key invalid/expire? Android API密钥是否有任何原因无效/过期? Or Is Android API key deprecated by GCM? 或者GCM是否弃用了Android API密钥?

Still I'm not getting proper explanation regarding Why Android API key is invalid? 我还没有得到关于为什么Android API密钥无效的正确解释? Most of them prefer Server key instead of Android API key for GCM but not mentioning as to why not use Android API key. 他们中的大多数人更喜欢使用服务器密钥而不是GCM的Android API密钥,但没有提到为什么不使用Android API密钥。

Why do not use Android API Key in GCM? 为什么不在GCM中使用Android API Key? Is there any limitation for Android API key? Android API密钥有任何限制吗? Or Is GCM server blocks the notification/GCM server is making Android API key invalid if notification message exceeds per day limit? 或者,如果通知消息超过每日限制,GCM服务器是否阻止通知/ GCM服务器使Android API密钥无效?

Update : The term being used is back to Server Key . 更新 :使用的术语是返回服务器密钥

Just recently, there is now a Firebase Cloud Messaging Token (FCM Token) that is being rolled out. 就在最近,现在正在推出Firebase云消息传递令牌 (FCM令牌)。 This is visible in the Cloud Messaging tab in your Firebase Console. 这在Firebase控制台的Cloud Messaging选项卡中可见。


Update : There is now a visible note in the GCM docs saying: 更新GCM文档中现在有一个明显的说明:

Starting from Sept. 2016 new server key can only be created in the Firebase Console using the Cloud Messaging tab of the Settings panel. 从2016年9月开始,只能使用“设置”面板的“ 云消息传递”选项卡在Firebase控制台中创建新的服务器密钥。 Existing projects that need to create a new server key can be imported in the Firebase console without affecting their existing configuration. 可以在Firebase控制台中导入需要创建新服务器密钥的现有项目,而不会影响其现有配置。


Update : It would also seem that Migrating from GCM to FCM fixes the issue for 401 Unauthorized Error. 更新 :似乎从GCM迁移到FCM修复了401 Unauthorized Error的问题。

If you are just starting to use GCM , instead of creating a project in the Google Developers Console, do it in the Firebase Console. 如果您刚刚开始使用GCM ,而不是在Google Developers Console中创建项目,请在Firebase控制台中执行此操作。 After creating the project, simply use the auto-generated Server Key. 创建项目后,只需使用自动生成的服务器密钥。 Here are the steps where to find the Server Key: 以下是查找服务器密钥的步骤:

  1. Go to your Firebase Console and click on CREATE NEW PROJECT . 转到Firebase控制台 ,然后单击“ 创建新项目”
  2. Fill in your desired Project Name and select your Country. 填写您想要的项目名称并选择您的国家/地区。 After this, the new Project should be active. 在此之后,新项目应该是活跃的。
  3. Then on left-side panel, click on the gear button and select Project Settings . 然后在左侧面板上,单击齿轮按钮并选择“ 项目设置”
  4. Then go to the Cloud-Messaging Tab . 然后转到Cloud-Messaging选项卡

For old GCM projects , you can simply Import the project to the Firebase Console: 对于旧的GCM项目 ,您只需将项目导入Firebase控制台:

  1. Go to your Firebase Console and click on IMPORT PROJECT . 转到Firebase控制台 ,然后单击IMPORT PROJECT
  2. Select the project you want to import and your country. 选择要导入的项目和您所在的国家/地区。
  3. Click on ADD FIREBASE . 单击ADD FIREBASE After this, the new Project should be active. 在此之后,新项目应该是活跃的。
  4. Then on left-side panel, click on the gear button and select Project Settings . 然后在左侧面板上,单击齿轮按钮并选择“ 项目设置”
  5. Then go to the Cloud-Messaging Tab . 然后转到Cloud-Messaging选项卡

For some reason, only a Server Key works now with GCM. 出于某种原因,现在只有一个服务器密钥可用于GCM。 The Android Key isn't the only one that seems to be invalid, all other Client API keys (Browser, iOS, Android) are. Android Key并不是唯一似乎无效的,所有其他客户端API密钥(浏览器,iOS,Android)都是。


When pertaining to the Server Key 's description: 当涉及Server Key的描述时:

Create and use a Server key if your application runs on a server. 如果应用程序在服务器上运行,请创建并使用服务器密钥。

It is logical to use a Server key since you are using the API key on your Server, and it is working with GCM Connection server. 使用服务器密钥是合乎逻辑的,因为您在服务器上使用API​​密钥,并且它正在与GCM连接服务器一起使用。

If you compare the FCM docs with the GCM docs (under Credentials ), you can see that in GCM, it only states API key, while in FCM, it already specifies a Server key. 如果将FCM文档GCM文档 (在凭据下)进行比较,则可以看到在GCM中,它仅表示API密钥,而在FCM中,它已指定服务器密钥。 Well, FCM is the newer version of GCM, may be it has something to do with it? 那么,FCM是GCM的新版本,可能与它有关吗? I'm not so sure, but I think you get the gist of what I'm saying. 我不太确定,但我认为你得到了我所说的要点。

Update : In the GCM docs, Server Key is now indicated. 更新 :在GCM文档中,现在指示了服务器密钥。


By default, after going through the Configure your API Project steps, a Server API key is generated (I tested it by selecting Android app). 默认情况下,在完成配置API项目步骤后,将生成服务器API密钥(我通过选择Android应用程序对其进行了测试)。 Same goes with . Whenever I create a new project, it just generates a Server Key automatically. 每当我创建一个新项目时,它只会自动生成一个服务器密钥。

Hopefully there will be an explanation in the future as to why. 希望将来能解释原因。


Gonna go on ahead and add the steps on how to create a Server Key. 继续前进并添加有关如何创建服务器密钥的步骤。 Just in case others are confused, or newbies are not sure how. 以防其他人感到困惑,或者新手不确定如何。

  1. Go to your Google Developers Console 转到您的Google Developers Console
  2. On the left-pane, click on Credentials 在左窗格中,单击“ 凭据”
  3. Under the Credentials Tab, Click on Create Credentials 在“凭据”选项卡下,单击“ 创建凭据”
  4. Select API Key 选择API密钥

--- Update in the Developers Console cuts the step here --- --- 开发人员控制台中的更新减少了这里的步骤 ---

  1. Select Server Key 选择服务器密钥
  2. Fill out the details. 填写详细信息。
  3. Click on Create 单击“ 创建”

The Server Key should be available by then. 那时服务器密钥应该可用。


Update : It seems there was a recent change when generating an API key. 更新 :生成API密钥似乎最近发生了变化。 It is the same up until Step 4 that I mentioned above. 直到我上面提到的第4步,它才一样。 However, after you select API Key, it will directly create an API key without asking what type of key (Server, Android, Browser, iOS) it is. 但是,在您选择API密钥后,它将直接创建API密钥,而不会询问它是什么类型的密钥(服务器,Android,浏览器,iOS)。 It will only allow you to set some Restrictions that was visible depending on which API Key you intend to generate. 它只允许您设置一些可见的限制 ,具体取决于您要生成的API密钥。


Update : When creating an API Key with No Restrictions, it is showing a Notice (Exclamation Point) that your API key is vulnerable, that's why it is highly encouraged for you to add a restriction for your API key. 更新 :创建没有限制的API密钥时,它会显示一个通知(感叹号),表明您的API密钥容易受到攻击,这就是为什么强烈建议您为API密钥添加限制 One post had this issue and adding a restriction was able to resolved it. 有一个帖子有这个问题,并且添加限制能够解决它。

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

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