HTTP Status Code: 401 in GCMDemo

2019-01-15 00:39发布

问题:

Reference:http://developer.android.com/google/gcm/demo.html

Server 401 when trying to send a message to my android device.

HTTP Status 500 - HTTP Status Code: 401

type Exception report

message HTTP Status Code: 401

description The server encountered an internal error (HTTP Status Code: 401) that        prevented it from fulfilling this request.

exception

com.google.android.gcm.server.InvalidRequestException: HTTP Status Code: 401
com.google.android.gcm.server.Sender.sendNoRetry(Sender.java:177)
com.google.android.gcm.server.Sender.send(Sender.java:121)
com.google.android.gcm.demo.server.SendAllMessagesServlet.doPost(SendAllMessagesServ    let.java:83)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.29 logs.

Could anybody tell me how to solve?Thank you!

回答1:

You should take a look at the GCM docs where it explains the GCM response: http://developer.android.com/google/gcm/gcm.html#response and troubleshooting the 401 error code: http://developer.android.com/google/gcm/gcm.html#auth_error

Description from the docs:

Authentication Error 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 number sent as key.

Key valid but with GCM service disabled.

Request originated from a server not whitelisted in the Server Key IPs.

So I would check to make sure that you are setting you authorization header properly and that you Google Project number is properly setup with GCM and accepting your servers IP.