terry3041 / pyChatGPT

An unofficial Python wrapper for OpenAI's ChatGPT API
GNU General Public License v3.0
1.35k stars 255 forks source link

407 Proxy Authorization Required #103

Open levmarkov opened 1 year ago

levmarkov commented 1 year ago

Without proxy works fine locally, I want to run on server. To do this, I connect to a proxy on the local computer (I tried https and socks5) - everything works. When I run the script locally, an error appears. Although I have already passed the login and password in the string.

Maybe they need to be transferred somehow separately?

from pyChatGPT import ChatGPT

session_token = 'Mytoken'  # `__Secure-next-auth.session-token` cookie from https://chat.openai.com/chat

api = ChatGPT(session_token,proxy='https://user:pass@ip:port', verbose=True, conversation_id='efa27bc1-fe3a-49bb-a504-a96560af6385')  # verbose mode (print debug messages)

resp = api.send_message('Hi, how are you?')
print(resp['message'])

api.reset_conversation()  # reset the conversation
api.clear_conversations()  # clear all conversations
api.refresh_chat_page()  # refresh the chat page

I also tried to fix the pyChatGPT.py file by replacing:

self.driver = uc.Chrome(options=options)

on this

sw_options = {
     proxy: {
         'http': "user:password@host:port",
         'https': "user:password@host:port",
         'no_proxy': 'localhost,127.0.0.1'
     },
self.driver = uc.Chrome(options=options)

Same result. What could be the problem?

levmarkov commented 1 year ago
[__init_browser] Initializing browser...
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/urllib/request.py", line 1348, in do_open
    h.request(req.get_method(), req.selector, req.data, headers,
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/http/client.py", line 1276, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/http/client.py", line 1322, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/http/client.py", line 1271, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/http/client.py", line 1031, in _send_output
    self.send(msg)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/http/client.py", line 969, in send
    self.connect()
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/http/client.py", line 1441, in connect
    super().connect()
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/http/client.py", line 945, in connect
    self._tunnel()
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/http/client.py", line 923, in _tunnel
    raise OSError(f"Tunnel connection failed: {code} {message.strip()}")
OSError: Tunnel connection failed: 407 Proxy Authorization Required

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/nomax/Desktop/pyChatGPT/test.py", line 9, in <module>
    api = ChatGPT(session_token,proxy='https://user:pass@ip:port', verbose=True, conversation_id='efa27bc1-fe3a-49bb-a504-a96560af6385')  # verbose mode (print debug messages)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pyChatGPT/pyChatGPT.py", line 125, in __init__
    self.__init_browser()
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pyChatGPT/pyChatGPT.py", line 182, in __init_browser
    self.driver = uc.Chrome(options=options)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/undetected_chromedriver/__init__.py", line 246, in __init__
    patcher.auto()
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/undetected_chromedriver/patcher.py", line 130, in auto
    release = self.fetch_release_number()
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/undetected_chromedriver/patcher.py", line 151, in fetch_release_number
    return LooseVersion(urlopen(self.url_repo + path).read().decode())
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/urllib/request.py", line 216, in urlopen
    return opener.open(url, data, timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/urllib/request.py", line 519, in open
    response = self._open(req, data)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/urllib/request.py", line 536, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/urllib/request.py", line 496, in _call_chain
    result = func(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/urllib/request.py", line 1391, in https_open
    return self.do_open(http.client.HTTPSConnection, req,
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/urllib/request.py", line 1351, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error Tunnel connection failed: 407 Proxy Authorization Required>
levmarkov commented 1 year ago

Proxy support (only without basic auth)

Understood it. But when I used proxy without username and password the same error

terry3041 commented 1 year ago

That should be the problem of your proxy. I cannot reproduce this issue.