无法使用Swift SDK对AWS AppSync的用户进行身份验证 [英] cannot authenticate user for aws appsync with swift SDK
问题描述
我正在尝试使用AWS Swift SDK从我的Swift移动应用程序连接到我的AWS AppSync服务,但始终出现以下错误:
I am trying to connect to my AWS AppSync service from my Swift mobile app using the AWS Swift SDK but keep getting the following error:
Error occurred: (401 unauthorized) Did not receive a successful HTTP code.
我正在使用用户池,并已按照教程快速进行了所有设置。我的问题是,如何在请求中合并控制台中生成的AppSync.json配置文件?该教程中未提及,可能是我无法连接的原因。
I am using User Pools and have set everything up following the tutorial for swift. My question is, how do I incorporate the AppSync.json config file generated in the console in my request? That is not mentioned in the tutorial and may be the reason I cannot connect.
json文件如下所示:
The json file looks like this:
{
"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)")
}
EDIT#1
事实证明该示例使用的是API密钥方法而非用户池,因此现在我将配置更改为:
It turns out the example is using the API key method rather than user pools. So now I have changed the config to:
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:参数。
Not sure how to satisfy the userPoolsAuthProvider: argument in the config.
推荐答案
要解决您的特定问题, userPoolsAuthProvider
需要接受一个扩展 AWSCognitoUserPoolsAuthProvider
协议的类。因此,您的实例化应如下所示:
To address your specific issue, userPoolsAuthProvider
needs to accept a class that extends the AWSCognitoUserPoolsAuthProvider
protocol. So your instantiation would look something like this:
let appSyncConfig = try AWSAppSyncClientConfiguration(url: AppSyncEndpointURL,
serviceRegion: AppSyncRegion,
userPoolsAuthProvider: self,
databaseURL:databaseURL)
您将在其中创建AppSyncClient的类,扩展如下:
And then in the class in which you are creating the AppSyncClient, you would extend like this:
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可能发生了变化。
Also I think the AppSync configuration by default looks for an awsconfiguration.json
file in your project. You posted this a while back so possibly things have changed with AWS services and AppSync.
希望这会有所帮助
这篇关于无法使用Swift SDK对AWS AppSync的用户进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!