2013-02-26 76 views
6

我想弄清楚如何使用AWS .NET SDK来确认订阅SNS主题。使用AWS .NET SDK的SNS订阅确认示例

订阅是通过HTTP

的终点是在.NET MVC的网站。

我无法在任何地方找到任何.net示例?

一个可行的例子会很棒。

我想是这样的

Dim snsclient As New Amazon.SimpleNotificationService.AmazonSimpleNotificationServiceClient(ConfigurationSettings.AppSettings("AWSAccessKey"), ConfigurationSettings.AppSettings("AWSSecretKey")) 

    Dim TopicArn As String = "arn:aws:sns:us-east-1:991924819628:post-delivery" 


    If Request.Headers("x-amz-sns-message-type") = "SubscriptionConfirmation" Then 

     Request.InputStream.Seek(0, 0) 
     Dim reader As New System.IO.StreamReader(Request.InputStream) 
     Dim inputString As String = reader.ReadToEnd() 

     Dim jsSerializer As New System.Web.Script.Serialization.JavaScriptSerializer 
     Dim message As Dictionary(Of String, String) = jsSerializer.Deserialize(Of Dictionary(Of String, String))(inputString) 

     snsclient.ConfirmSubscription(New Amazon.SimpleNotificationService.Model.ConfirmSubscriptionRequest With {.AuthenticateOnUnsubscribe = False, .Token = message("Token"), .TopicArn = TopicArn}) 


    End If 

回答

0

我最终得到它的工作使用中的代码。我在开发服务器上捕获异常时遇到了问题,结果告诉我服务器的时间与SNS消息中的时间戳不匹配。

一旦服务器的时间被解决了(亚马逊服务器BTW),确认工作。

0

下面的例子帮助我与SNS合作。它会通过所有步骤来处理主题。在这种情况下,订阅请求是一个电子邮件地址,但可以更改为HTTP。

Pavel's SNS Example
Documentation

+0

谢谢,但这个例子不包括确认通过http订阅这是我遇到的麻烦的具体的事情。 – 2013-02-27 00:31:48

9

这是一个使用MVC WebApi 2和最新的AWS .NET SDK的工作示例。

var jsonData = Request.Content.ReadAsStringAsync().Result; 
var snsMessage = Amazon.SimpleNotificationService.Util.Message.ParseMessage(jsonData); 

//verify the signaure using AWS method 
if(!snsMessage.IsMessageSignatureValid()) 
    throw new Exception("Invalid signature"); 

if(snsMessage.Type == Amazon.SimpleNotificationService.Util.Message.MESSAGE_TYPE_SUBSCRIPTION_CONFIRMATION) 
{ 
    var subscribeUrl = snsMessage.SubscribeURL; 
    var webClient = new WebClient(); 
    webClient.DownloadString(subscribeUrl); 
    return "Successfully subscribed to: " + subscribeUrl; 
} 
1

@克雷格的回答以上(这大大帮助了我),下面是一个ASP.NET MVC的WebAPI控制器消耗和自动订阅SNS主题建筑上。 #WebHooksFTW

using RestSharp; 
using System; 
using System.Net; 
using System.Net.Http; 
using System.Reflection; 
using System.Web.Http; 
using System.Web.Http.Description; 

namespace sb.web.Controllers.api { 
    [System.Web.Mvc.HandleError] 
    [AllowAnonymous] 
    [ApiExplorerSettings(IgnoreApi = true)] 
    public class SnsController : ApiController { 
    private static string className = MethodBase.GetCurrentMethod().DeclaringType.Name; 

    [HttpPost] 
    public HttpResponseMessage Post(string id = "") { 
     try { 
     var jsonData = Request.Content.ReadAsStringAsync().Result; 
     var sm = Amazon.SimpleNotificationService.Util.Message.ParseMessage(jsonData); 
     //LogIt.D(jsonData); 
     //LogIt.D(sm); 

     if (!string.IsNullOrEmpty(sm.SubscribeURL)) { 
      var uri = new Uri(sm.SubscribeURL); 
      var baseUrl = uri.GetLeftPart(System.UriPartial.Authority); 
      var resource = sm.SubscribeURL.Replace(baseUrl, ""); 
      var response = new RestClient { 
      BaseUrl = new Uri(baseUrl), 
      }.Execute(new RestRequest { 
      Resource = resource, 
      Method = Method.GET, 
      RequestFormat = RestSharp.DataFormat.Xml 
      }); 
      if (response.StatusCode != System.Net.HttpStatusCode.OK) { 
      //LogIt.W(response.StatusCode); 
      } else { 
      //LogIt.I(response.Content); 
      } 
     } 

     //read for topic: sm.TopicArn 
     //read for data: dynamic json = JObject.Parse(sm.MessageText); 
     //extract value: var s3OrigUrlSnippet = json.input.key.Value as string; 

     //do stuff 
     return Request.CreateResponse(HttpStatusCode.OK, new { }); 
     } catch (Exception ex) { 
     //LogIt.E(ex); 
     return Request.CreateResponse(HttpStatusCode.InternalServerError, new { status = "unexpected error" }); 
     } 
    } 
    } 
} 
0

我不知道这是如何最近更改,但我发现,AWS SNS现在提供了一个非常简单的订阅,不涉及使用RESTSharp提取网址或建筑物请求的方法.. ...以下是简化的WebAPI POST方法:

[HttpPost] 
    public HttpResponseMessage Post(string id = "") 
    { 
     try 
     { 
      var jsonData = Request.Content.ReadAsStringAsync().Result; 
      var sm = Amazon.SimpleNotificationService.Util.Message.ParseMessage(jsonData); 

      if (sm.IsSubscriptionType) 
      { 
       sm.SubscribeToTopic(); // CONFIRM THE SUBSCRIPTION 
      } 
      if (sm.IsNotificationType) // PROCESS NOTIFICATIONS 
      { 
       //read for topic: sm.TopicArn 
       //read for data: dynamic json = JObject.Parse(sm.MessageText); 
       //extract value: var s3OrigUrlSnippet = json.input.key.Value as string; 
      } 

      //do stuff 
      return Request.CreateResponse(HttpStatusCode.OK, new { }); 
     } 
     catch (Exception ex) 
     { 
      //LogIt.E(ex); 
      return Request.CreateResponse(HttpStatusCode.InternalServerError, new { status = "unexpected error" }); 
     } 
    }