Server error 401 when sending a message

3,714 views
Skip to first unread message

Joe Butt

unread,
Jul 22, 2012, 7:34:20 PM7/22/12
to andro...@googlegroups.com
Hello.
 
I have used the sample code for the gcm-demo-appengine and uploaded it to my .appspot.com site.  I was getting a null reference error until I changed this line in the SendMessageServlet.java file:
 

//sender = newSender(config);

sender = new Sender(ApiKeyInitializer.ATTRIBUTE_ACCESS_KEY);

 

Now it will atleast try to send but the log shows that it is getting a http 401 error code.  So I tried both the server and browser app api keys, and I reset them both and tried them again.  I also double checked the project Id number matches my phone app.  So I am wondering if it has something to do with the fact that I had to change the sender constructor line to get it to run at all??  Here is a snippet from the appspot log:

  1. com.google.android.gcm.server.Sender sendNoRetry: Request body: registration_id=APA91bH6KShZMvEb_Nxb2aLFmKGua4W9SFpkLawPtON2BAdBuW-r5G3ILPWrD9kqGy6KPmXR9_forB_YvVsIk3ThQxATEAnbGC_3065cjntEmvQ9AtSzfeRihsG1Tyf7zK2YUD9j5ArDIesWUn6_mGDrAC1TgY7WGw
    
  1. D 2012-07-22 18:17:40.088
    com.google.android.gcm.server.Sender post: Sending POST to https://android.googleapis.com/gcm/send
    
  1. D 2012-07-22 18:17:40.088
    com.google.android.gcm.server.Sender post: POST body: registration_id=APA91bH6KShZMvEb_Nxb2aLFmKGua4W9SFpkLawPtON2BAdBuW-r5G3ILPWrD9kqGy6KPmXR9_forB_YvVsIk3ThQxATEAnbGC_3065cjntEmvQ9AtSzfeRihsG1Tyf7zK2YUD9j5ArDIesWUn6_mGDrAC1TgY7WGw
    
  1. E 2012-07-22 18:17:40.251
    com.joebutt.mouseworldgcm.SendMessageServlet sendSingleMessage: Exception posting Message()
    com.google.android.gcm.server.InvalidRequestException: HTTP Status Code: 401
    	at com.google.android.gcm.server.Sender.sendNoRetry(Sender.java:177)
    	at com.joebutt.mouseworldgcm.SendMessageServlet.sendSingleMessage(SendMessageServlet.java:121)
    	at com.joebutt.mouseworldgcm.SendMessageServlet.doPost(SendMessageServlet.java:103)
    

 

 
I would really appreciate any help!!
 
Joe

Felipe Leme

unread,
Jul 23, 2012, 1:57:48 PM7/23/12
to andro...@googlegroups.com
Joe,

Your code is creating a Sender object passing "apiKey" (the value of ApiKeyInitializer.ATTRIBUTE_ACCESS_KEY) as the key, which will cause a 401. If you want to use the constructor directly, you should hardcode string the key into the Sender() call.

When you use newSender(), it gets the key from the AppEngine datastore. When you run the server for the first time, it will create a key with the value "replace_this_text_by_your_Simple_API_Access_key" ; you should then go to Datastore Viewer and change it. 

-- Felipe

Joe Butt

unread,
Jul 23, 2012, 4:54:52 PM7/23/12
to andro...@googlegroups.com
Felipe,
 
Thank you very much!! I have it working now.
 
Joe

Dmitry

unread,
Oct 12, 2012, 4:09:59 AM10/12/12
to andro...@googlegroups.com
Felipe.

I have the same problem as Joe Butt. I'm running gcm-demo server on local machine under Windows XP using jetty or tomcat servers. But unfortunately i can't find key entry in AppEngine datastore. I thought that in that case structure as: sender = new Sender("my_key_string_here"); will work but i still has the same 401 error. I even created a string entry "key" in datastore with my key value but still got the same error. Please give me any hint what i can read or fix to make this app working? I'm new in java and in programming for android also.

Thank u in advance. Best regards.

понедельник, 23 июля 2012 г., 20:57:48 UTC+3 пользователь Felipe Leme написал:
Reply all
Reply to author
Forward
0 new messages