我正在使用angular 5.x和aws-amplify构建一个项目。我成功地通过aws-cognito注册、确认和登录我的用户,现在,我想检索用户jwt
并将其添加到请求标头以对我的dynamoDb集合执行 CRUD 操作。
不幸的是,当我尝试在发电机上执行此类操作时,我收到以下错误:
{
"message": "Authorization header requires 'Credential' parameter. Authorization header requires 'Signature' parameter. Authorization header requires 'SignedHeaders' parameter. Authorization header requires existence of either a 'X-Amz-Date' or a 'Date' header. Authorization=xxxxx"
}
我使用以下Cognito.service获取用户的令牌:
import { Injectable } from '@angular/core';
/** rxjs **/
import { fromPromise } from 'rxjs/observable/fromPromise';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
/** 3rd party **/
import { Auth } from 'aws-amplify';
@Injectable()
export class CognitoService {
getJwtToken(): Observable<any> {
return this.getCurrentSession()
.switchMap((token) => {
return of(token.getIdToken().getJwtToken());
})
}
private getCurrentSession(): Observable<any> {
return fromPromise(Auth.currentSession());
}
}
该 get 由以下方式调用:
this.cognitoService.getJwtToken()
.subscribe((token: string) => {
this.dynamoService.get(
'testResource?id=someValue',
{
Authorization: token
}
)
})
Dynamo.service如下:
import { Injectable } from '@angular/core';
/** rxjs **/
import { fromPromise } from 'rxjs/observable/fromPromise';
import { Observable } from 'rxjs/Observable';
/** 3rd party **/
import { API } from 'aws-amplify';
/** App Environment **/
import { environment } from '../../../environments/environment';
@Injectable()
export class DynamoDBService {
apiName = environment.amplify.API.endpoints[0].name;
get(path: string, headers: any): Observable<any> {
return fromPromise(
API.get(
this.apiName,
path,
{
headers: headers
}
)
);
}
}
我的环境如下:
export const environment = {
production: false,
amplify: {
Auth: {
region: 'eu-central-1',
identityPoolId: 'eu-central-1:xxx',
userPoolId: 'eu-central-1_xxx',
userPoolWebClientId: 'xxxx'
},
API: {
endpoints: [
{
name: "someName,
endpoint: "xxxxx"
}
]
}
}
};
当然,在我的应用程序启动时,我正在配置放大,例如:
...
/** App Environment **/
import { environment } from '../../environments/environment';
...
Amplify.configure(environment.amplify);
...
在我的api-gatway 上,我启用了标准CORS
、Authorization
必需和无Token
验证。我觉得我做的一切都很好,我不明白我做错了什么。
编辑:
Authorization
header 在使用API.get(...)
和传递一个 header 对象时得到正确设置,例如:
{
Authorization: 'myToken'
}
更多内容可以在以下链接aws.github.io/aws-amplify 阅读
有什么建议吗?