Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/fireinrain/opaitokens

A golang lib to help you to get openai access token and refresh the token(一个golang sdk,帮助你更容易的获取openai chatgpt的accessToken :)
https://github.com/fireinrain/opaitokens

access go golang openai openai-api token

Last synced: 1 day ago
JSON representation

A golang lib to help you to get openai access token and refresh the token(一个golang sdk,帮助你更容易的获取openai chatgpt的accessToken :)

Awesome Lists containing this project

README

        

# opaitokens
A golang lib to help you to get openai access token and refresh the token

# How to use?

## if you cant visit openai freely, then you need set HTTP_PROXY and HTTPS_PROXY env before use.
```bash
unix/linux/macos

export http_proxy=http://proxy.example.com:port
export https_proxy=http://proxy.example.com:port

windows

set http_proxy=http://proxy.example.com:port
set https_proxy=http://proxy.example.com:port

```

## official account
```go
email := "[email protected]"
password := "xxxxx"
//false mean you dont want to use fakeopenproxy to login
// you can set it as true, so when you dont have a vps for get your access with fakeopen api proxy
tokens := NewOpaiTokens(email, password, false)
token := tokens.FetchToken()
fmt.Printf("token info: %v\n", token)
accessToken := token.OpenaiToken.AccessToken
// use the access token
fmt.Printf("i am using access token: %v \n", accessToken)

token = tokens.RefreshToken()
fmt.Printf("token info again: %v\n", token)
accessToken = token.RefreshedToken.AccessToken
//use the refresh token
fmt.Println("i am using refresh token: ", accessToken)

```

## official account with MFA

```go
// if you have set MFA to the account, then use this below
email := "[email protected]"
password := "xxxxx"
mfa := "your mfa code"

tokens := NewOpaiTokensWithMFA(email, password,mfa,false)
token := tokens.FetchToken()
fmt.Printf("token info: %v\n", token)
accessToken := token.OpenaiToken.AccessToken
// use the access token
fmt.Printf("i am using access token: %v \n", accessToken)

token = tokens.RefreshToken()
fmt.Printf("token info again: %v\n", token)
accessToken = token.RefreshedToken.AccessToken
//use the refresh token
fmt.Println("i am using refresh token: ", accessToken)

```
## use share token with ai.fakenopen.com
```go

tokens := FakeOpenTokens{}
account := OpenaiAccount{
Email: "[email protected]",
Password: "xx@xx",
MFA: "",
}
token, err := tokens.FetchSharedToken(account, "fireinrain")
if err != nil {
fmt.Println("error: ", err)
}
fmt.Println(token.TokenKey)
```

## use pooled token with ai.fakenopen.com

```go
var accounts []OpenaiAccount
account := OpenaiAccount{
Email: "[email protected]",
Password: "xx@xx",
MFA: "",
}
accounts = append(accounts, account)
tokens := FakeOpenTokens{}
token, err := tokens.FetchPooledToken(accounts)
if err != nil {
fmt.Println("error: ", err)
}
fmt.Println(token)

```

## renew shared token for keep pooled token valid
```go
//主动在14天之内刷新所有账号的shared token 来确保pooled token有效
//可以使用
var accounts []OpenaiAccount
account := OpenaiAccount{
Email: "[email protected]",
Password: "xx@xx",
MFA: "",
}
accounts = append(accounts, account)
tokens := FakeOpenTokens{}
renewResult, err := tokens.RenewSharedToken(accounts)
if err != nil {
fmt.Println("error: ", err)
}
fmt.Println(renewResult)

```

## fetch pooled token with official accounts and offline sk keys
```go
var accounts []OpenaiAccount
account := OpenaiAccount{
Email: "[email protected]",
Password: "xx@xx",
MFA: "",
}
accounts = append(accounts, account)

var skKeys []string
skKeys = append(skKeys,"sk-xxxxxx")
tokens := FakeOpenTokens{}
token, err := tokens.FetchMixedPooledToken(accounts,skKeys)
if err != nil {
fmt.Println("error: ", err)
}
fmt.Println(token)

```

## if you have store the openai refresh token, congratulation! you can use openai official refresh token to get access token and then use in fakeopen fk and pk.
```go
func TestFakeOpenTokens_FetchSharedTokenWithRefreshToken(t *testing.T) {
openaiAccountEmail := "[email protected]"
openaiRefreshToken := "xxxxxxxx"
uniqueName := "fireinrain"
receiver := FakeOpenTokens{}
token, err := receiver.FetchSharedTokenWithRefreshToken(openaiAccountEmail, openaiRefreshToken, uniqueName)
if err != nil{
fmt.Println("error: ", err)
}
fmt.Printf("%v\n", token)

}

```

## if you want to get access token by openai session token, here you are.
```go
func TestFakeOpenTokens_FetchAccessTokenBySessionToken(t *testing.T) {
receiver := &FakeOpenTokens{}
session_token := "xxxxxxxxxxx"
token, err := receiver.FetchAccessTokenBySessionToken(session_token)
if err != nil {
println("error: ", err)
}
fmt.Println(token.AccessToken)
}

```
Note that every function with suffic "WithRefreshToken" will use openai official refresh token to get access token and then use in fakeopen fk and pk.

## Thanks for DartNode Platform.
![DartNode](./dt.jpg)

Get a free vps for your work, Let's go!
[DartNode](https://dartnode.com)