java - Google GCM 中的 HTTP 响应代码 401

标签 java google-cloud-messaging

下面是我得到的异常,即使我的服务器和浏览器应用程序的 API key 是有效的。我用 curl 检查了它。我以 UTF-8 和 JSON 两种格式发送 GCM 请求。从外部公司网络对其进行测试。

java.io.IOException: Server returned HTTP response code: 401 for URL: https://android.googleapis.com/gcm/send
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1345)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1339)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:993)
        at java.net.URLConnection.getContent(URLConnection.java:688)
        at sun.net.www.protocol.https.HttpsURLConnectionImpl.getContent(HttpsURLConnectionImpl.java:406)
        at gcm1.MessageUtil.sendMessage(MessageUtil.java:58)

请问这是什么原因和解决方法?

最佳答案

以下是使用 GCM 时出现 401 错误的可能原因:

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

  • 授权 header 缺失或语法无效。
  • 作为 key 发送的项目 ID 无效。
  • key 有效但 GCM 服务已禁用。
  • 请求来自未列入服务器 key IP 白名单的服务器。

检查您在授权 header 中发送的 token 是否是与您的项目关联的正确 API key 。

来源:https://developers.google.com/cloud-messaging/http-server-ref#error-codes

关于java - Google GCM 中的 HTTP 响应代码 401,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11900855/

相关文章:

java - For 循环内的 For 循环 - ListArrays - Android、Java!

java - 使用 Java 代码中的不同引号执行 Bash 单行代码以进行 Elasticsearch 查询

Firebase 云消息传递 : Notification_key invalidated when removing one device among many

javascript - 从 GAS 发送 GCM 请求时为 "JSON_PARSING_ERROR: Unexpected character (d) at position 0."

java - 从java中的xpath表达式中提取所有变量引用的最安全方法

java - 带有 JMS 队列的同步消费者

java - Android 套接字 : listening doesn't work through 4G

android - 无法使用 GCM 在设备上接收消息

android - android P 中的 GCM 推送通知问题 (28)

android - 在android中创建推送通知