无法使用swift SDK对aws appsync的用户进行身份验证

时间:2018-03-10 00:14:29

标签: swift xcode amazon-web-services amazon-cognito aws-appsync

我正在尝试使用AWS Swift SDK从我的Swift移动应用程序连接到我的AWS AppSync服务,但不断收到以下错误:

Error occurred: (401 unauthorized) Did not receive a successful HTTP code.

我正在使用用户池,并按照swift教程设置了所有内容。我的问题是,如何在我的请求中合并控制台中生成的AppSync.json配置文件?这在教程中没有提到,可能是我无法连接的原因。

json文件如下所示:

{
    "graphqlEndpoint": "my_endpoint_url",
    "region": "us-east-1",
    "authenticationType": "AMAZON_COGNITO_USER_POOLS",
    "apiKey": null
}

目前我正在使用以下配置:

// Set up  Amazon Cognito credentials
let credentialsProvider = AWSCognitoCredentialsProvider(regionType: CognitoIdentityRegion,
                                                        identityPoolId: CognitoIdentityPoolId, identityProviderManager: pool)
// You can choose your database location, accessible by SDK
let databaseURL = URL(fileURLWithPath:NSTemporaryDirectory()).appendingPathComponent(database_name)

do {
    // Initialize the AWS AppSync configuration

    let appSyncConfig = try AWSAppSyncClientConfiguration(url: AppSyncEndpointURL,
                                                          serviceRegion: AppSyncRegion,
                                                          credentialsProvider: credentialsProvider,
                                                          databaseURL:databaseURL)

    appSyncClient = try AWSAppSyncClient(appSyncConfig: appSyncConfig)
    // Set id as the cache key for objects
    appSyncClient?.apolloClient?.cacheKeyForObject = { $0["id"] }
} catch {
    print("Error initializing appsync client. \(error)")
}

编辑#1

事实证明,该示例使用的是API密钥方法,而不是用户池。所以现在我已将配置更改为:

let appSyncConfig = try AWSAppSyncClientConfiguration(url: AppSyncEndpointURL, serviceRegion: AppSyncRegion, userPoolsAuthProvider: CognitoUserPoolsAuthProvider(pool: pool))

问题是现在的信息是:

Use of unresolved identifier 'CognitoUserPoolsAuthProvider'

如果我尝试:

let appSyncConfig = try AWSAppSyncClientConfiguration(url: AppSyncEndpointURL, serviceRegion: AppSyncRegion, userPoolsAuthProvider: AWSCognitoUserPoolsAuthProvider(pool: pool))

错误是:

'AWSCognitoUserPoolsAuthProvider' cannot be constructed because it has no accessible initializers

不确定如何满足config中的userPoolsAuthProvider:参数。

1 个答案:

答案 0 :(得分:0)

要解决您的特定问题,userPoolsAuthProvider需要接受扩展AWSCognitoUserPoolsAuthProvider协议的类。因此,您的实例化应如下所示:

 let appSyncConfig = try AWSAppSyncClientConfiguration(url: AppSyncEndpointURL,
                                                          serviceRegion: AppSyncRegion,
                                                          userPoolsAuthProvider: self,
                                                          databaseURL:databaseURL)

然后在创建AppSyncClient的类中,您将像这样进行扩展:

extension YourClass: AWSCognitoUserPoolsAuthProvider {
  func getLatestAuthToken() -> String {
    var token: String = ""

    // pool is an instance of AWSCognitoIdentityUserPool
    self.pool?.currentUser()?.getSession().continueOnSuccessWith(block: { (task) -> Any? in 
      token = task.result!.idToken!.tokenString
      return nil
    }).waitUnitFinished()
  }

  return token
}

我还认为默认情况下,AppSync配置在您的项目中查找awsconfiguration.json文件。您前不久发布了此消息,因此AWS服务和AppSync可能发生了变化。

希望这会有所帮助