0

我正在尝试使用 C# 通过 UrbanAirship 向我的 ipod 发送测试通知。我尝试了许多代码片段,试图从我的 Windows 服务器向 UrbanAirship 发送通知,但我总是以 401 错误告终。

这是我的最新代码:

string postData = "{\"aps\": {\"badge\": 1, \"alert\": \"Hello from Urban Airship!\"}, \"device_tokens\": [\""+ devToken +"\"]}";

var uri = new Uri("https://go.urbanairship.com/api/push/");
var encoding = new UTF8Encoding();
var request = (HttpWebRequest)WebRequest.Create(uri);
char[] charArray = Encoding.UTF8.GetChars(Encoding.UTF8.GetBytes(postData));
request.Method = "POST";
request.Credentials = new NetworkCredential(username, password);
request.ContentType = "application/json";
request.ContentLength = encoding.GetByteCount(charArray);

using (var stream = request.GetRequestStream())
{
   stream.Write(encoding.GetBytes(postData), 0, encoding.GetByteCount(postData));
   stream.Close();
   var response = request.GetResponse();
   response.Close();
}

注意:我已经验证我的用户名和密码是正确的。

===更新===

我会说我的城市飞艇配置有问题,但以下工作:

curl -X PUT -u "appKey:appSecret" -H "Content-Type: application/json" --data '{"alias": "myalias"}' https://go.urbanairship.com/api/device_tokens/myTOken/ 
4

1 回答 1

0

已修复,我修改了我的代码,最重要的是:您的密码是应用程序的主要机密

string postData = "{\"aps\": {\"badge\": 1, \"alert\": \"Hello from Urban Airship!\"}, \"device_tokens\": [\""+ devToken +"\"]}";

var uri = new Uri("https://go.urbanairship.com/api/push/");
var encoding = new UTF8Encoding();
var request = (HttpWebRequest)WebRequest.Create(uri);
char[] charArray = Encoding.UTF8.GetChars(Encoding.UTF8.GetBytes(postData));
request.Method = "POST";
request.Credentials = new NetworkCredential(username, password);
request.ContentType = "application/json";
request.ContentLength = encoding.GetByteCount(charArray);

using (var stream = request.GetRequestStream())
{
    stream.Write(encoding.GetBytes(postData), 0, encoding.GetByteCount(postData));
    stream.Close();
    var response = request.GetResponse();
    response.Close();
}
于 2012-10-30T21:25:41.520 回答