Android C2DM获取(401)未经授权

时间:2010-09-20 04:09:26

标签: asp.net android oauth android-c2dm

我有一个带有ASP.NET后端的Android应用程序。我有手机的registration_id以及来自谷歌的执行推送的应用服务器的身份验证令牌。

当我向C2DM发出http post请求以便手机收到消息时,我一直收到401 Unauthorized。以下是我在.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();

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:4)

建议角:每隔一段时间对您的代码有一定的信心!而且,即使谷歌有时会搞砸。

在花了大约9个小时阅读每篇博文和关于Google OAuth和C2DM的文章并在我的代码中尝试不同的内容后,我通过电子邮件发送了Google。我很高兴地说,我不仅很快就收到了回复,而且还说我的帐户搞砸了。我在他们的网站上注册时出了点问题,虽然看起来一切都是我收到的注册成功电子邮件的工作,但事实并非如此。我重新注册,一切正常!

答案 1 :(得分:2)

我遇到了类似的问题:尝试使用google c2dm(云端设备消息传递)代码示例时出现错误401“未经授权”。它看起来像以前一样工作的例子,但现在谷歌改变了它的条件。在运行代码示例之前,您必须注册:

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

我注册了,这些东西在几分钟内就开始工作了。

答案 2 :(得分:0)

我遇到了同样的问题,即我的C2DM_ACCOUNT_EMAIL突然停止工作。

要解决此问题,只需使用相同的信息和相同的C2DM_ACCOUNT_EMAIL重新注册。

HTH