开发者

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. H开发者_开发百科ere 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.


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!


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