2015-09-27 18 views
1

工作,我無法獲得開發者認證身份斯威夫特在所有的工作...無法爲獲得開發權威性身份斯威夫特

伊夫基於我的代碼找到本指南https://mobile.awsblog.com/post/Tx3E3NJURV1LNV1/Integrating-Amazon-Cognito-using-developer-authenticated-identities-An-end-to-en 的斯威夫特轉換在這裏 - https://stackoverflow.com/a/28514719/535363

我繼承AWSAbstractCognitoIdentityProvider類

import AWSCore 

class EmailIdentityProvider: AWSAbstractCognitoIdentityProvider { 
    var _token: String! 
    var _logins: [ NSObject : AnyObject ]! 
    override var token: String { 
     get { 
      return _token 
     } 
    } 

    override var logins: [ NSObject : AnyObject ]! { 

     get { 
      return _logins 
     } 
     set { 
      _logins = newValue 
     } 
    } 

    override func getIdentityId() -> AWSTask! { 

     if self.identityId != nil { 
      return AWSTask(result: self.identityId) 
     }else{ 
      return AWSTask(result: nil).continueWithBlock({ (task) -> AnyObject! in 
       if self.identityId == nil { 
        return self.refresh() 
       } 
       return AWSTask(result: self.identityId) 
      }) 
     } 
    } 

    override func refresh() -> AWSTask! { 

     let task = AWSTaskCompletionSource() 

     let tmp = NSMutableDictionary() 
     tmp.setObject("temp", forKey: "login.continualsuccess.com.motivation") 
     self.logins = tmp as [NSObject : AnyObject] 

     self.identityId = "us-east-1:xxxxxx-xxxx-xxxx-xxxx-90223fc9333f" 
     self._token = "eyJraWQiOiJ....ehTOEVhA" 

     return AWSTask(result: self.identityId) 
    } 
} 

經由

AWSLogger.defaultLogger().logLevel = AWSLogLevel.Verbose 

let identityProvider = EmailIdentityProvider() 

let cp = AWSCognitoCredentialsProvider(
    regionType: AWSRegionType.USEast1 , 
    identityProvider: identityProvider, 
    unauthRoleArn: "Cognito_ContinualSuccessUnauth_Role", 
    authRoleArn: "Cognito_ContinualSuccessAuth_Role" 

); 

let configuration = AWSServiceConfiguration(region: AWSRegionType.USEast1, credentialsProvider: cp) 
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration 

let syncClient = AWSCognito.defaultCognito() 
let dataSet = syncClient.openOrCreateDataset("myDataSet") 
dataSet.setString("123", forKey: "new") 
dataSet.synchronize() 

用下面的後端代碼調用。寫在Golang

我用它來生成()的字符串值,我把儘可能self.identityIdself._token我在刷新返回

var testCredentials = credentials.NewStaticCredentials("MY_KEY", "MY_SECRET", "") 

    svc := cognitoidentity.New(&aws.Config{Credentials: testCredentials,Region: aws.String("us-east-1")}) 

    params := &cognitoidentity.GetOpenIdTokenForDeveloperIdentityInput{ 
     IdentityPoolId: aws.String("us-east-1:xxxxxxx-xxxx-xxxx-xxxx-2fb2e08f7daf"), // Required 
     Logins: map[string]*string{ // Required 
      "login.continualsuccess.com.motivation": aws.String(userid), // Required 
      // More values... 
     }, 
     IdentityId: nil, 
     TokenDuration: aws.Int64(82800), 
    } 
    resp, err := svc.GetOpenIdTokenForDeveloperIdentity(params) 

錯誤即時得到的

