我在一个与mobilehub项目相关的离子项目中配置了放大器 .
使用无服务器框架创建了api:
functions:
device:
handler: handler.device
events:
- http:
path: devices
method: get
authorizer: aws_iam
- http:
path: devices
method: put
authorizer: aws_iam
- http:
path: devices
method: post
authorizer: aws_iam
- http:
path: devices
method: delete
authorizer: aws_iam
根据我的知识,API工作和我的认知用户池相关联 . 事实上,当我调用get API时,我会收到一条“遗失令牌”消息 .
我正在尝试在Ionic应用程序中实现API调用 . 该应用程序是Ionic aws
模板,因此它可以毫无问题地登录到cognito .
我从mobilehub页面下载了'aws-exports.js'文件,包含在我的源代码中并导入
import awsmobile from '../../aws-exports';
import Amplify, { API } from 'aws-amplify';
ionViewDidLoad(){
Amplify.configure(awsmobile);
console.log(Amplify);
let apiName = 'dev-parameters';
let path = '/devices';
API.get(apiName, path).then(response => {
debugger;
console.log(response);
});
}
但是我可以从http调用中看到,调用 OPTIONS
会返回403 .
请求:
:method GET
:authority cx1smjqtj5.execute-api.eu-west-1.amazonaws.com
:scheme https
:path /dev/devices
user-agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 OPR/50.0.2762.67
upgrade-insecure-requests 1
accept text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q= 0.8
accept-encoding gzip, deflate, br
accept-language en-GB,en-US;q=0.9,en;q=0.8
响应:
{
"message": "Missing Authentication Token"
}
我真的不明白我在这里错过了什么或做错了什么 .
1 回答
此配置中的一切都是正确的 . 我能够通过在typescript中编译aws-export.js(在将其重命名为aws-exports.ts之后)来使其工作 . 之后,API调用将被正确验证 .
tsc aws-export.ts
生成一个新的aws-exports.js,该模块正确导入 .