java Google GCM 中的 HTTP 响应代码 401
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11900855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTTP response code 401 in Google GCM
提问by chako
Below is the exception I am getting even though my API key for server and browser application are valid. I checked it using curl. I send GCM request in both format UTF-8 and JSON. Testing it from outside corporate network.
下面是我得到的异常,即使我的服务器和浏览器应用程序的 API 密钥有效。我使用 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.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)
May I know the reason and solution for this ?
我可以知道原因和解决方案吗?
回答by azgolfer
Here are the possible causes for 401 error when using GCM:
以下是使用 GCM 时出现 401 错误的可能原因:
The sender account that you're trying to use to send a message couldn't be authenticated. Possible causes are:
您尝试用于发送邮件的发件人帐户无法通过身份验证。可能的原因有:
- Authorization header missing or with invalid syntax.
- Invalid project ID sent as key.
- Key valid but with GCM service disabled.
- Request originated from a server not whitelisted in the Server Key IPs.
- 授权标头丢失或语法无效。
- 作为密钥发送的项目 ID 无效。
- 密钥有效但禁用 GCM 服务。
- 请求源自未在服务器密钥 IP 中列入白名单的服务器。
Check that the token you're sending inside the Authorization header is the correct API key associated with your project.
检查您在 Authorization 标头中发送的令牌是否是与您的项目关联的正确 API 密钥。
Source: https://developers.google.com/cloud-messaging/http-server-ref#error-codes
来源:https: //developers.google.com/cloud-messaging/http-server-ref#error-codes
回答by David Vávra
In my case the problem was that I forgot to redeploy after setting correct API key in the datastore.
就我而言,问题是我在数据存储中设置了正确的 API 密钥后忘记重新部署。
回答by Nilesh Patel
Please follow as par below instruction it's may help you.
请按照以下说明进行操作,它可能对您有所帮助。
Update: It would also seem that Migrating from GCM to FCMfixes the issue for 401 Unauthorized Error.
更新:从 GCM 迁移到 FCM似乎也解决了 401 未授权错误的问题。
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:
如果您刚开始使用 GCM,请不要在 Google Developers Console 中创建项目,而是在 Firebase Console 中创建项目。创建项目后,只需使用自动生成的服务器密钥。以下是查找服务器密钥的步骤:
- Go to your Firebase Console and click on CREATE NEW PROJECT.
- Fill in your desired Project Name and select your Country. After this, the new Project should be active.
- Then on left-side panel, click on the gear button and select Project Settings.
- Then go to the Cloud-Messaging Tab.
- 转到您的 Firebase 控制台并点击创建新项目。
- 填写您想要的项目名称并选择您的国家。在此之后,新项目应该处于活动状态。
- 然后在左侧面板上,单击齿轮按钮并选择项目设置。
- 然后转到云消息选项卡。
For old GCMprojects, you can simply Import the project to the Firebase Console:
对于旧的 GCM项目,您可以简单地将项目导入 Firebase 控制台:
- Go to your Firebase Console and click on IMPORT PROJECT.
- Select the project you want to import and your country.
- Click on ADD FIREBASE. After this, the new Project should be active.
- Then on left-side panel, click on the gear button and select Project Settings.
- Then go to the Cloud-Messaging Tab.
- 转到您的 Firebase 控制台并点击导入项目。
- 选择您要导入的项目和您所在的国家/地区。
- 单击添加 FIREBASE。在此之后,新项目应该处于活动状态。
- 然后在左侧面板上,单击齿轮按钮并选择项目设置。
- 然后转到云消息选项卡。