带有API密钥和Webview Flutter的发布请求

问题描述 投票:0回答:1

我有一个flask应用程序,该应用程序曾经从我的flutter应用程序中调用,创建了一些HTML文件并返回了文件名的位置。然后,我的flutter应用程序使用“ webview_flutter”包查看了该文件。在本地开发服务器中,这工作得很好。当我将其上传到AWS LAMbda时,我使用一些API密钥保护了应用程序。所以我的问题是如何现在使用http.post将此键调用此api,然后使用“ webview_flutter包”和该api_key查看创建的html文件。请帮助我。

flask flutter webview aws-lambda http-post
1个回答
0
投票

您可以使用软件包https://pub.dev/packages/amazon_cognito_identity_dartAPI网关+ Lambda的身份验证访问,请参阅https://github.com/jonsaw/amazon-cognito-identity-dart/#for-api-gateway--lambda

代码段

import 'package:http/http.dart' as http;
import 'package:amazon_cognito_identity_dart/cognito.dart';
import 'package:amazon_cognito_identity_dart/sig_v4.dart';

void main() async {
  final credentials = new CognitoCredentials(
      'ap-southeast-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', userPool);
  await credentials.getAwsCredentials(session.getIdToken().getJwtToken());

  const endpoint =
      'https://xxxx.execute-api.ap-southeast-1.amazonaws.com/dev';
  final awsSigV4Client = new AwsSigV4Client(
      credentials.accessKeyId, credentials.secretAccessKey, endpoint,
      sessionToken: credentials.sessionToken,
      region: 'ap-southeast-1');

  final signedRequest = new SigV4Request(awsSigV4Client,
      method: 'POST',
      path: '/projects',
      headers: new Map<String, String>.from(
          {'header-1': 'one', 'header-2': 'two'}),
      queryParams: new Map<String, String>.from({'tracking': 'x123'}),
      body: new Map<String, dynamic>.from({'color': 'blue'}));

  http.Response response;
  try {
    response = await http.post(
        signedRequest.url,
        headers: signedRequest.headers, body: signedRequest.body);
  } catch (e) {
    print(e);
  }
  print(response.body);
}
© www.soinside.com 2019 - 2024. All rights reserved.