get_token.go
// ---------------------------------------------------------------
//
// get_token.go
//
// Feb/06/2021
// ---------------------------------------------------------------
package main
import (
"fmt"
"os"
"io/ioutil"
"net/http"
"net/url"
"encoding/json"
"github.com/joho/godotenv"
)
// ---------------------------------------------------------------
func main() {
fmt.Fprintf (os.Stderr,"*** 開始 ***\n")
email := os.Args[1]
password := os.Args[2]
fmt.Fprintf (os.Stderr,"email = " + email + "\n")
fmt.Fprintf (os.Stderr,"password = " + password + "\n")
err := godotenv.Load(".env")
if err != nil {
panic(err)
}
api_key := os.Getenv("API_KEY")
url_target := "https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=" + api_key
args := url.Values{}
args.Add("email",email)
args.Add("password",password)
args.Add("returnSecureToken", "True")
res, err := http.PostForm(url_target,args)
if err != nil {
fmt.Println("Request error:", err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println("Request error:", err)
return
}
str_json := string(body)
var unit_aa map[string]interface{}
json.Unmarshal ([]byte(str_json), &unit_aa )
token := unit_aa["idToken"]
fmt.Println(token)
fmt.Fprintf (os.Stderr,"*** 終了 ***\n")
}
// ---------------------------------------------------------------
.env
API_KEY = "AIzaS....."
実行スクリプト
go run get_token.py ppp@example.com hello888 > token01.txt
go run get_token.py qqq@example.com hello999 > token02.txt