使用 Python 的 Amazon SNS

发布于 2024-09-27 15:55:37 字数 2499 浏览 5 评论 0原文

尝试开始使用 Amazon SNS。 API非常简单REST。我似乎挂断了电话的签名部分。 API 示例是:

http://sns.us-east-1.amazonaws.com/
?Subject=My%20first%20message
&TopicArn=arn%3Aaws%3Asns%3Aus-east-1%3A698519295917%3AMy-Topic
&Message=Hello%20world%21
&Action=Publish
&SignatureVersion=2
&SignatureMethod=HmacSHA256
&Timestamp=2010-03-31T12%3A00%3A00.000Z
&AWSAccessKeyId=AKIAJHS4T6XPF7XIURNA
&Signature=9GZysQ4Jpnz%2BHklqM7VFTvEcjR2LIUtn6jW47054xxE%3D

我一直在关注签名 API 文档,所以我正在尝试:

from time import strftime,gmtime,time
import urllib2
import hmac
import hashlib
import base64
import string

def publichSNSMsg(Subject,TopicArn,Message,AWSAccessKeyId,privatekey):
  #http://docs.amazonwebservices.com/AWSSimpleQueueService/2008-01-01/SQSDeveloperGuide/
  amzsnshost = 'sns.us-east-1.amazonaws.com'
  values = {'Subject' : Subject,
            'TopicArn' : TopicArn,
            'Message' :Message,
            'Timestamp' : strftime("%Y-%m-%dT%H:%M:%S.000Z", gmtime(time())),
            'AWSAccessKeyId' : AWSAccessKeyId,
            'Action' : 'Publish',
            'SignatureVersion' : '2',
            'SignatureMethod' : 'HmacSHA256',
            }

  amazquote=lambda v: urllib2.quote(v).replace('%7E','~')
  cannqs=string.join(["%s=%s"%(amazquote(key),amazquote(values[key])) for key in sorted(values.keys(),key=str.lower)],'&')
  string_to_sign=string.join(["GET",amzsnshost,"/",cannqs],'\n')

  sig=base64.encodestring(hmac.new(privatekey,string_to_sign,hashlib.sha1).digest())
  querystring = "%s&Signature=%s"%(cannqs,amazquote(sig))
  url="http://%s/?%s"%(amzsnshost,querystring)

  try:
    return urllib2.urlopen(url).read()
  except urllib2.HTTPError, exception:
    return "Error %s (%s):\n%s"%(exception.code,exception.msg,exception.read())

然后回复:

<ErrorResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/"> 
  <Error> 
    <Type>Sender</Type> 
    <Code>SignatureDoesNotMatch</Code> 
    <Message>The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.</Message> 
  </Error> 
  <RequestId>8d6e5a41-dafb-11df-ac33-f981dc4e6c50</RequestId> 
</ErrorResponse> 

有什么想法吗?

Trying to get started with Amazon SNS. API is very simple REST. I seem to be hung up on the signature part of the call. The API example is:

http://sns.us-east-1.amazonaws.com/
?Subject=My%20first%20message
&TopicArn=arn%3Aaws%3Asns%3Aus-east-1%3A698519295917%3AMy-Topic
&Message=Hello%20world%21
&Action=Publish
&SignatureVersion=2
&SignatureMethod=HmacSHA256
&Timestamp=2010-03-31T12%3A00%3A00.000Z
&AWSAccessKeyId=AKIAJHS4T6XPF7XIURNA
&Signature=9GZysQ4Jpnz%2BHklqM7VFTvEcjR2LIUtn6jW47054xxE%3D

I've been following the API docs for signatures, so I'm trying:

from time import strftime,gmtime,time
import urllib2
import hmac
import hashlib
import base64
import string

