2

我正在尝试使用 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

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

4

1 回答 1

1

要解决您的特定问题,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 可能发生了变化。

希望这可以帮助

于 2019-02-20T14:21:39.463 回答