2016-02-18 189 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