简体   繁体   English

向AWS Gateway API发出请求React Native

[英]Making requests to AWS Gateway API React Native

In my app, the user can authenticate via AWS Cognito User Pools or Facebook all managed an AWS identity pool. 在我的应用程序中,用户可以通过AWS Cognito用户池或Facebook进行身份验证,所有人都可以管理AWS身份池。 That's all working correctly, the user can login and authenticate successfully. 这一切都正常,用户可以成功登录和验证。 Now, I need to make authenticated requests to an AWS Gateway API which will then trigger Lambda functions. 现在,我需要向AWS Gateway API发出经过身份验证的请求,然后触发Lambda函数。 I'm confused as to what comes next. 我对接下来会发生什么感到困惑。 Do I need to write code to sign these requests or does the AWS javascript SDK already have something built in for this? 我是否需要编写代码来签署这些请求,或者AWS javascript SDK是否已经内置了这些内容? Do I need to create an Authorizer? 我需要创建一个授权人吗? How do I go from AWS.config.credentials to making successful, authenticated requests to a Gateway API? 如何从AWS.config.credentials转到对Gateway API进行成功的,经过身份验证的请求?

I'm using React Native so the auto generated APIs won't work. 我正在使用React Native,因此自动生成的API将无法正常工作。

EDIT: Here's my request code: 编辑:这是我的请求代码:

fetch('https://MY_API_GATEWAY_URL/prod/handleMessages/', { method: 'GET', body: null, headers: { Authorization: 'Bearer ' + this.state.token, /* this is the JWT token from AWS Cognito. */ }, }) .then((response) => { alert(JSON.stringify(response, null, 2)) })

I get 403 response from this with the exception: IncompleteSignatureException 我从此得到403响应,但异常:IncompleteSignatureException

At this point you should have a valid Cognito issued JWT. 此时,您应该拥有有效的Cognito颁发的JWT。 To call APIs behind AWS API Gateway you need to pass the JWT along with your API calls. 要在AWS API Gateway后面调用API,您需要将JWT与API调用一起传递。 This should go in the Authorization header with type of Bearer. 这应该在具有Bearer类型的Authorization标头中。 You can determine whether a custom authorizer is necessary or just use the built in authorization with API Gateway. 您可以确定是否需要自定义授权程序,或者仅使用API​​网关的内置授权。

Additional info can be found here - http://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-integrate-with-cognito.html 其他信息可以在这里找到 - http://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-integrate-with-cognito.html

You also need to ensure IAM rules are in place to allow the UserPool to access the API Gateway endpoints - http://docs.aws.amazon.com/apigateway/latest/developerguide/permissions.html 您还需要确保IAM规则到位以允许UserPool访问API网关端点 - http://docs.aws.amazon.com/apigateway/latest/developerguide/permissions.html

You can generate the JWT token from Server-side, after AWS Cognito UserPools Authentication, using a API Gateway Endpoint. 在AWS Cognito UserPools身份验证之后,您可以使用API​​网关端点从服务器端生成JWT令牌。 eg /<stage>/authenticate 例如/<stage>/authenticate

var authenticationData = {
    Username : 'username',
    Password : 'password',
};
var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
var poolData = { UserPoolId : 'us-east-1_TcoKGbf7n',
    ClientId : '4pe2usejqcdmhi0a25jp4b5sh3'
};
var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
var userData = {
    Username : 'username',
    Pool : userPool
};
var cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
    onSuccess: function (result) {
        console.log('access token + ' + result.getAccessToken().getJwtToken());
        /*Use the idToken for Logins Map when Federating User Pools with Cognito Identity or when passing through an Authorization Header to an API Gateway Authorizer*/
        console.log('idToken + ' + result.idToken.jwtToken);
    },

    onFailure: function(err) {
        alert(err);
    },

});

For more details, check this example . 有关更多详细信息,请查看此示例

After the authentication send the jwtToken back to the React Native app, where it needs to be sent in Authorization header with the token for subsequent API requests. 在身份验证之后,将jwtToken发送回React Native应用程序,在该应用程序中,需要在Authorization标头中发送该标记以及后续API请求的标记。

At API Gateway configure user pool authorizer in integration method configuration, so that it will automatically validate the authorization header and load user context information for the endpoint. 在API网关中,在集成方法配置中配置用户池授权程序,以便它自动验证授权标头并加载端点的用户上下文信息。

You can use the AWS Amplify library's API module which will automatically sign requests: https://github.com/aws/aws-amplify 您可以使用AWS Amplify库的API模块自动签署请求: https//github.com/aws/aws-amplify

For React Native this is available via npm: 对于React Native,这可以通过npm获得:

npm install aws-amplify-react-native

If using Cognito User Pools link the library as outlined here: https://github.com/aws/aws-amplify/blob/master/media/quick_start.md#react-native-development 如果使用Cognito User Pools链接库,如下所示: https//github.com/aws/aws-amplify/blob/master/media/quick_start.md#react-native-development

The API module will have you configure a friendly name for your endpoint and also the Invocation URL from an API Gateway stage. API模块将让您为端点配置友好名称,并从API网关阶段配置调用URL。 Then you simply call the HTTP method and pass optional parameters as options: 然后,您只需调用HTTP方法并将可选参数作为选项传递:

import Amplify, {API} from 'aws-amplify-react-native';
Amplify.configure('your_config_file_here');
API.get('apiname', 'invokeURL', options).then(res=>{
    console.log(res);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM