Boto服务器错误:端点已存在,具有相同的令牌,但属性不同

时间:2015-08-29 19:56:38

标签: amazon-web-services boto amazon-sns

我的应用程序返回以下错误:

  

BotoServerError:BotoServerError:400 Bad Request   {“Error”:{“Code”:“InvalidParameter”,“Message”:“无效参数:令牌   原因:   端点   [myendpoint]已经存在相同的令牌,但是   不同   属性 “” 类型 “:” 发件人 “},” 的requestId “:” myrequestid“}

我在http://mobile.awsblog.com/post/Tx223MJB0XKV9RU/Mobile-token-management-with-Amazon-SNS上对我的代码进行了建模,以避免出现这种问题,但我无法解决导致此问题的原因或解决方法。我是否从SNS控制台删除端点,或者将我的数据库中的endpointarn设置为NULL或两者都没关系。请帮忙!

def createEndpoint(sns_conn, applicationArn, testUser, token):
    cpe_result = sns_conn.create_platform_endpoint(applicationArn, token, str(testUser.userid))
    try:
            endpointArn = cpe_result["CreatePlatformEndpointResponse"]["CreatePlatformEndpointResult"]["EndpointArn"]
    except BotoServerError, e:
            if "already exists with the same Token, but different attributes" in e.error_message:
                    s = e.error_message
                    endpointArn = s[s.index("Endpoint ") + len("Endpoint "):s.index("already exists") - 1]
            else:
                    raise
    testUser.endpointarn = endpointArn
    db.session.commit()
    return endpointArn

def registerWithSNS(testUser):
    # Adaptation of code from http://mobile.awsblog.com/post/Tx223MJB0XKV9RU/Mobile-token-management-with-Amazon-SNS
    endpointArn = testUser.endpointarn
    token = request.form["token"]
    platform = request.form["platform"]
    updateNeeded = False
    createNeeded = endpointArn == None

    # Init sns_conn and applicationArn
    sns_conn = sns.connect_to_region("eu-west-1")
    lpa_response = sns_conn.list_platform_applications()
    platformApps = lpa_response["ListPlatformApplicationsResponse"]["ListPlatformApplicationsResult"]["PlatformApplications"]
    if platform == "Android":
            requiredSuffix = "GCM"
    elif platform == "iOS":
            requiredSuffix = "APNS"        
    else:
            raise Exception("Unknown platform: '{}'".format(platform))
    applicationArn = None
    for pa in platformApps:
            if pa["PlatformApplicationArn"].endswith(requiredSuffix):
                    applicationArn = pa["PlatformApplicationArn"]
                    break
    if applicationArn == None:
            raise Exception("Missing SNS platform application for '{}'".format(platform))

    if createNeeded:
            # No endpoint ARN is stored; need to call createEndpoint
            endpointArn = createEndpoint(sns_conn, applicationArn, testUser, token)
            createNeeded = False

    # Look up the endpoint and make sure the data in it is current, even if
    # it was just created
    try:
            gea_result = sns_conn.get_endpoint_attributes(endpointArn)
            returnedToken = gea_result["GetEndpointAttributesResponse"]["GetEndpointAttributesResult"]["Attributes"]["Token"]
            returnedEnabled = gea_result["GetEndpointAttributesResponse"]["GetEndpointAttributesResult"]["Attributes"]["Enabled"]
            updateNeeded = (returnedToken != token) or (returnedEnabled != "true")
    except BotoServerError, e:
            if e.error_code == "NotFound":
                    # we had a stored ARN, but the endpoint associated with it
                    # disappeared. Recreate it.
                    createNeeded = True
            else:
                    raise

    if createNeeded:
            createEndpoint(sns_conn, applicationArn, testUser, token)

    if updateNeeded:
            # endpoint is out of sync with the current data;
            # update the token and enable it.
            attribs = {}
            attribs["Token"] = token
            attribs["Enabled"] = "true"
            sns_conn.set_endpoint_attributes(endpointArn, attribs)

1 个答案:

答案 0 :(得分:0)

捂脸。 createEndpoint中的try只需要高一行......