GCM 的 API 密钥突然无效?未经授权 (401) 错误 [英] API key for GCM is suddenly invalid? Unauthorized (401) error

查看:30
本文介绍了GCM 的 API 密钥突然无效?未经授权 (401) 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的 Android 应用程序创建了一个用于 GCM 推送通知的 Android API 密钥.从过去两天开始,GCM 服务器在 PHP 中返回未经授权 (401) 响应.

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 API 密钥无效/过期是否有任何原因?还是 GCM 已弃用 Android API 密钥?

Is there any reason for Android API key invalid/expire? Or Is Android API key deprecated by GCM?

我仍然没有得到关于为什么 Android API 密钥无效的正确解释?他们中的大多数人更喜欢 GCM 的服务器密钥而不是 Android API 密钥,但没有提到为什么不使用 Android API 密钥.

Still I'm not getting proper explanation regarding Why Android API key is invalid? 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 Key?Android API 密钥有任何限制吗?或者,如果通知消息超过每日限制,GCM 服务器是否会阻止通知/GCM 服务器是否使 Android API 密钥无效?

Why do not use Android API Key in GCM? Is there any limitation for Android API key? Or Is GCM server blocks the notification/GCM server is making Android API key invalid if notification message exceeds per day limit?

推荐答案

更新:所使用的术语又回到了服务器密钥.

Update: The term being used is back to Server Key.

就在最近,现在正在推出 Firebase 云消息传递令牌(FCM 令牌).这在 Firebase 控制台的 Cloud Messaging 选项卡中可见.

Just recently, there is now a Firebase Cloud Messaging Token (FCM Token) that is being rolled out. This is visible in the Cloud Messaging tab in your Firebase Console.

更新:现在在 GCM 文档 说:

从 2016 年 9 月开始,只能使用 云消息传递"选项卡.可以在 Firebase 控制台 中导入需要创建新服务器密钥的现有项目,而不会影响其现有配置.

Starting from Sept. 2016 new server key can only be created in the Firebase Console using the Cloud Messaging tab of the Settings panel. Existing projects that need to create a new server key can be imported in the Firebase console without affecting their existing configuration.

<小时>

更新:从 GCM 迁移到 FCM 似乎也解决了 401 未授权错误的问题.


Update: It would also seem that Migrating from GCM to FCM fixes the issue for 401 Unauthorized Error.

如果您刚开始使用 GCM,请不要在 Google Developers Console 中创建项目,而是在 Firebase Console 中创建项目.创建项目后,只需使用自动生成的服务器密钥.以下是查找服务器密钥的步骤:

If you are just starting to use GCM, instead of creating a project in the Google Developers Console, do it in the Firebase Console. After creating the project, simply use the auto-generated Server Key. Here are the steps where to find the Server Key:

  1. 转到您的 Firebase 控制台,然后点击CREATE NEW PROJECT.
  2. 填写您想要的项目名称并选择您所在的国家/地区.在此之后,新项目应该处于活动状态.
  3. 然后在左侧面板上,点击齿轮按钮并选择项目设置.
  4. 然后转到云消息传递标签.
  1. Go to your Firebase Console and click on CREATE NEW PROJECT.
  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.

对于旧的 GCM 项目,您只需将项目导入 Firebase 控制台即可:

For old GCM projects, you can simply Import the project to the Firebase Console:

  1. 转到您的 Firebase 控制台,然后点击导入项目.莉>
  2. 选择您要导入的项目和您所在的国家/地区.
  3. 点击添加FIREBASE.在此之后,新项目应该处于活动状态.
  4. 然后在左侧面板上,点击齿轮按钮并选择项目设置.
  5. 然后转到云消息传递标签.
  1. Go to your Firebase Console and click on IMPORT PROJECT.
  2. Select the project you want to import and your country.
  3. Click on 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.

<小时>

出于某种原因,现在只有服务器密钥可与 GCM 配合使用.Android 密钥并不是唯一一个似乎无效的密钥,所有其他客户端 API 密钥(浏览器、iOS、Android)都是如此.


For some reason, only a Server Key works now with GCM. The Android Key isn't the only one that seems to be invalid, all other Client API keys (Browser, iOS, Android) are.

关于服务器密钥说明:

如果您的应用程序在服务器上运行,则创建并使用服务器密钥.

Create and use a Server key if your application runs on a server.

使用服务器密钥是合乎逻辑的,因为您在服务器上使用 API 密钥,并且它与 GCM 连接服务器一起使用.

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.

如果您将 FCM 文档GCM 文档(在凭据下),你可以看到,在 GCM 中,它只声明了 API 密钥,而在 FCM 中,它已经指定了一个服务器密钥.好吧,FCM是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. Well, FCM is the newer version of GCM, may be it has something to do with it? I'm not so sure, but I think you get the gist of what I'm saying.

更新:在 GCM 文档中,现在指明了服务器密钥.

Update: In the GCM docs, Server Key is now indicated.

默认情况下,在完成配置您的 API 项目步骤后,服务器 API 密钥是生成(我通过选择 Android 应用程序对其进行了测试)..每当我创建一个新项目时,它都会自动生成一个服务器密钥.

By default, after going through the Configure your API Project steps, a Server API key is generated (I tested it by selecting Android app). Same goes with firebase-cloud-messaging. Whenever I create a new project, it just generates a Server Key automatically.

希望将来能解释原因.

继续添加有关如何创建服务器密钥的步骤.以防万一其他人感到困惑,或者新手不确定如何.

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. 转到您的 Google Developers Console
  2. 在左侧窗格中,单击 凭据
  3. 在凭据选项卡下,单击 创建凭据
  4. 选择 API 密钥

---开发者控制台中的更新减少了这里的步骤---

  1. 选择服务器密钥
  2. 填写详细信息.
  3. 点击创建

届时服务器密钥应该可用.

The Server Key should be available by then.

更新:最近在生成 API 密钥时似乎发生了变化.直到我上面提到的第 4 步之前都是一样的.但是,当您选择 API Key 后,它会直接创建一个 API Key,而不会询问它是什么类型的密钥(Server、Android、Browser、iOS).它只允许您设置一些限制,这些限制取决于您打算生成的 API 密钥.

Update: It seems there was a recent change when generating an API key. It is the same up until Step 4 that I mentioned above. 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. It will only allow you to set some Restrictions that was visible depending on which API Key you intend to generate.

更新:在创建无限制的 API 密钥时,它会显示一条通知(感叹号),表明您的 API 密钥易受攻击,这就是为什么强烈鼓励以便您为 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. One post had this issue and adding a restriction was able to resolved it.

这篇关于GCM 的 API 密钥突然无效?未经授权 (401) 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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