g_captcha 1.0.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 73

Flutter g_captcha #

A flutter plugin for reCAPTCHA v2 - android. The FREE anti-abuse service.

What is reCAPTCHA? #

reCAPTCHA is a free service that protects your site from spam and abuse. It uses advanced risk analysis engine to tell humans and bots apart. With the new API, a significant number of your valid human users will pass the reCAPTCHA challenge without having to solve a CAPTCHA (See blog for more details). reCAPTCHA comes in the form of a widget that you can easily add to your blog, forum, registration form, etc.

Please check docs for further details.

Sign up for an API key pair #

To use reCAPTCHA, you need to sign up for an API key pair for your site. The key pair consists of a site key and secret. The site key is used to display the widget on your site. The secret authorizes communication between your application backend and the reCAPTCHA server to verify the user's response. The secret needs to be kept safe for security purposes.

Config 'android package name' to reCaptcha's admin console

You need to add 'android package name' to reCaptcha's admin console, or you will always get error: RECAPTCHA_INVALID_PACKAGE_NAME

Usage #

1. Add dependency to pubspec.yaml #

dependencies:
....g_captcha: ^1.0.0

Don't forget this

flutter pub get

2. Import in dart file #

import 'package:g_captcha/g_captcha.dart';

3. Config CAPTCHA_SITE_KEY and Call out #

const String CAPTCHA_SITE_KEY = "CAPTCHA_SITE_KEY_HERE";
...
String tokenResult = await GCaptcha.reCaptcha(CAPTCHA_SITE_KEY);
print('tokenResult: $tokenResult');

avatar

Verify token (java with okhttp demo) #

https://developers.google.com/recaptcha/docs/verify

package com.yuanchongyu.recapcha;

import okhttp3.FormBody;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.json.JSONObject;

public class Main {

    // TODO
    private static final String RECAPTCHA_SECRET_KEY = "RECAPTCHA_SECRET_KEY_HERE";

    private static final String RECAPTCHA_VERIFY_URL = "https://www.google.com/recaptcha/api/siteverify";

    public static void main(String[] args) throws Exception {
        // TODO
        boolean success = verify("03AGdBq25vYY080h0Wvk-XpCkhvEoBxS2YK-EbGqWq5Ru_hWxCt2XfGr7S8LMB9z3aU411MGXRoTSIQ_OvBeFSIqLNsxLyOUDCFOXzh1DYGMbaMvnc0FfqnfFc1yWu3fK6fYNSb09QVbUKeuifpYo6GBX6GiqOEu-AjIbZMz8TxkBUbBw9VpQG2PmfREPNwV6dWVpEQe4-oy-SP3IL94DFdTrkRoYQoCfZsSpTuGXh1gepxuqn-VJOBbxeFy_Qsha1BFYRvp2reifIX9Fd18jcToYI1OVLhQmRgM1shYNoszAnRjVSGFNfE6M");
        System.out.println("verify result: " + success);
    }

    private static boolean verify(String token) throws Exception {
        RequestBody formBody = new FormBody.Builder().add("secret", RECAPTCHA_SECRET_KEY).add("response", token)
            .build();
        Response response = post(RECAPTCHA_VERIFY_URL, formBody);
        String resp = response.body().string();
        return new JSONObject(resp).getBoolean("success");
    }

    private Response post(String url, RequestBody body) throws IOException {
        Request request = new Request.Builder().url(url).post(body).build();
        Response response = httpClient.newCall(request).execute();
        if (!response.isSuccessful()) {
            throw new IOException("request error: " + response);
        }
        return response;
    }
}

1.0.0 #

  • first version

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:g_captcha/g_captcha.dart';
import 'package:fluttertoast/fluttertoast.dart';

// TODO
const String CAPTCHA_SITE_KEY = "6LcKqfQUAAAAAC1I5Bjg0WI9RMc6wK9gjwG29Nr3";

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: const Text('Plugin example app'),
          ),
          body: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              RaisedButton(onPressed: _openReCaptcha, child: Text('reCaptcha')),
            ],
          )),
    );
  }

  _openReCaptcha() async {
    String tokenResult = await GCaptcha.reCaptcha(CAPTCHA_SITE_KEY);
    print('tokenResult: $tokenResult');
    Fluttertoast.showToast(msg: tokenResult, timeInSecForIosWeb: 4);

    // setState
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  g_captcha: ^1.0.0

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:g_captcha/g_captcha.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
54
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
83
Overall:
Weighted score of the above. [more]
73
Learn more about scoring.

We analyzed this package on Jul 8, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform linux

Because of import path [package:g_captcha/g_captcha.dart] that declares support for platforms: android, ios

Package does not support Flutter platform macos

Because of import path [package:g_captcha/g_captcha.dart] that declares support for platforms: android, ios

Package does not support Flutter platform web

Because of import path [package:g_captcha/g_captcha.dart] that declares support for platforms: android, ios

Package does not support Flutter platform windows

Because of import path [package:g_captcha/g_captcha.dart] that declares support for platforms: android, ios

Package not compatible with SDK dart

because of import path [g_captcha] that is in a package requiring null.

Health issues and suggestions

Document public APIs. (-1 points)

4 out of 4 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Maintenance suggestions

The package description is too short. (-17 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.1
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8
Dev dependencies
flutter_test