请帮助我,我有一个问题是从ASP.NET webservice(VB.NET)推送android的通知。 这是我的代码。
Public sApplicationID As String = "YYY....ZZZ"
Public sSENDER_ID As String = "222...555"
<WebMethod()> _
Public Function SendMessage(ByVal RegistrationID As String, ByVal Message As String) As String
Dim regid As String = RegistrationID
Dim tRequest As WebRequest
tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send")
tRequest.Method = "post"
'tRequest.ContentType = " application/json"
tRequest.ContentType = " application/x-www-form-urlencoded"
tRequest.Headers.Add(String.Format("Authorization: key={0}", sApplicationID))
tRequest.Headers.Add(String.Format("Sender: id={0}", sSENDER_ID))
Dim postData As String = "{""collapse_key"":""score_update"",""time_to_live"":108,""delay_while_idle"":1,""data"":{""message"":""" & Convert.ToString(Message) & """,""time"":""" & System.DateTime.Now.ToString() & """},""registration_ids"":" & regid & "}"
Console.WriteLine(postData)
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()
tReader.Close()
dataStream.Close()
tResponse.Close()
Return sResponseFromServer
End Function
我正在测试此服务,但我总是收到错误消息
System.Net.WebException: The remote server returned an error: (401) Unauthorized.
at System.Net.HttpWebRequest.GetResponse()
答案 0 :(得分:0)
似乎某些身份验证不起作用。根据{{3}},您的API密钥无效:
如果您收到401 HTTP状态代码,则您的API密钥无效
有关使用GCM时401的可能原因,请参阅此答案:guide。它指的是一个不再存在的链接,但看起来非常全面。