【问题标题】:Using Google places api in flutter使用 Google 将 api 置于 Flutter 中
【发布时间】:2021-02-16 17:20:45
【问题描述】:

我一直在 Flutter 中使用 Google Places Search,使用我自己的使用 Google Places API 的简单客户端服务。 我注意到还有其他专门为 Android 和 IOS 设计的 SDK - (Places SDK for Android / IOS)

当我查看 API 的密钥限制时,我意识到这似乎 Google Places API 旨在用于服务器端应用程序(密钥可以限制为某些 IP),而不能限制为 Android / IOS应用程序。

据我所知,每个人都可以破解应用程序然后他们可以获得 API。 这是在颤振中使用谷歌位置搜索的更安全的方式吗?

【问题讨论】:

    标签: flutter google-places-api


    【解决方案1】:

    我假设您的意思是来自 GG 服务的 API 密钥。像大多数 API 服务提供商一样,以最低限度的安全措施使用服务是关键。确实是服务的租用者(您)对滥用使用采取保护措施。

    假设您经营一家面包店,而 Google 用钥匙通过一扇门为您提供面包。如果您的所有客户都可以访问该密钥,他们将打开并使用任意数量的面包。解决方案是让您在后院拥有那扇门,并且只有您的员工(您的程序/服务)可以访问。然后客户可以购买您的面包,您可以将面包装饰得更漂亮以提高价格。

    我认为这不仅仅与 Flutter 有关。问题是您需要保护该 API 密钥。完全不允许用户访问它。一些防御技术可以实施。我会推荐阅读这个短名单:https://dev.to/bearer/api-security-best-practices-3gjl

    【讨论】:

    • 感谢您深入了解 API 的安全性。正如我所说,我的问题更多是关于谷歌的地方 API/SDK,它有三种类型。如果我开发原生 Android,我将使用 Place SDK for Android,对于 IOS 也是如此。但是对于 Flutter,我还不能使用其中的任何一个(据我所知 - 如果有一个包装大量 SDK 会很好)。而且我知道有很多人在他们的 Flutter 应用程序中只使用 Places API。我想知道他们如何保护 Places API 的 API 密钥。
    • 一个关于 Android 的类似问题,这里是答案。 stackoverflow.com/a/44745592/2641128
    【解决方案2】:

    pubspec.ymal

     flutter_inappwebview: ^5.3.2
    

    ma​​in.dart

    import 'dart:io';
    import 'package:flutter/material.dart';
    import 'package:flutter_inappwebview/flutter_inappwebview.dart';
    
    
    
    class MyChromeSafariBrowser extends ChromeSafariBrowser {
      @override
      void onOpened() {
        print("ChromeSafari browser opened");
      }
    
      @override
      void onCompletedInitialLoad() {
        print("ChromeSafari browser initial load completed");
      }
    
      @override
      void onClosed() {
        print("ChromeSafari browser closed");
      }
    }
    
    Future main() async {
      WidgetsFlutterBinding.ensureInitialized();
    
      if (Platform.isAndroid) {
        await AndroidInAppWebViewController.setWebContentsDebuggingEnabled(true);
      }
    
      runApp(MaterialApp(home: MyApp(), theme: new ThemeData(scaffoldBackgroundColor: const Color(0xFFA7A5A5)),
          debugShowCheckedModeBanner: false));
    }
    
    class MyApp extends StatefulWidget {
      final ChromeSafariBrowser browser = new MyChromeSafariBrowser();
    
      @override
      _MyAppState createState() => new _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      // @override
      // void initState() {
      //   widget.browser.addMenuItem(new ChromeSafariBrowserMenuItem(
      //       id: 1,
      //       label: 'Custom item menu 1',
      //       action: (url, title) {
      //         print('Custom item menu 1 clicked!');
      //       }));
      //   super.initState();
      // }
    
      TextEditingController controller=TextEditingController();
      var urlString="";
      launchUrl()
      async {
        String prefix = "https://www.google.com/search?q=";
        urlString=controller.text;
        if(!urlString.startsWith("http://") && !urlString.startsWith("https://")&&
            !urlString.endsWith(".com")  && !urlString.endsWith(".as") &&
            !urlString.endsWith(".uk") && !urlString.endsWith(".biz")&&!urlString.endsWith(".in")|| urlString.contains(" ") )
        {
          urlString=prefix+urlString;
        }
        if(urlString.endsWith(".com") || urlString.endsWith(".as") || urlString.endsWith(".uk") || urlString.endsWith(".biz")||urlString.endsWith(".in") )
        {
          if(!urlString.startsWith("http://") && !urlString.startsWith("https://"))
          {
            urlString = "http://"+urlString;
          }
        }
        await widget.browser.open(
            url: Uri.parse(urlString),
            options: ChromeSafariBrowserClassOptions(
                android: AndroidChromeCustomTabsOptions(
                  addDefaultShareMenuItem: false,),
                ios: IOSSafariOptions(barCollapsingEnabled: true)));
      }
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title:  TextField(
              autofocus: false,
              controller: controller,
              cursorColor: Colors.black,
              textInputAction: TextInputAction.search,
              onSubmitted: (url)=>launchUrl(),
              style: TextStyle(color: Colors.black),
              decoration: InputDecoration(
                border:InputBorder.none,
                hintText: "Search Google Or Enter URL",
                hintStyle: TextStyle(color:Colors.grey),
                filled: true,
                fillColor: Colors.white,
                prefixIcon: Icon(Icons.search),
              ),
            ),
          ),
    
          body:
    
    
          SingleChildScrollView(
            scrollDirection: Axis.vertical,
            child:
            Column(children: <Widget>[
    
    
              Row(children: <Widget>[
                Container(
                  // padding: EdgeInsets.all(50),
                  alignment: Alignment.center,
                  margin: const EdgeInsets.only(top: 30.0,left:10.0),
                  child:
                  Text(
                    'youtube.com/bappasaikh',
                    style: TextStyle(fontSize: 25),
                  ) ,
                ),
              ]),
    
    
    
    
    
            ]),
    
    
          ),
    
    
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-10
      • 2018-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-08
      • 2018-10-09
      相关资源
      最近更新 更多