我正在尝试使用 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: 参数。