AWSCognitoSQLiteManager.m line:1455 | -[AWSCognitoSQLiteManager filePath] | Local database is: /Users/Jase/Library/Developer/CoreSimulator/Devices/32D1AE52-E4A4-421D-BBC7-B32F30A03772/data/Containers/Data/Application/61402FEB-F499-4829-A051-013EF45EAF0A/Documents/CognitoData.sqlite3 
2015-09-27 19:25:28.322 SlideOutNavigation[42167:635443] AWSiOSSDKv2 [Debug] AWSCognitoSQLiteManager.m line:179 | __51-[AWSCognitoSQLiteManager initializeDatasetTables:]_block_invoke | sqlString = 'INSERT INTO CognitoMetadata(Dataset,ModifiedBy,IdentityId) VALUES (?,?,?)' 
2015-09-27 19:25:28.324 SlideOutNavigation[42167:635443] AWSiOSSDKv2 [Debug] AWSCognitoSQLiteManager.m line:282 | __53-[AWSCognitoSQLiteManager loadDatasetMetadata:error:]_block_invoke | query = 'SELECT LastSyncCount, LastModified, ModifiedBy, CreationDate, DataStorage, RecordCount FROM CognitoMetadata WHERE IdentityId = ? and Dataset = ?' 
2015-09-27 19:25:28.324 SlideOutNavigation[42167:635443] AWSiOSSDKv2 [Debug] AWSCognitoSQLiteManager.m line:386 | __73-[AWSCognitoSQLiteManager getRecordById_internal:datasetName:error:sync:]_block_invoke | query = 'SELECT LastModified, ModifiedBy, Data, Type, SyncCount, Dirty FROM CognitoData WHERE Key = ? AND IdentityId = ? AND Dataset = ?' 
2015-09-27 19:25:33.060 SlideOutNavigation[42167:635690] AWSiOSSDKv2 [Verbose] AWSURLRequestSerialization.m line:483 | -[AWSQueryStringRequestSerializer serializeRequest:headers:parameters:] | Request body: [RoleArn=Cognito_ContinualSuccessAuth_Role&Version=2011-06-15&RoleSessionName=iOS-Provider&Action=AssumeRoleWithWebIdentity&WebIdentityToken=eyJraW....4fehTOEVhA] 
2015-09-27 19:25:35.134 SlideOutNavigation[42167:635692] AWSiOSSDKv2 [Debug] AWSURLResponseSerialization.m line:257 | -[AWSXMLResponseSerializer responseObjectForResponse:originalRequest:currentRequest:data:error:] | Response header: [{ 
    Connection = close; 
    "Content-Length" = 269; 
    "Content-Type" = "text/xml"; 
    Date = "Sun, 27 Sep 2015 09:25:34 GMT"; 
    "x-amzn-RequestId" = "b45aec1d-64f9-11e5-8fbd-d148bd98dc03"; 
}] 
2015-09-27 19:25:35.135 SlideOutNavigation[42167:635692] AWSiOSSDKv2 [Verbose] AWSURLResponseSerialization.m line:262 | -[AWSXMLResponseSerializer responseObjectForResponse:originalRequest:currentRequest:data:error:] | Response body: [<ErrorResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/"> 
    <Error> 
    <Type>Sender</Type> 
    <Code>ValidationError</Code> 
    <Message>Request ARN is invalid</Message> 
    </Error> 
    <RequestId>b45aec1d-64f9-11e5-8fbd-d148bd98dc03</RequestId> 
</ErrorResponse> 
] 
2015-09-27 19:25:35.139 SlideOutNavigation[42167:635692] AWSiOSSDKv2 [Error] AWSCredentialsProvider.m line:527 | __40-[AWSCognitoCredentialsProvider refresh]_block_invoke352 | Unable to refresh. Error is [Error Domain=com.amazonaws.AWSSTSErrorDomain Code=0 "(null)" UserInfo={Type=Sender, Message=Request ARN is invalid, Code=ValidationError, __text=(
    "\n ", 
    "\n ", 
    "\n ", 
    "\n " 
)}] 
2015-09-27 19:25:35.140 SlideOutNavigation[42167:635692] AWSiOSSDKv2 [Error] AWSCognitoDataset.m line:315 | __30-[AWSCognitoDataset syncPull:]_block_invoke | Unable to list records: Error Domain=com.amazonaws.AWSSTSErrorDomain Code=0 "(null)" UserInfo={Type=Sender, Message=Request ARN is invalid, Code=ValidationError, __text=(
    "\n ", 
    "\n ", 
    "\n ", 
    "\n " 
)} 
+0

嘿你用什麼令牌?你有沒有得到這個工作大聲笑 – Lamar

+0

是的,我最終得到它的工作....我想我只是硬編碼由我的後端調用GetOpenIdTokenForDeveloperIdentityInput()在EmailIdentityProvider Swift類中返回的令牌值。我認爲你需要調用你的後端,以獲得一個新的令牌,如果當前過期...我將不得不再次看到這一切,但在嘗試獲得它的噩夢之後,就像我一樣,我只是想從它休息一下... –

+0

我是否必須打電話給我的後端,我使用雲層管理器會自動執行它嗎? – Lamar

回答

2

它看起來就像你用你的角色名字,而不是角色ARN(亞馬遜資源名稱)初始化您的憑證提供程序。

角色ARN的形式如下:arn:aws:iam::1234567890:role/ROLE_NAME

您可以使用提供程序中的ARN替換角色名稱,也可以將憑據提供程序中的角色值保留爲零,這將告知Cognito使用與身份池關聯的角色。

後面是增強流程;你可以在我們的developer guide中瞭解更多。

+0

感謝鮑勃!我用上面描述的形式替換了ARN的角色名稱。然而,我現在得到一個UnknownOperationException:完整的調試錯誤日誌 - https://gist.github.com/anonymous/51b7fc80e2e1b895158d 然而,它確實工作,當我把nil作爲unauthRoleArn和authRoleArn參數:) –

+0

@nullptr UnknownOperationException到證書提供者沒有身份池ID的事實。 –

+0

@nullptr具體來說,Cognito Sync客戶端需要能夠從身份提供者訪問的憑證提供程序加載身份池ID,因此您需要將其添加到自定義提供程序中。 –