未经授权的错误401 GCM错误 [英] Unauthorized error 401 GCM error

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

问题描述

注意,这个问题应该与其他类似名称的问题有很大不同,因为API在过去几年似乎发生了重大变化。



我试图使用GCM向iOS设备发送推送通知。



我有(我认为)大部分设置正确。我有一个来自GCM的API密钥,我在Apple Developer上设置了我的SSL证书,并且我的设备正确地获得了注册ID。



我有一点测试脚本写入试图测试它,我得到401未经授权的错误在我的结果。这里是我的代码:

  define('API_ACCESS_KEY','< API KEY HERE>'); 

$ fields = array(
'to'=>'< REG_ID HERE>',
'registration_ids'=>'< REG_ID HERE>',$ b'b'notification'=> array('body'=>'test','title'=>'Test'),
);

$ headers = array(
'Authorization:key ='。API_ACCESS_KEY,
'Content-Type:application / json'
);

$ ch = curl_init();

curl_setopt($ ch,CURLOPT_URL,'https://gcm-http.googleapis.com/gcm/send');

curl_setopt($ ch,CURLOPT_POST,true);
curl_setopt($ ch,CURLOPT_HTTPHEADER,$ headers);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,false);

curl_setopt($ ch,CURLOPT_POSTFIELDS,json_encode($ fields));

$ result = curl_exec($ ch);

curl_close($ ch);

我目前使用不受限制的密钥进行测试:

 此密钥不受限制。为防止未经授权的使用和配额窃取,请限制您的密码。 
密钥限制允许您指定哪些网站,IP地址或应用程序可以使用此密钥。

据我所知,这应该是白名单,不是吗?几年前,有一些关于如何将服务器列入白名单的答案,但似乎是API的旧界面/设置。

什么可能会导致此问题?

解决方案

更新:现在 GCM文档说:


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






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


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

  2. 填写您想要的项目名称并选择您的国家。在此之后,新项目应该处于活动状态。

  3. 然后在左侧面板上,点击齿轮按钮,然后选择项目设置


  4. $ b
  5. 然后转到云消息传递标签
  6. 对于旧的GCM项目,您可以简单地将项目导入到Firebase控制台: Firebase控制台并点击 IMPORT PROJECT
  7. 选择您要导入的项目和您的国家。

  8. 点击 ADD FIREBASE 。在此之后,新项目应该处于活动状态。

  9. 然后在左侧面板上,点击齿轮按钮,然后选择项目设置


  10. $ b
  11. 然后转到云消息传递标签
  12. hr>

    过去几天我已经看到很多这方面的问题。似乎无限制的键会返回401个错误,这可能是因为他们正在执行所有API键的安全性(您会看到警告)。



    因此,我通常建议使用IP地址限制(请参阅GCM,它应该是服务器密钥)并添加您的服务器IP地址。 请参阅我的答案此处


    Note, this question should be materially different from other questions with a similar name because of the fact that the API seems to have changed significantly over the past few years.

    I am trying to send a push notification to an iOS device using GCM.

    I have (I think) mostly set it up correctly. I have an API key from GCM, I've setup my SSL certificate on Apple Developer and I have my device correctly getting a registration id.

    I've got a little test script written up to try to test it and I'm getting 401 unauthorized error in my result. Here's my code:

    define('API_ACCESS_KEY', '<API KEY HERE>');
    
    $fields = array(
        'to' => '<REG_ID HERE>',
        'registration_ids' => '<REG_ID HERE>',
        'notification' => array('body' => 'test', 'title' => 'Test'),
      );
    
      $headers = array(
        'Authorization: key=' . API_ACCESS_KEY,
        'Content-Type: application/json'
      );
    
      $ch = curl_init();
    
      curl_setopt($ch, CURLOPT_URL, 'https://gcm-http.googleapis.com/gcm/send');
    
      curl_setopt($ch, CURLOPT_POST, true);
      curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    
      curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
    
      $result = curl_exec($ch);
    
      curl_close($ch);
    

    I am currently using an unrestricted key for testing purposes:

    This key is unrestricted. To prevent unauthorized use and quota theft, restrict your key. 
    Key restriction lets you specify which web sites, IP addresses, or apps can use this key.
    

    As far as I know, this should white list everything, shouldn't it? There are some answers from several years back on SO about how you have to whitelist a server, but that seems to be for the old interface/setup of the API.

    What could be causing this issue?

    解决方案

    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.


    I've been seeing a lot of this concern this past few days. It would seem that unrestricted keys return 401 errors and it may be because they are enforcing the security for all API keys (where you see the warning).

    As such, what I commonly recommend is to make use of the IP address restriction (seeing as this is for GCM, and it should be a Server Key) and add your server IP Address. See my answer here.

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

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