Code Monkey home page Code Monkey logo

opaitokens's Introduction

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.

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

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

// 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

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

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

//主动在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

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.

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.

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

Get a free vps for your work, Let's go! DartNode

opaitokens's People

Contributors

fireinrain avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

opaitokens's Issues

access_denied

partSix's Location of header include:
【OpenAI's services are not available in your country. (error=unsupported_country)】

提示:error request preauth code

image

用浏览器能访问openai,网络没问题,下面是main文件

package main
import (
"fmt"
"github.com/fireinrain/opaitokens"
)
func main() {
email := "account"
password := "passwd"

tokens := opaitokens.NewOpaiTokens(email, password, false)
token := tokens.FetchToken()
fmt.Printf("token info: %v\n", token)
accessToken := token.OpenaiToken.AccessToken
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
fmt.Println("I am using refresh token: ", accessToken)

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.