GCM服务器(远程服务器返回错误:(401)未经授权。)

时间:2014-01-13 13:54:44

标签: android asp.net .net wcf google-cloud-messaging

我一直在使用.net中的GCM Server,但我无法将数据发布到服务器并始终收到此错误。

远程服务器返回错误:(401)未经授权。

下面提到了asp.net代码。

        string regId = "xxxx1bER6XXjuEhxxxxxxxxxxxxxxxxxxxxxf_thTnR7CSxxxxxxqX60-jxxxN55xxxxK074AaV9M09jP2xxxxu28aXXrd6ltxxxxxxhvmVt-nxxfw";


          var applicationID = "AIzaxxxxxt6fJx5txxxxxx";



            var SENDER_ID = "x8xxxxxx030x";
            var value = Text1.Text;
            WebRequest tRequest;
            tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
            tRequest.UseDefaultCredentials = true;
            tRequest.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
            tRequest.Method = "post";
            tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
            tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));

            tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));

             //Data_Post Format
             // string postData = "{'collapse_key' : 'demo', 'registration_id': [ '" + regId + "' ], 
              //'data': {'message': '" + Label1.Text + "'},'time_to_live' : '3' }";


            string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message="
                + value + "&data.time=" + System.DateTime.Now.ToString() + "&registration_id=" + regId + "";


            Console.WriteLine(postData);
            Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            tRequest.ContentLength = byteArray.Length;

            Stream dataStream = tRequest.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();

            WebResponse tResponse = tRequest.GetResponse();

            dataStream = tResponse.GetResponseStream();

            StreamReader tReader = new StreamReader(dataStream);

            String sResponseFromServer = tReader.ReadToEnd();

            Label3.Text = sResponseFromServer; //printing response from GCM server.
            tReader.Close();
            dataStream.Close();
            tResponse.Close();

我输入了正确的凭据,但仍然存在错误。我现在已经坚持这个场景两天了,请帮助我解决.NET WCF中的GCM Server问题。感谢

1 个答案:

答案 0 :(得分:0)

打开firebase控制台,然后在firbase中导入Google项目,点击设置并下载" google-services.json"使用服务器密钥作为api密钥然后运行Android应用程序和genrate注册ID,然后发送通知它将是工作