c# – 在致电Google GCM时未经授权

栏目: ASP.NET · 发布时间: 5年前

内容简介:Daniel – Dude GCM文档存在问题!使用Browser密钥作为Server API密钥位置的授权密钥.它会工作.翻译自:https://stackoverflow.com/questions/11431261/unauthorized-when-calling-google-gcm

我尝试使用Google GCM发送推送通知.但得到一个WebException,说远程服务器返回401 unautorized.我无法理解为什么它不起作用.

有谁知道它为什么不起作用?

这是我的代码:

ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateServerCertificate);

           HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send");


            Request.Method = "POST";
            Request.KeepAlive = false;

            string postData = "{ 'registration_ids': [ '"+registrationId+"' ], 'data': {'message': '"+message+"'}}";

            byte[] byteArray = Encoding.UTF8.GetBytes(postData);

            Request.ContentType = "application/json";
            //Request.ContentLength = byteArray.Length;


            //Request.Headers.Add(HttpRequestHeader.Authorization, "GoogleLogin auth=" + AuthString);
            Request.Headers.Add(HttpRequestHeader.Authorization, "Authorization: key=AIzaSyCEygavdzrNM3pWNPtvaJXpvW66CKnjH_Y");
            //-- Delegate Modeling to Validate Server Certificate --//


            //-- Create Stream to Write Byte Array --// 
            Stream dataStream = Request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();

            //-- Post a Message --//
            WebResponse Response = Request.GetResponse();
            HttpStatusCode ResponseCode = ((HttpWebResponse)Response).StatusCode;
            if (ResponseCode.Equals(HttpStatusCode.Unauthorized) || ResponseCode.Equals(HttpStatusCode.Forbidden))
            {
                var text = "Unauthorized - need new token";

            }
            else if (!ResponseCode.Equals(HttpStatusCode.OK))
            {
                var text =  "Response from web service isn't OK";
            }

            StreamReader Reader = new StreamReader(Response.GetResponseStream());
            string responseLine = Reader.ReadLine();
            Reader.Close();

Daniel – Dude GCM文档存在问题!使用Browser密钥作为Server API密钥位置的授权密钥.它会工作.

翻译自:https://stackoverflow.com/questions/11431261/unauthorized-when-calling-google-gcm


以上所述就是小编给大家介绍的《c# – 在致电Google GCM时未经授权》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Web Security, Privacy and Commerce, 2nd Edition

Web Security, Privacy and Commerce, 2nd Edition

Simson Garfinkel / O'Reilly Media / 2002-01-15 / USD 44.95

Since the first edition of this classic reference was published, World Wide Web use has exploded and e-commerce has become a daily part of business and personal life. As Web use has grown, so have ......一起来看看 《Web Security, Privacy and Commerce, 2nd Edition》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具