Android C2DM getting (401) Unauthorized

I have an Android application with an ASP.NET backend. I have the registration_id for the phone as well as an auth token from google for the application server that is performing a push.

When I make the http post request to C2DM so that the phone gets a message I keep getting the 401 Unauthorized. Here is how I’m making the request in .NET:

    WebRequest myRequest = WebRequest.Create("https://android.apis.google.com/c2dm/send");
    myRequest.ContentType = "application/x-www-form-urlencoded";
    myRequest.Method = "POST";
    myRequest.Headers.Add("Authorization", "GoogleLogin auth=" + authId);
    // buiold the post string
    StringBuilder myPost = new StringBuilder();
    myPost.AppendFormat("registration_id={0}", regId);
    myPost.AppendFormat("&data.payload={0}", msg);
    myPost.AppendFormat("&collapse_key={0}", colKey);

    // write the post-string as a byte array
    byte[] myData = ASCIIEncoding.ASCII.GetBytes(myPost.ToString());
    myRequest.ContentLength = myData.Length;
    Stream myStream = myRequest.GetRequestStream();
    myStream.Write(myData, 0, myData.Length);
    myStream.Close();
    // Do the actual request and read the response stream
    WebResponse myResponse = myRequest.GetResponse();
    Stream myResponseStream = myResponse.GetResponseStream();
    StreamReader myResponseReader = new StreamReader(myResponseStream);
    string strResponse = myResponseReader.ReadToEnd();
    myResponseReader.Close();
    myResponseStream.Close();

Any help would be much appreciated.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Advice corner: Have some confidence in your code every once and a while! And, even Google messes up sometimes.

After spending about nine hours reading every blog post and article about Google OAuth and C2DM and trying different things in my code, I emailed Google. I’m excited to say that not only did I receive a response very quickly, but also that my account was messed up. Something went wrong while I was registering on their site and although it appeared that everything was working from the registration success email I received, it wasn’t. I re-registered and everything works!

Method 2

I have encountered a similar problem: error 401 “Unauthorized” while trying a google c2dm (cloud to device messaging) code sample. It looks like the example used to work as is, but now Google has changed its conditions. Before running code examples, you have to sign-up:

http://code.google.com/android/c2dm/signup.html

I signed up, and the stuff began to work in minutes.

Method 3

I’ve got the same problem, i.e. suddenly my C2DM_ACCOUNT_EMAIL stopped working.

To solve the problem, just fill the registration again with same info and the same C2DM_ACCOUNT_EMAIL.

HTH


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x