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

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

问题描述

我为Android应用程序创建了一个GCM推送通知的Android API密钥。从最近两天开始,GCM服务器在PHP中返回Unauthorized(401)响应。

Android API密钥是否有任何理由无效/过期?或者Android API密钥被GCM弃用?



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



为什么不在GCM中使用Android API密钥? Android API密钥有任何限制吗?或者,如果通知消息超出每日限制,GCM服务器是否阻止了通知/ GCM服务器正在使Android API密钥无效? strong>更新:所使用的术语回到 服务器密钥

就在最近,现在正在推出一个 Firebase云消息传递令牌(FCM令牌)。这在Firebase控制台的云消息传递标签中可见。


$ b

更新:现在在 GCM文档说:


从2016年9月起,只能使用 Cloud Messaging选项卡。需要创建新服务器密钥的现有项目可以在 Firebase控制台中导入,而不会影响其现有配置。






$ b 更新:似乎<强>从GCM迁移到FCM 可解决401未授权错误的问题。如果您刚刚开始使用GCM ,则不需要在Google Developers Console中创建项目,而是在Firebase控制台中执行该项目。 创建项目后,只需使用自动生成的服务器密钥即可。以下是查找服务器密钥的步骤:


  1. 转到您的 Firebase Console 并点击 CREATE NEW PROJECT

  2. 填写您想要的项目名称并选择您的国家。然后在左侧面板上,点击齿轮按钮,然后选择项目设置


  3. 然后转到云消息标签对于旧的GCM项目,您可以简单地将项目导入到Firebase控制台:


    1. 转到您的 Firebase Console 并点击 IMPORT PROJECT

    2. 选择您要导入的项目和您的国家。
    3. 点击添加火灾。然后在左侧面板上,点击齿轮按钮,然后选择项目设置

      然后转到云消息标签 hr>

      由于某些原因,现在只有服务器密钥可以与GCM一起工作。 Android Key并不是唯一一个看起来无效的Client Key(浏览器,iOS,Android)。

      $ b

      关于服务器密钥的描述:
      $ b


      如果您的应用程序在服务器上运行,请创建并使用Server键。


      blockquote>

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



      如果您将 FCM文档与< a href =https://developers.google.com/cloud-messaging/gcm#key =noreferrer> GCM文档(位于 凭证 ),您可以看到在GCM中,它只声明了API密钥,而在FCM中,它已经指定了Server密钥。那么,FCM是GCM的更新版本,可能与它有什么关系呢?我不是很确定,但是我认为你知道我要说的是什么。



      更新:在GCM文档中,服务器密钥现在显示。






      默认情况下,通过配置您的API项目步骤,生成一个服务器API密钥(我通过选择Android应用程序测试它)。问题同 firebase-cloud-消息。每当我创建一个新的项目,它只是自动生成一个服务器密钥。



      希望将来会有一个解释为什么。 b




      要继续前进,并添加如何创建服务器密钥的步骤。以防万一他人混淆,或新手不知道如何。


      1. 转到您的 Google Developers Console

      2. 在左侧窗格中,点击 Credentials

      3. 在凭据标签下,点击创建凭证

      4. 选择 API密钥
      5. $


        $ b $更新在开发者控制台切入步骤 ---


        1. 选择服务器密钥

        2. 填写详细信息。 b
        3. 点击创建

        服务器密钥应该可用。






        更新:生成API密钥似乎最近发生了变化。直到上面提到的第4步,这是一样的。但是,选择API密钥后,它将直接创建一个API密钥,而不必询问它是什么类型的密钥(服务器,Android,浏览器,iOS)。它只会允许你设置一些可见的 限制 ,具体取决于你想要生成的API密钥。






        更新:创建无限制的API密钥时,会显示通知(感叹号),表示您的API密钥易受攻击,这就是为什么强烈建议您为您的API密钥添加限制。一个文章有这个问题,并添加一个限制能够解决它。


        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.

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

        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.

        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.

        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.


        Update: There is now a visible note in the GCM docs saying:

        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.


        Update: It would also seem that Migrating from GCM to FCM fixes the issue for 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. 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.
        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.

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

        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.


        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.


        When pertaining to the Server Key's description:

        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.

        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.

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


        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 . 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
        2. On the left-pane, click on Credentials
        3. Under the Credentials Tab, Click on Create Credentials
        4. Select API Key

        ---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. 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.


        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天全站免登陆