title: Intermittent 401 Unauthorized from Google GCM
body: <p>We are getting intermittent 401 Unauthorized errors from Google's GCM service. In the past it worked 100% of the time. The problem might coincide with our routers accepting IPv6 traffic, but the problem remains now even if we disable IPv6 on the adapter. It also doesn't work on a separate IPv4 only network. We also retry our requests using Google's recommended exponential back-off (<a href="http://developer.android.com/google/gcm/adv.html#retry" rel="noreferrer">http://developer.android.com/google/gcm/adv.html#retry</a>), so the problem could have been developing over time and we might have not noticed.</p>
  
  <p>All the production IPv4 and IPv6 addresses we've tested on are listed in our project at:
  <a href="https://console.developers.google.com" rel="noreferrer">https://console.developers.google.com</a></p>
  
  <p>Here is what our key looks like, we've got our entire publicly accessible subnet listed for testing, but I've tried with just single IP addresses as well.</p>
  
  <p><img src="https://i.stack.imgur.com/b4MMM.png" alt="enter image description here"></p>
  
  <p>Sometimes it works:</p>
  
  <pre><code>C:\Users\Administrator&gt;curl --header "Authorization: key=REDACTED" --header Content-Type:"application/json" https://android.googl
  eapis.com/gcm/send  -d "{\"registration_ids\":[\"test\"],\"data\":{\"code\":123}
  }" -k
  {"multicast_id":REDACTED,"success":0,"failure":1,"canonical_ids":0,"r
  esults":[{"error":"InvalidRegistration"}]}
  </code></pre>
  
  <p>But, sometimes we get this response:</p>
  
  <pre><code>C:\Users\Administrator&gt;curl --header "Authorization: key=REDACTED" --header Content-Type:"application/json" https://android.googl
  eapis.com/gcm/send  -d "{\"registration_ids\":[\"test\"],\"data\":{\"code\":123}
  }" -k
  &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><strong>Minor Update (4/10/2014)</strong></p>
  
  <p>Changing the settings to "Any IP allowed" fixes the problem, but changing to any other IP address or addresses breaks it again.</p>