2016-01-15 122 views
0

因此根據Tom Raz的帖子Ionic push notification api in c# with WebApi。我試圖實現的方法使用離子推送通知API這裏是我的代碼發送推送通知:c中的離子推送通知api#

public void sendToIonic(string regId, string msg) { 

     using (var client = new HttpClient()) 
     { 
      string data = "{ \"user_ids\":[\" "+ regId + "\"],\"notification\":{\"alert\":\" "+ msg + "\"}}"; 

      string json = Newtonsoft.Json.JsonConvert.SerializeObject(data); 

      client.BaseAddress = new Uri("https://push.ionic.io/api/v1/push"); 
      client.DefaultRequestHeaders.Accept.Clear(); 
      client.DefaultRequestHeaders.Add("X-Ionic-Application-Id", "1d74b1f2"); 
      var keyBase64 = "Basic %s" + "53a03dc7d9ce58511263e40580294f62af36b89be7cc4db2"; 
      client.DefaultRequestHeaders.Add("Authorization", keyBase64); 
      HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://push.ionic.io/api/v1/push"); 

      request.Content = new StringContent(data, Encoding.UTF8, "application/json"); 
      var response = client.SendAsync(request).Result; 
     } 

    } 

但我不斷收到響應error 403 Forbidden;。這是完整的響應:

{的StatusCode:403,ReasonPhrase: '禁止',版本:1.1,內容:System.Net.Http.StreamContent,頭: { 連接:保持活躍 訪問 - Control-Allow-Origin:* Access-Control-Allow-Headers:Origin,X-Requested-With,Content-Type,Accept Date:2016年1月15日星期五20:50:33 GMT ETag:W /「35 -nGlZv/eC8CyS + KJzQ1P9rg「 服務器:Cowboy Via:1.1 vegur X-Powered-By:Express Content-Length:53 Content-Type:application/json; charset = utf-8 }}

不知道什麼是錯的。我被卡住了。任何幫助,將不勝感激。

回答

0
var keyBase64 = "Basic %s" + "..." 

%S,這是在Python來連接。 你需要轉換爲Base64

您可以使用

client.DefaultRequestHeaders.Add("X-Ionic-Application-Id", "1d74b1f2"); 
var IONIC_PRIVATE_KEY_BASE_64 = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("53a03dc7d9ce58511263e40580294f62af36b89be7cc4db2:")); // ends with two points 
var keyBase64 = "Basic " + IONIC_PRIVATE_KEY_BASE_64 ; 
client.DefaultRequestHeaders.Add("Authorization", keyBase64); 

http://docs.ionic.io/docs/push-sending-push#section-authenticating-your-requests