0
aws-sdk/dist/aws-sdk-react-nativeと通常のfetch
コールを使用して、React NativeアプリケーションからAPIゲートウェイエンドポイントを呼び出そうとしています。 APIゲートウェイエンドポイントにはIAM認可が必要です。その目的のために、私はAWS Cognito連合アイデンティティプールを設定し、Unauthenticated IAM RoleをAPIに対する実行権限を持つように設定しました。React Nativeからの署名付きリクエストにAPIアクセスする「不正リクエスト」
以下のコードはReact Nativeアプリで実行され、Cognitoから新しいトークンを取得し、APIゲートウェイエンドポイントへのリクエストに署名するために使用されています。次のように残念ながら、私はそれを実行するたびに私は、HTMLのエラー応答を受信します。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<TITLE>ERROR: The request could not be satisfied</TITLE>
</HEAD><BODY>
<H1>ERROR</H1>
<H2>The request could not be satisfied.</H2>
<HR noshade size="1px">
Bad request.
<BR clear="all">
<HR noshade size="1px">
<PRE>
Generated by cloudfront (CloudFront)
Request ID: fiYpmNCsqwm7D9yUjNOmBCLisxtKNBiV2EO6X-eeKpbpmwk6rkkMJQ==
</PRE>
<ADDRESS>
</ADDRESS>
</BODY></HTML>
私は
書かれたようimport AWS from 'aws-sdk/dist/aws-sdk-react-native';
let region = 'us-west-2';
AWS.config.region = region;
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'xxxxxxxxxxxx',
});
let host = 'https://xxxxxxx.execute-api.us-west-2.amazonaws.com';
let route = '/beta/api/foo';
let url = `${host}${route}`;
AWS.config.credentials.get(function(err) {
console.log('AWS.config.credentials', AWS.config.credentials);
var httpRequest = new AWS.HttpRequest(url);
httpRequest.method = 'GET';
httpRequest.path = route;
httpRequest.region = region;
httpRequest.headers['Host'] = host;
httpRequest.headers['Content-Type'] = "application/json";
var service = "execute-api";
var v4signer = new AWS.Signers.V4(httpRequest, service);
v4signer.addAuthorization(AWS.config.credentials, new Date());
fetch(url, httpRequest)
.then(resp => {
console.log('API gateway response', resp)
// Should receive JSON, but currently getting text with HTML error message
// return resp.json();
return resp.text();
})
.then(txt => {
console.log("API Gateway result", txt)
})
.catch(err => {
console.log('Failed call to API Gateway', err)
});
});