The remote server returned an error: (401) Unauthorized using google cloud messaging using dotnet

56 views
Skip to first unread message

Ronak

unread,
Sep 22, 2014, 7:34:26 AM9/22/14
to dotnetde...@googlegroups.com
Hii,

I want to use GCM for push message, followed all the steps from the article : https://developer.android.com/google/gcm/gs.html

below is my code:

Private Sub send(ByVal regId As String)

        Dim tRequest As WebRequest
        tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send")
        tRequest.Method = "post"
        tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8"
        tRequest.Headers.Add("Authorization: key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
        tRequest.Headers.Add("Sender: id=0000000000")

        Dim collaspeKey As String = Guid.NewGuid().ToString("n")
        Dim postData As String = "collapse_key=" & collaspeKey & "&data.payload=" & Convert.ToString(txtMsg.Text) & "&data.time=" & System.DateTime.Now.ToString() & "&registration_id=" & regId & ""
        Dim byteArray As [Byte]() = Encoding.UTF8.GetBytes(postData)
        tRequest.ContentLength = byteArray.Length

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

        Dim tResponse As WebResponse = tRequest.GetResponse()
        dataStream = tResponse.GetResponseStream()
        Dim tReader As New StreamReader(dataStream)
        Dim sResponseFromServer As [String] = tReader.ReadToEnd()
        lblStat.Text = sResponseFromServer
        tReader.Close()
        dataStream.Close()
        tResponse.Close()
    End Sub
***************************************************************************************************
on executing getresponse, google fires "The remote server returned an error: (401) Unauthorized" error.

Please help me, what is wrong.
I have given both key as per the article described.

Thank you
Reply all
Reply to author
Forward
0 new messages