title: How to Troubleshoot Android GCM 401 error?
body: <p>I'm getting many <code>401</code> <strong>Unauthorized</strong> errors when trying to connect to Google Cloud Message Server. </p>
  
  <p>Our code was working for some time on our test server but on the production one it fails some times.</p>
  
  <p>The API key was working ok, the push notifications arrive (eventually), but meanwhile I get a lot of <code>401</code> errors. </p>
  
  <p>I've verified on the console that <strong>server IP address</strong> is on the <strong>white list</strong>:</p>
  
  <p>When following <a href="http://developer.android.com/google/gcm/http.html#auth_error" rel="nofollow noreferrer">Google Troublesooting guide</a>, I get surprising results:</p>
  
  <pre><code>me@myserver:~$ :curl --header "Authorization: key=AIzaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxFFU" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send  -d "{\"registration_ids\":[\"ABC\"]}"
  &lt;HTML&gt;
  &lt;HEAD&gt;
  &lt;TITLE&gt;Unauthorized&lt;/TITLE&gt;
  &lt;/HEAD&gt;
  &lt;BODY BGCOLOR="#FFFFFF" TEXT="#000000"&gt;
  &lt;H1&gt;Unauthorized&lt;/H1&gt;
  &lt;H2&gt;Error 401&lt;/H2&gt;
  &lt;/BODY&gt;
  &lt;/HTML&gt;
  me@myserver:~$ curl --header "Authorization: key=AIzaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxFFU" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send  -d "{\"registration_ids\":[\"ABC\"]}"
  {"multicast_id":7560936933053964665,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
  me@myserver:~$ curl --header "Authorization: key=AIzaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxFFU" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send  -d "{\"registration_ids\":[\"ABC\"]}"
  &lt;HTML&gt;
  &lt;HEAD&gt;
  &lt;TITLE&gt;Unauthorized&lt;/TITLE&gt;
  &lt;/HEAD&gt;
  &lt;BODY BGCOLOR="#FFFFFF" TEXT="#000000"&gt;
  &lt;H1&gt;Unauthorized&lt;/H1&gt;
  &lt;H2&gt;Error 401&lt;/H2&gt;
  &lt;/BODY&gt;
  &lt;/HTML&gt;
  </code></pre>
  
  <p>Sometimes, the same <code>curl</code> command works and sometimes it doesn't without any change nor in the console nor in the command line... As sometimes it works, I asume the key it's ok (I've double checked the key and the allowed IPs several times).</p>
  
  <p>I have checked all the stackoverflow questions and tested all the suggestions, but even removing all the IPs from the white list (to allow requests from any IP) the behavior is still the same, some times it works some times it doesn't</p>
  
  <p>Some references I've checked:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/11242743/gcm-with-php-google-cloud-messaging">GCM with PHP (Google Cloud Messaging)</a></li>
  <li><a href="https://stackoverflow.com/questions/20521459/gcm-http-401-authorization-error">GCM http 401 authorization error</a></li>
  <li><a href="https://stackoverflow.com/questions/22063043/unreliable-401-errors-with-gcm-services">Unreliable 401 errors with GCM services</a></li>
  <li><a href="https://stackoverflow.com/questions/17170009/gcm-send-message-fails-with-401-unauthorized">GCM send message fails with 401 (Unauthorized)</a></li>
  </ul>
  
  <p>Any advice on what can be going on would be very appreciated.</p>