0

Ich versuche, einen API-Gateway-Endpunkt von einer React Native-Anwendung mit aws-sdk/dist/aws-sdk-react-native und einem normalen fetch Aufruf aufzurufen. Der API-Gateway-Endpunkt erfordert eine IAM-Autorisierung. Zu diesem Zweck habe ich einen Cognito Federated Identity-Pool von AWS eingerichtet und die nicht authentifizierte IAM-Rolle so konfiguriert, dass sie eine Ausführungserlaubnis über die API hat."Ungültige Anfrage" auf API-Gateway mit signierter Anfrage von React Native zugreifen

Der folgende Code läuft auf meiner React Native App und soll ein neues Token von Cognito erhalten und damit eine Anfrage an den API Gateway Endpunkt signieren. Leider erhalten Sie jedes Mal, wenn ich es betreibe ich eine HTML-Fehlerreaktion wie folgt:

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

ich kann nicht herausfinden, was ich falsch mache ...

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

Antwort

Verwandte Themen