0

J'essaie d'appeler un point de terminaison API Gateway à partir d'une application React Native en utilisant aws-sdk/dist/aws-sdk-react-native et un appel fetch standard. Le point de terminaison API Gateway nécessite une autorisation IAM. Pour ce faire, j'ai configuré un pool d'identités fédérées AWS Cognito et configuré le rôle IAM non authentifié pour qu'il ait une autorisation d'exécution sur l'API.«Demande erronée» accédant à la passerelle API avec demande signée de React Native

Le code ci-dessous s'exécute sur mon application React Native et est supposé obtenir un nouveau jeton de Cognito et l'utiliser pour signer une requête au point de terminaison API Gateway. Malheureusement, chaque fois que je le lance, je reçois une réponse d'erreur html comme suit:

<!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> 

Je ne peux pas comprendre ce que je fais mal ...

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) 
     }); 
}); 

Répondre