The demo of golang sdk is based on net/http.


Item Description
Data communication flow chart Data communication flow chart
Download SDK gt3-golang-sdk

Golang SDK Installation

Download​ ​SDK

Use​ ​command​ ​line​ ​to​ ​import​ ​from​ ​Github

go get github.com/GeeTeam/gt3-golang-sdk

API example

Captcha initialization

Initiate captcha via API1, get challenge and set the status

func registerGeetest(w http.ResponseWriter, r *http.Request) {

geetest := geetest.NewGeetestLib(captchaID, privateKey, 2 * time.Second)
status, response := geetest.PreProcess("", "")
session, _ := store.Get(r, "geetest")
session.Values["geetest_status"] = status
session.Save(r, w)

Notice:The gtServerStatus and success refer to the status of API1. 1 for uptime , or 0 for downtime procedures.
The gtServerStatus is stored in session via API1. Then, the API2 gets gtServerStatus from session and decides which procedures will be executed. The value of the session is 1 for uptime procedures, or 0 for downtime procedures.
The success is a value which API1 returns to the front end and controls the user interface of the front end. The value of the success is 1 for uptime user interface, or 0 for downtime user interface.

Secondary verification (API2), including uptime and downtime

func validateGeetest(w http.ResponseWriter, r *http.Request) {
var geetestRes bool
geetest := geetest.NewGeetestLib(captchaID, privateKey, 2 * time.Second)
res := make(map[string]interface{})
session, _ := store.Get(r, "geetest")
challenge := r.Form.Get("geetest_challenge")
validate := r.Form.Get("geetest_validate")
seccode := r.Form.Get("geetest_seccode")
val := session.Values["geetest_status"]
status := val.(int8)
if status == 1 {
geetestRes = geetest.SuccessValidate(challenge, validate, seccode, "", "")
} else {
geetestRes = geetest.FailbackValidate(challenge, validate, seccode)
if geetestRes {
res["code"] = 0
res["msg"] = "Success"
} else {
res["code"] = -100
res["msg"] = "Failed"
response, _ := json.Marshal(res)

Notice:How to simulate the Failback mode? Please fill in an incorrect string (e.g. 123456789) for the captcha ID. Then, it will enter the Failback mode.

Run demo

  1. Enter the root directory of your SDK project, then executes the command:
  2. go run main.go
  3. To view the demo, please visit http://localhost:8888/static/login.html in​ ​browser​.

Troubleshooting: secondary verification failure

  1. SDK internal logic errors. Please check: a) whether session is stored and read successfully, b) whether the code could successfully process to the module which send a request to GeeTest server, c) check the return value of GeeTest server.
  2. Multiple submission. The API which initiates the secondary verification is only available for one time.
  3. The challenge is inconsistent. Please ensure the consistency of challenge during the whole verification process.
  4. Check if gt and key in the code are correct in SDK. Please check if the parameters has been passed correctly.
  5. Please provide challenge to our service team. They could help you to check the log.