2016-02-18 186 views
2

我在尝试使用此代码认购在iOS终端的SNS话题:AWS无法订阅SNS主题:CognitoIdentityCredentials无权执行:SNS:订阅

let sns = AWSSNS.defaultSNS() 
    let request = AWSSNSCreatePlatformEndpointInput() 
    request.token = deviceTokenString 
    request.platformApplicationArn = SNSPlatformApplicationArn 

    sns.createPlatformEndpoint(request).continueWithExecutor(AWSExecutor.mainThreadExecutor(), withBlock: { (task: AWSTask!) -> AnyObject! in 
     if task.error != nil { 
      print(task.error) 
     } else { 
      let createEndpointResponse = task.result as! AWSSNSCreateEndpointResponse 

      // Subscribe to the topic 
      let subscribeTopicInput = AWSSNSSubscribeInput() 
      subscribeTopicInput.endpoint = createEndpointResponse.endpointArn 
      subscribeTopicInput.protocols = "application" 
      subscribeTopicInput.topicArn = MyTopicARN 
      sns.subscribe(subscribeTopicInput).continueWithExecutor(AWSExecutor.mainThreadExecutor(), withBlock: { (topicTask: AWSTask!) -> AnyObject! in 

       if topicTask.error != nil { 
        // Authorization error prints here 
        print(topicTask.error) 
       } 

       return nil 
      }) 

     } 

     return nil 
    }) 

尝试当我收到错误订阅主题:

UserInfo={Type=Sender, Message=User: arn:aws:its::000000000000:assumed-role/appname_unauth_MOBILEHUB_000000000/CognitoIdentityCredentials is not authorized to perform: SNS:Subscribe on resource:...

this answer笔者解释说,您必须授予在Cognito角色访问sns:Subscribe,让您的应用程序,使这个电话。我的Cognito用户已被授予AmazonSNSFullAccess,允许访问所有sns操作(例如sns:*)。为什么我的Cognito用户被拒绝访问?我的主题策略已设置为只有主题所有者才能订阅...但主题所有者似乎与我的Cognito用户相同。

enter image description here

回答

1

我已经使用亚马逊移动中心配置推送通知我。我没有意识到,移动中心创建三个角色作为这一过程的一部分:

  1. appname_consolepush_MOBILEHUB_000000000
  2. appname_unauth_MOBILEHUB_000000000
  3. MobileHub_Service_Role

的iOS应用程序是使用appname_unauth_MOBILEHUB_00000000作用连接,而不是用户我手动创建。这个角色不允许sns:Subscribe

要解决,或者:

  1. 格兰特AmazonSNSFullAccess到适当的角色
  2. 创建一个内嵌的政策,让sns:Subscribe所有的资源(更好IMO)

例子:

{ 
    "Effect": "Allow", 
    "Action": [ 
     "sns:Subscribe" 
    ], 
    "Resource": [ 
     "*" 
    ] 
} 
+0

很高兴听到你的工作。一如果你有任何问题,随时张贴在这里或在我们的论坛。 –

+1

另一个注意事项:你不能只复制/粘贴上面,它将无法验证。不应该给予完全访问权限。最简单的方法是使用策略生成器并选择SNS和订阅。超级简单。 – user3344977