批量生成chatGPT apiKey

538 阅读1分钟

日期:2023年5月11日

目前免费账号一分钟只能请求三次,超过就会删key

批量生成chatGPT apiKey

authorization要去控制台请求头里面拿

代码实现

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"net/http"

	"github.com/google/uuid"
	"github.com/tidwall/gjson"
)

func main() {
	s := getApiKey(uuid.New().String(), "you authorization")
	fmt.Println(s)
}

func getApiKey(keyName, authorization string) string {
	httpposturl := "https://api.openai.com/dashboard/user/api_keys"
	var jsonData = []byte(fmt.Sprintf(`{
		"action": "create",
		"name": "%s"
	}`, keyName))
	request, err := http.NewRequest("POST", httpposturl, bytes.NewBuffer(jsonData))
	if err != nil {
		panic(err)
	}
	request.Header.Set("Content-Type", "application/json")
	request.Header.Set("Authorization", authorization)

	client := &http.Client{}
	response, err := client.Do(request)
	if err != nil {
		panic(err)
	}
	defer response.Body.Close()
	body, _ := ioutil.ReadAll(response.Body)
	return gjson.Get(string(body), "key.sensitive_id").String()
}