4

如何通过 http 确认订阅 Amazon SNS?我目前正在为我的应用程序使用 Rails,并希望收到有关我的电子邮件退回的通知。因此,我尝试浏览亚马逊论坛,发现最接近的是:https ://forums.aws.amazon.com/thread.jspa?threadID=87555它告诉我使用 request.raw_post 获取请求和 JSON 解析它。但是,当我尝试这样做时,它给了我一个错误,说请求是空的。我做对了吗?有人可以指导我如何从 Amazon SNS 获取订阅令牌,以便我确认订阅吗?谢谢!:)

4

3 回答 3

5

这对我有用。在您的控制器中,创建类似于以下代码的操作。你想要令牌路线,所以你可以在那里看到它。但是我选择了 SubscribeURL 路由,并且只是在 URL 上执行 GET。

  def sns
     request_json = JSON.parse(request.raw_post, {symbolize_names: true})
     subscribe_url = request_json[:SubscribeURL]
     token = request_json[:Token]
  end
于 2015-04-07T02:45:36.203 回答
0

这是一个适合我的php脚本。(有一些日志行将其写入文件 i /tmp/log.txt 有助于调试正在发生的事情。首先,我从https://sns.us-east-1.amazonaws下载了亚马逊证书.com/SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem确保也可以在任何地方使用 utf8。

对我来说,它只适用于 http。当我订阅 https url 时,我在服务器中看不到任何访问权限。

<?php

mLog ("\n\n----------------Start Receive -------------\n");

require_once APP_DIR . 'util/AWSSDKforPHP/sdk.class.php';

$h= fopen("php://input","r");
$X = stream_get_contents($h);


$J = json_decode( $X , true);

mLog("Received body: \n" . json_encode($J). "\n");

function mLog($Msg) {
    file_put_contents(sys_get_temp_dir() .'/log.txt',$Msg,FILE_APPEND);
}


function CheckCall($J) {

    if ($J["Type"]=="Notification") {
        $SB='';
        $SB .= "Message\n";
        $SB .= $J["Message"] . "\n";
        $SB .= "MessageId\n";
        $SB .= $J["MessageId"] . "\n";

        if ($J["Subject"] != null) {
            $SB .= "Subject\n";
            $SB .= $J["Subject"]. "\n";
        }

        $SB .= "Timestamp\n";
        $SB .= $J["Timestamp"] . "\n";

        $SB .="TopicArn\n";
        $SB .= $J["TopicArn"]."\n";
        $SB .="Type\n";
        $SB .=$J["Type"]."\n";

    } else if ($J["Type"]=="SubscriptionConfirmation") {
        $SB='';
        $SB .= "Message\n";
        $SB .= $J["Message"] . "\n";
        $SB .= "MessageId\n";
        $SB .= $J["MessageId"] . "\n";
        $SB .= "SubscribeURL\n";
        $SB .= $J["SubscribeURL"] . "\n";
        $SB .= "Timestamp\n";
        $SB .= $J["Timestamp"] . "\n";

        $SB .= "Token\n";
        $SB .= $J["Token"] ."\n";
        $SB .="TopicArn\n";
        $SB .= $J["TopicArn"]."\n";
        $SB .="Type\n";
        $SB .=$J["Type"]."\n";
    } else {
        $SB='';
    }


    $cert = file_get_contents('SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem');
    $pubkey_id = openssl_get_publickey($cert);

    mLog("Signed string:\n" . $SB . "End Signed string\n");

    $Sig=base64_decode($J["Signature"]);
    if ( openssl_verify($SB, $Sig, $pubkey_id)) {
        return true;
    } else {

        return false;
    }
}

if (!CheckCall($J)) {
    mLog("Authorization failed. \n");
    header('HTTP/1.1 500 Internal Server Error');
    echo "<html><head></head><body>".sys_get_temp_dir()."</body></html>";

    exit(0);
}

$sns = new AmazonSNS();

if ($J["Type"]=="SubscriptionConfirmation") {
    $Res = $sns->confirm_subscription($J["TopicArn"], $J["Token"]);
    mLog("confitm_subscription sended: \n" . json_encode($Res));
} else if ($J["Type"]=="Notification") {
    mLog("-----------Begin Message-----------\n");
    mLog("Subject: ".$J["Subject"]. "\n");
    mLog($J["Message"]);
    mLog("\n-----------End Messagr------------\n\n");

} else {
    mLog("Invalid type \n");
}
echo "<html><head></head><body>Authentication Error</body></html>";
于 2013-02-01T11:44:47.117 回答
-1

使用 PHP:

你可以进去file_get_contents('php://input');

你会收到这样的 json 结构:

{
  "Type" : "SubscriptionConfirmation",
  "MessageId" : "4..050670318",
  "Token" : "2336412f37fb687f5d51e6e2425f00....",
  "TopicArn" : "arn:aws:sns......",
  "Message" : "You have chosen to subscribe to the topic arn:aws:sns:us-east-1:430......\nTo confirm the subscription, visit the SubscribeURL included in this message.",
  "SubscribeURL" : "https://sns.us-east-1.amazonaws.com/?Action=ConfirmSubscription&TopicArn=arn:aws:sns:us-....&Token=2336412f37fb687f5d51e6e2425f004aef17b3ebbd98e122....",
  "Timestamp" : "2020-06-30T14:31:43.953Z",
  "SignatureVersion" : "1",
  "Signature" : "i3zpoQ3mAbcxTr18m/z5PL9Ls...",
  "SigningCertURL" : "https://sns.us-east-1.amazonaws.com/..."
}
于 2020-06-30T15:42:31.087 回答