加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – Android C2DM获取(401)未经授权

发布时间:2020-12-16 00:02:40 所属栏目:asp.Net 来源:网络整理
导读:我有一个带有ASP.NET后端的Android应用程序.我有手机的registration_id以及来自谷歌的执行推送的应用服务器的身份验证令牌. 当我向C2DM发出http post请求以便手机收到消息时,我一直收到401 Unauthorized.以下是我在.NET中发出请求的方式: WebRequest myRequ
我有一个带有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,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();

任何帮助将非常感激.

解决方法

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读