def publichSNSMsg(Subject,TopicArn,Message,AWSAccessKeyId,privatekey):
  #http://docs.amazonwebservices.com/AWSSimpleQueueService/2008-01-01/SQSDeveloperGuide/
  amzsnshost = 'sns.us-east-1.amazonaws.com'
  values = {'Subject' : Subject,
            'TopicArn' : TopicArn,
            'Message' :Message,
            'Timestamp' : strftime("%Y-%m-%dT%H:%M:%S.000Z", gmtime(time())),
            'AWSAccessKeyId' : AWSAccessKeyId,
            'Action' : 'Publish',
            'SignatureVersion' : '2',
            'SignatureMethod' : 'HmacSHA256',
            }

  amazquote=lambda v: urllib2.quote(v).replace('%7E','~')
  cannqs=string.join(["%s=%s"%(amazquote(key),amazquote(values[key])) for key in sorted(values.keys(),key=str.lower)],'&')
  string_to_sign=string.join(["GET",amzsnshost,"/",cannqs],'\n')

  sig=base64.encodestring(hmac.new(privatekey,string_to_sign,hashlib.sha1).digest())
  querystring = "%s&Signature=%s"%(cannqs,amazquote(sig))
  url="http://%s/?%s"%(amzsnshost,querystring)

  try:
    return urllib2.urlopen(url).read()
  except urllib2.HTTPError, exception:
    return "Error %s (%s):\n%s"%(exception.code,exception.msg,exception.read())

And getting back:

<ErrorResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/"> 
  <Error> 
    <Type>Sender</Type> 
    <Code>SignatureDoesNotMatch</Code> 
    <Message>The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.</Message> 
  </Error> 
  <RequestId>8d6e5a41-dafb-11df-ac33-f981dc4e6c50</RequestId> 
</ErrorResponse> 

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月竹挽风 2024-10-04 15:55:37

噢,很简单!

查询字符串中的键应该按字节顺序排序,而不是不区分大小写(适用于版本 1 签名)。

gist 上提供了稍微更新(现在是正确的)代码。

Aw, it was simple!

Keys in the query string were supposed to be byte-order sorted, not case-insensitive sorted (that was for version 1 signatures).

Slightly updated (and now correct) code is available on gist.

瘫痪情歌 2024-10-04 15:55:37

我发现这个示例非常有用,因此我用 C# 重写了它。由于AWS没有用于SNS的WP7库,也许这会对某人有所帮助:https://gist.github .com/2705156

I found this example so useful that I rewrote it in C#. Since AWS does not have a WP7 library for SNS, perhaps this will be of help to someone: https://gist.github.com/2705156

烧了回忆取暖 2024-10-04 15:55:37

我建议您使用 boto3 进行 SNS。该文档可以在 http://boto3.readthedocs.io/ 找到en/latest/reference/services/sns.html。以下代码片段可用于获取 JSON 格式的签名。

import boto3
from django.views.decorators.csrf import csrf_exempt
topic_arn = <your topic>
client = boto3.client('sns',region_name="us-west-2")

#Call this to publish
def sns_publish(arg1,arg2):
    response = client.publish(
                        TopicArn=topic_arn,
                        Message=arg2,
                        Subject=arg2
                    )

#Function to subscribe to topic
@csrf_exempt
def sns_parse(request):
    print request.body.decode('utf-8')
    if request.method == "POST":
        response = json.loads(request.body.decode('utf-8'))
        type = response.get('Type')
        if type == 'Subscription':
            print(type)
            return("Subscription URL: "+<Url obtained>)
        elif type == 'Notification':
            return HttpResponse(type.get('Signature'))
    else:
        return HttpResponse('Not a POST object')

虽然这只是一个模板代码,但是,boto3 确实很有帮助。

I would suggest you to use boto3 for SNS. The documentation can be found at http://boto3.readthedocs.io/en/latest/reference/services/sns.html. The following code snippet can be used for fetching signature as JSON.

import boto3
from django.views.decorators.csrf import csrf_exempt
topic_arn = <your topic>
client = boto3.client('sns',region_name="us-west-2")

#Call this to publish
def sns_publish(arg1,arg2):
    response = client.publish(
                        TopicArn=topic_arn,
                        Message=arg2,
                        Subject=arg2
                    )

#Function to subscribe to topic
@csrf_exempt
def sns_parse(request):
    print request.body.decode('utf-8')
    if request.method == "POST":
        response = json.loads(request.body.decode('utf-8'))
        type = response.get('Type')
        if type == 'Subscription':
            print(type)
            return("Subscription URL: "+<Url obtained>)
        elif type == 'Notification':
            return HttpResponse(type.get('Signature'))
    else:
        return HttpResponse('Not a POST object')

This is just a template code though, but yeah, boto3 is indeed helpful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文