acheong08 / ChatGPT

Reverse engineered ChatGPT API
GNU General Public License v2.0
28k stars 4.48k forks source link

V1: retrieve the authentication token and update the config file automatically. #1314

Closed hongyi-zhao closed 1 year ago

hongyi-zhao commented 1 year ago

When using V1 for querying, the following error occurred

Chatbot: 
Traceback (most recent call last):
  File "/home/werner/Public/repo/github.com/acheong08/ChatGPT.git/src/revChatGPT/V1.py", line 680, in __check_response
    response.raise_for_status()
  File "/home/werner/.pyenv/versions/datasci/lib/python3.11/site-packages/requests/models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://bypass.churchless.tech/api/conversation

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/werner/Public/repo/github.com/acheong08/ChatGPT.git/src/revChatGPT/V1.py", line 1290, in main
    for data in chatbot.ask(prompt, auto_continue=True):
  File "/home/werner/Public/repo/github.com/acheong08/ChatGPT.git/src/revChatGPT/V1.py", line 568, in ask
    yield from self.post_messages(
  File "/home/werner/Public/repo/github.com/acheong08/ChatGPT.git/src/revChatGPT/V1.py", line 525, in post_messages
    yield from self.__send_request(
  File "/home/werner/Public/repo/github.com/acheong08/ChatGPT.git/src/revChatGPT/V1.py", line 376, in __send_request
    self.__check_response(response)
  File "/home/werner/Public/repo/github.com/acheong08/ChatGPT.git/src/revChatGPT/V1.py", line 54, in wrapper
    out = func(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^
  File "/home/werner/Public/repo/github.com/acheong08/ChatGPT.git/src/revChatGPT/V1.py", line 687, in __check_response
    raise error from e
revChatGPT.typings.Error: OpenAI: {"detail":{"message":"Your authentication token has expired. Please try signing in again.","type":"invalid_request_error","param":null,"code":"token_expired"}} (code: 401)
Please check that the input is correct, or you can resolve this issue by filing an issue
Project URL: https://github.com/acheong08/ChatGPT

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/home/werner/Public/repo/github.com/acheong08/ChatGPT.git/src/revChatGPT/__main__.py", line 63, in <module>
    main()
  File "/home/werner/Public/repo/github.com/acheong08/ChatGPT.git/src/revChatGPT/__main__.py", line 47, in main
    V1.main(V1.configure())
  File "/home/werner/Public/repo/github.com/acheong08/ChatGPT.git/src/revChatGPT/V1.py", line 54, in wrapper
    out = func(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^
  File "/home/werner/Public/repo/github.com/acheong08/ChatGPT.git/src/revChatGPT/V1.py", line 1300, in main
    raise error from exc
revChatGPT.typings.CLIError: command line program unknown error
Please check that the input is correct, or you can resolve this issue by filing an issue
Project URL: https://github.com/acheong08/ChatGPT

So, is it possible to retrieve the authentication token and update the config file automatically?

acheong08 commented 1 year ago

There used to be a way but it has since been broken by OpenAI. Requires PUID

hongyi-zhao commented 1 year ago

What's the meaning of PUID?

acheong08 commented 1 year ago

Plus user ID. Found in cookies

hongyi-zhao commented 1 year ago

If there are any scriptable or command line based methods to do this job? I've tried as follows but got nothing:

werner@X10DAi:~$ curl -fsSLx socks5h://127.0.0.1:18890 https://chat.openai.com/api/auth/session |grep -i accesstoken
curl: (22) The requested URL returned error: 403
prairiewolf11 commented 1 year ago

if it is possiable to use Python to simulate a browser and submit an account and password to "openai.com" through a proxy, and then use requests to obtain token from "https://chat.openai.com/api/auth/session"?

prairiewolf11 commented 1 year ago

import requests

target_url = 'https://chat.openai.com/api/auth/session'

session = requests.Session() session.proxies = {'https': 'socks5://192.168.0.101:20171','http': 'socks5://192.168.0.101:20171'}

login_data = { 'username': 'openai account', 'password': 'passwd' }

login_response = session.post('https://openai.com/login', data=login_data)

if login_response.status_code == 200: response = session.get(target_url)

print(response.text)

else: print('Login failed.')

I tried.But goes wrong.Don't konw why.

acheong08 commented 1 year ago

https://github.com/acheong08/OpenAIAuth

prairiewolf11 commented 1 year ago

Thank you very much.

acheong08 commented 1 year ago

We are currently already capable of this: in config:

{
  "email": "email",
  "password": "your password"
}

It will fetch token via that library

prairiewolf11 commented 1 year ago

Great. I found it in src.It will get new token automaticly when it's expired. Thanks again.

hongyi-zhao commented 1 year ago

I would like to know how to integrate the method mentioned above into other tools, such as gpt_academic, to bypass the OpenAI API expiration problem discussed here.

hongyi-zhao commented 1 year ago

@prairiewolf11

I found it in src.It will get new token automaticly when it's expired.

Where is the corresponding source code?

prairiewolf11 commented 1 year ago

\Lib\site-packages\revChatGPT\V1.py

hongyi-zhao commented 1 year ago

I mean, what are the exact source code lines in V1.py.

acheong08 commented 1 year ago

https://github.com/acheong08/ChatGPT/blob/a1c32f46c2409b5e880d0161c031f380b3957975/src/revChatGPT/V1.py#L364-L415

acheong08 commented 1 year ago

If error email & password is available and cached access token fails (which also checks for expiry)

https://github.com/acheong08/ChatGPT/blob/a1c32f46c2409b5e880d0161c031f380b3957975/src/revChatGPT/V1.py#L251-L261

acheong08 commented 1 year ago

Login is in check credentials:

https://github.com/acheong08/ChatGPT/blob/a1c32f46c2409b5e880d0161c031f380b3957975/src/revChatGPT/V1.py#L317-L339

hongyi-zhao commented 1 year ago

@acheong08

https://github.com/acheong08/ChatGPT/issues/1314#issuecomment-1615868449 https://github.com/acheong08/OpenAIAuth

https://github.com/acheong08/ChatGPT/issues/1314#issuecomment-1615871117 It will fetch token via that library

Thank you very much for pointing out the corresponding implementation. But I still cannot see the calling of OpenAIAuth package in these code snippets. Any more hints?

acheong08 commented 1 year ago

https://github.com/acheong08/ChatGPT/blob/a1c32f46c2409b5e880d0161c031f380b3957975/src/revChatGPT/V1.py#L455-L468

auth = Authenticator(
    email=self.config.get("email"),
    password=self.config.get("password"),
    proxy=self.config.get("proxy"),
)
self.set_access_token(auth.get_access_token())
hongyi-zhao commented 1 year ago

Got it. Another related line:

https://github.com/acheong08/ChatGPT/blob/a1c32f46c2409b5e880d0161c031f380b3957975/src/revChatGPT/V1.py#L36

So, all the above code snippets are corresponding to the usage mentioned here.