【问题标题】:Unauthorized error while sending push notification in GCM在 GCM 中发送推送通知时出现未经授权的错误
【发布时间】:2016-01-11 16:02:30
【问题描述】:

您好,我正在尝试通过 GCM 从服务器(应用程序服务器)向移动设备发送推送通知消息。我尝试了下面的代码,但得到“连接被拒绝错误”,但是当我使用 Web 服务测试器进行测试时,我得到了附件中的“未经授权的错误”。我正在使用服务器 API 密钥,没有 IP 地址被列入白名单。我已经为 Android API 启用了谷歌云消息传递,也不知道原因。请帮忙。 web service tester error screenshot

代码:

        String google_server_key="";
        ResourceBundle rb = ResourceBundle.getBundle("GCMServerKey");
        Enumeration <String> keys = rb.getKeys();
        while (keys.hasMoreElements()) 
        {
            google_server_key = keys.nextElement();

        }

        try
        {

        String pushNotificationMessage="nagesh";
        String regID="APA91bEZD2MFH7TzMvry1UR2ezqTSAwCbqRgIZ8qXo_h559TYId29j3u2zPyHzxiZrA65tVhtPlY9O77LnLRbqJf585YmwYccBrGoQ5OjPAz4xT_JfIGJ1AEyjq9EUEhawc2V4kMrIar";              //pushMessage.getRegId();
        Sender sender = new Sender(google_server_key);
        Message message = new Message.Builder().timeToLive(30)
                .delayWhileIdle(true).addData(MESSAGE_KEY, pushNotificationMessage).build();

         Result result = sender.send(message,regID , 1);

        }
        catch (IOException ioe) 
        {
            ioe.printStackTrace();

        }
        catch (Exception e) 
        {
            e.printStackTrace();

        }   

【问题讨论】:

  • 检查发件人 sender = new Sender(google_server_key); google_server_key 的行。大多数时候,未经授权意味着 server_key 是错误的

标签: android google-cloud-messaging


【解决方案1】:

这是Downstream message error response code 文档。

身份验证错误 401

无法验证用于发送消息的发件人帐户。可能的原因有:

  • HTTP 请求中缺少授权标头或语法无效。
  • 作为密钥发送的项目编号无效。
  • 密钥有效,但 GCM 服务已禁用。
  • 请求来自未在服务器密钥 IP 中列入白名单的服务器。

检查您在 Authentication 标头中发送的令牌是否是与您的项目关联的正确 API 密钥。详情请见Checking the validity of an API Key

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 2015-12-21
    相关资源
    最近更新 更多