下面是我得到的异常,即使我的服务器和浏览器应用程序的 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/