我有一個帶有ASP.NET後端的Android應用程序。我有電話的registration_id以及正在執行推送的應用程序服務器的谷歌身份驗證令牌。Android C2DM獲取(401)未授權
當我發送到C2DM的http post請求,以便手機得到一條消息時,我總是得到401 Unauthorized。這裏是我如何在.NET中提出請求:
WebRequest myRequest = WebRequest.Create("https://android.apis.google.com/c2dm/send");
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.Method = "POST";
myRequest.Headers.Add("Authorization", "GoogleLogin auth=" + authId);
// buiold the post string
StringBuilder myPost = new StringBuilder();
myPost.AppendFormat("registration_id={0}", regId);
myPost.AppendFormat("&data.payload={0}", msg);
myPost.AppendFormat("&collapse_key={0}", colKey);
// write the post-string as a byte array
byte[] myData = ASCIIEncoding.ASCII.GetBytes(myPost.ToString());
myRequest.ContentLength = myData.Length;
Stream myStream = myRequest.GetRequestStream();
myStream.Write(myData, 0, myData.Length);
myStream.Close();
// Do the actual request and read the response stream
WebResponse myResponse = myRequest.GetResponse();
Stream myResponseStream = myResponse.GetResponseStream();
StreamReader myResponseReader = new StreamReader(myResponseStream);
string strResponse = myResponseReader.ReadToEnd();
myResponseReader.Close();
myResponseStream.Close();
任何幫助將不勝感激。
我猜我覺得也許我做的Authorization頭錯了,但我無法找到此服務的所有例子頭在線。 – 2010-09-20 16:53:55
或者我沒有正確編碼我的標題?我真的認爲這與授權標題有關。 – 2010-09-20 19:20:15
您可以分享一些關於如何使用.NET獲取OAuth令牌的代碼嗎? – katit 2012-06-02 01:58:46