Quantcast
Channel: Visual Studio and Visual Web Developer Express
Viewing all articles
Browse latest Browse all 3509

error System.Net.WebException: The remote server returned an error: (401) Unauthorized. - gcm

$
0
0

hi

I posted notifications from project(asp mvc) with gcm.

I receive the following error when sending notifications.

error System.Net.WebException: The remote server returned an error: (401) Unauthorized.

please guide me.

public Notification PushToAndroidDevice(string registrationid, string message)
        {
            Notification notification = new Notification();
            //try
            //{
                //var applicationID = "Axxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxs";//Server Key
                var applicationID = "Axxxxxxxxxxxxxxxxxxxxxxxxxxxxs";//Browser Key
                var SENDER_ID = "2xxxxxxxxxxxxxxxxxxxx5";

                WebRequest tRequest;
                tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
                tRequest.Method = "post";

                tRequest.ContentType = "application/x-www-form-urlencoded";
                //tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
            tRequest.Headers.Add(HttpRequestHeader.Authorization, "key=AIxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxs");

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

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

                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();

                notification.Message = sResponseFromServer;
                tReader.Close();
                dataStream.Close();
                tResponse.Close();

                notification.Status = true;
            //}
            //catch (Exception ex)
            //{
            //    notification.Status = false;
            //    notification.Message = "ERROR DESCRIPTION : " + ex.Message;
            //}
            return notification;
        }


Viewing all articles
Browse latest Browse all 3509

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>