Android C2DM 获取 (401) 未授权 [英] Android C2DM getting (401) Unauthorized
问题描述
我有一个带有 ASP.NET 后端的 Android 应用程序.我有手机的 registration_id 以及来自 google 的用于执行推送的应用程序服务器的身份验证令牌.
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.
当我向 C2DM 发出 http post 请求以便手机收到一条消息时,我不断收到 401 Unauthorized.这是我在 .NET 中发出请求的方式:
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();
任何帮助将不胜感激.
推荐答案
建议角:时不时地对您的代码充满信心!而且,即使是 Google 有时也会搞砸.
Advice corner: Have some confidence in your code every once and a while! And, even Google messes up sometimes.
在花了大约 9 个小时阅读了关于 Google OAuth 和 C2DM 的每篇博文和文章并在我的代码中尝试了不同的东西之后,我给 Google 发了电子邮件.我很高兴地说,我不仅很快就收到了回复,而且我的帐户也搞砸了.我在他们的网站上注册时出了点问题,虽然我收到的注册成功电子邮件似乎一切正常,但事实并非如此.我重新注册,一切正常!
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!
这篇关于Android C2DM 获取 (401) 未授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!