【发布时间】:2013-12-05 17:59:10
【问题描述】:
我已经在一千个地方看到过这个问题,我已经尝试了所有对其他人有用的解决方案,但我无法让它为我工作。我使用这篇文章作为我正在使用的项目的基线 (Unauthorized when calling Google GCM)。我已经尝试了 API 密钥和浏览器密钥,但均未成功。
这是我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.IO;
namespace GCMTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string BrowserAPIKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
string message = "some test message";
string tickerText = "example test GCM";
string contentTitle = "content title GCM";
string postData = "{ \"registration_ids\": [ \"" + txtRegistrationID.Text + "\" ], \"data\": {\"tickerText\":\"" + tickerText + "\", \"contentTitle\":\"" + contentTitle + "\", \"message\": \"" + message + "\"}}";
string response = SendGCMNotification(BrowserAPIKey, postData);
}
private string SendGCMNotification(string apiKey, string postData, string postDataContentType = "application/json")
{
// from here:
// https://stackoverflow.com/questions/11431261/unauthorized-when-calling-google-gcm
//
// original:
// http://www.codeproject.com/Articles/339162/Android-push-notification-implementation-using-ASP
ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateServerCertificate);
//
// MESSAGE CONTENT
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
//
// CREATE REQUEST
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send");
Request.Method = "POST";
Request.KeepAlive = false;
Request.ContentType = postDataContentType;
//Request.Headers.Add(string.Format("Authorization: key={0}", apiKey));
Request.Headers.Add(HttpRequestHeader.Authorization, String.Format("key={0}", apiKey));
Request.ContentLength = byteArray.Length;
//Stream dataStream;
try
{
Stream dataStream = Request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
//
// SEND MESSAGE
try
{
WebResponse Response = Request.GetResponse();
HttpStatusCode ResponseCode = ((HttpWebResponse)Response).StatusCode;
if (ResponseCode.Equals(HttpStatusCode.Unauthorized) || ResponseCode.Equals(HttpStatusCode.Forbidden))
{
MessageBox.Show("Unauthorized - need new token");
}
else if (!ResponseCode.Equals(HttpStatusCode.OK))
{
MessageBox.Show("Response from web service isn't OK");
}
StreamReader Reader = new StreamReader(Response.GetResponseStream());
string responseLine = Reader.ReadToEnd();
Reader.Close();
return responseLine;
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
return "error";
}
public static bool ValidateServerCertificate(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
return true;
}
}
}
这一行是错误的:
WebResponse Response = Request.GetResponse();
带有以下消息: “远程服务器返回错误:(401) Unauthorized。”
该表单允许输入设备注册 ID。如果我能得到这个工作,它将被改变为在生产环境中更有用。该应用程序现在正在我的本地计算机上运行,但最终将驻留在远程服务器上。
如果我能得到任何帮助,我们将不胜感激。我已经阅读有关此的帖子一周了,但我无法取得任何进展。我认为可能导致此问题的一件事是我在本地而不是在服务器上运行它。此请求是否需要来自浏览器键中“referer”中列出的地址?
再次感谢!
【问题讨论】:
-
您是否尝试过删除“referrer”,以便允许任何referrer?
-
哇......我不敢相信......我已经搞砸了好几天,你在 5 秒内搞定......这很有效!谢谢!!!
-
很高兴能帮上忙。我会将此作为答案发布。
标签: android push-notification google-cloud-messaging unauthorized