我创建了一个单视图应用程序in Xcode,然后我按照AWS Amplify iOS SDK指南使用我现有的Cognito用户池插入身份验证。工作得很漂亮。
然后,使用AWS Amplify,我added a REST API允许我的iOS应用程序通过Amplify生成的API网关和Lambda函数访问预先存在的DynamoDB表。但是,我必须重新连接所有内容才能使用我之前存在的Cognito用户池,因为Amplify CLI意外地创建了一个链接到它创建的REST API的新用户池。
我在尝试调用我的API时遇到以下错误,无论我做什么,我都无法让它消失:
Optional("{\"message\":\"Authorization header requires \'Credential\' parameter.
Authorization header requires \'Signature\' parameter. Authorization header requires
\'SignedHeaders\' parameter.
Authorization=abJarWQiOiJyR0NiTHFsbVwvbWdGeXgzcVA1YmNRaV...
403
更新5如需更多血腥细节,请参阅我的Amplify AWS iOS SDK issue。 :)
更新4此更新优先于我之前的更新,因为它现在看起来很清楚 - 正如上面的错误所示 - 我没有通过我的iOS应用程序的经过身份验证的Cognito用户正确调用我的REST API。我注意到Amplify创建了一个带有两个App Client的用户池,一个用于Web访问,另一个用于移动访问。我之前存在的用户池只有一个为Web访问量身定制的App Client。所以我创建了一个新的移动访问App Client,带有App Client Secret。
不幸的是,我得到了同样的错误。所以我想知道是否需要重新连接其他东西;或许我有一个令牌问题。正如我在下面的评论中提到的,也许我不应该使用REST API指南中指定的OIDC令牌?也许我应该使用AWS Credentials?结束更新4
我的API有一个资源/items
,我有一个ANY
和一个OPTIONS
方法。 ANY
方法没有为方法请求指定任何URL查询字符串参数。但它确实为请求体提供了RequestSchema
类型的application/json
。
我的API调用函数正是specified in the AWS Amplify iOS SDK for the REST API(我按照他们的指示使用Cognito User Pools授权程序调用我的API网关端点),除了我将httpMethodName
从POST
更改为GET
,因为POST导致了崩溃。这是它的样子:
func doInvokeAPI(token:String) {
// change the method name, or path or the query string parameters here as desired
let httpMethodName = "GET"
// the guid is the partition key for the DynamoDB table
let URLString = "/items/178dc797-4e3d-5bc4-815f-a280536fcd3a"
//let queryStringParameters = ["key1":"{value1}"]
let headerParameters = [
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": token
]
//let httpBody = "{ \n " +
// "\"key1\":\"value1\", \n " +
// "\"key2\":\"value2\", \n " +
// "\"key3\":\"value3\"\n}"
// Construct the request object
let apiRequest = AWSAPIGatewayRequest(httpMethod: httpMethodName,
urlString: URLString,
queryParameters: nil,
headerParameters: headerParameters,
httpBody: nil)
...
...
invocationClient.invoke(apiRequest).continueWith { (task: AWSTask) -> Any? in
if let error = task.error {
print("Error occurred: \(error)")
// Handle error here
return nil
}
// Handle successful result here
let result = task.result!
let responseString = String(data: result.responseData!, encoding: .utf8)
print(responseString as Any)
print(result.statusCode)
return nil
}
}
上面给出的错误由print(responseString)
打印。
我的DynamoDB表期望:userId
的分区键。 Guid字符串。
我很确定我想要传递的API是单个userId
,以便API可以检索与之关联的所有项目。不需要我的排序键。
我是如何通过API网关REST API进行此GET请求的?
更新1我的Lambda函数有两个JavaScript文件:index.js
和app.js
,由Amplify创建。
index.js
很短:
const awsServerlessExpress = require('aws-serverless-express');
const app = require('./app');
const server = awsServerlessExpress.createServer(app);
exports.handler = (event, context) => {
console.log(`EVENT: ${JSON.stringify(event)}`);
awsServerlessExpress.proxy(server, event, context);
};
app.js
包括一些设置(第一个截图),然后是get
,put
,post
,delete
的一系列方法。我只是包含get
方法的两个截图 - 这个问题的主题)。
更新2这是我单击我的API的ANY
方法的集成请求时看到的内容。请注意,我没有映射模板的下拉列表。
更新3但是我确实有一个扩展创建的Prod
阶段。但是我仍然看不到任何地方的映射模板。
"/items/{userId}"
只是一个字符串。试试"/items/\(userId)"
对不起,我没有足够的积分发表评论。