4

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

当我向 C2DM 发出 http 发布请求以便手机收到一条消息时,我不断收到 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();

任何帮助将非常感激。

4

3 回答 3

4

建议角:时不时地对你的代码有信心!而且,即使是谷歌有时也会搞砸。

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

于 2010-09-21T21:13:33.953 回答
2

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

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

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

于 2011-05-16T14:31:12.760 回答
0

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

要解决此问题,只需使用相同的信息和相同的 C2DM_ACCOUNT_EMAIL 再次填写注册。

高温高压

于 2011-06-05T09:47:47.137 回答