-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_test.go
34 lines (29 loc) · 981 Bytes
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package recaptcha_test
import (
"context"
"fmt"
"github.com/nicheinc/recaptcha"
)
func Example() {
// Clients are threadsafe, and should normally be created once and reused
// throughout the life of your application.
client := recaptcha.NewClient("my_secret")
// Fetch the token verification response from the reCAPTCHA endpoint. The
// userIP is optional, and can be omitted from the request by passing an
// empty string.
response, err := client.Fetch(context.Background(), "token", "user_ip")
if err != nil {
fmt.Printf("Error making request to reCAPTCHA endpoint: %s\n", err)
}
// Verify the response, with additional optional verification criteria.
// Verifying the hostname and action is strongly recommended, at a minimum.
if err := response.Verify(
recaptcha.Hostname("my_hostname"),
recaptcha.Action("my_action"),
recaptcha.Score(.5),
); err != nil {
fmt.Printf("Token is invalid: %s\n", err)
} else {
fmt.Println("Token is valid")
}
}