HanaokaYuzu / Gemini-API

✨ Elegant async Python API for Google Gemini web app
https://pypi.org/project/gemini-webapi/
GNU Affero General Public License v3.0
156 stars 21 forks source link

`gemini_webapi.GeminiClient()` not able to retrieve cookies #33

Closed a-sajjad72 closed 1 day ago

a-sajjad72 commented 3 weeks ago

I am experiencing issues while initiating a connection to Gemini using GeminiClient(). I had tried it on two machines with two different browsers.

  1. Ubuntu 24.04 with Mozilla Firefox
  2. Windows 10 22H2 with Edge

It gives error of ValueError: Failed to load cookies from local browser. Please pass cookie values manually. By passing the values manually it works like a charm.

I am not sure whether I post this issue here or on browser-cookie3 repo. I will give you more details if you need any information to resolve this issue. Thank You 😃

Warm Regards, Sajjad Ali

Traceback

2024-08-18 13:11:50.070 | WARNING  | gemini_webapi.utils.load_browser_cookies:load_browser_cookies:44 - Permission denied while trying to load cookies from edge. [Errno 13] Permission denied: 'C:\\Users\\asajj\\AppData\\Roaming\\..\\Local\\Microsoft\\Edge\\User Data\\Default\\Network\\Cookies'Traceback (most recent call last):
  File "C:\Users\asajj\OneDrive\Desktop\Sarmad_Tasks\testing.py", line 22, in <module>
    asyncio.run(main())
  File "C:\Users\asajj\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\asajj\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\asajj\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 654, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:\Users\asajj\OneDrive\Desktop\Sarmad_Tasks\testing.py", line 15, in main
    client = GeminiClient()
             ^^^^^^^^^^^^^^
  File "C:\Users\asajj\OneDrive\Desktop\Sarmad_Tasks\venv\Lib\site-packages\gemini_webapi\client.py", line 131, in __init__
    raise ValueError(
ValueError: Failed to load cookies from local browser. Please pass cookie values manually.
a-sajjad72 commented 3 weeks ago

My bad, I oversighted the Warning which shows PermissionError. This is due to the LockProfileCookieDatabase feature in Chromium based browsers. The work around is to close the browser then run it will work fine. But need to close the running browser everytime when the program should be run. What would I suggest to save the cookies values in a file so that it can be used for the next time without retrieving them from the browser.

EDIT: I tired it but this issue occurs gemini_webapi.exceptions.AuthError: Failed to initialize client. SECURE_1PSIDTS could get expired frequently, please make sure cookie values are up to date. The below-mentioned is only the automated way I guess.

@HanaokaYuzu What do you think about the shadowcopy as discussed here? There is also PR for that but not pushed yet.

HanaokaYuzu commented 2 weeks ago

Actually the current mechanism does exactly what you suggested: when client is successfully initialized, the valid cookie value will be stored to a local file and keeps updating in background when client is running. Later every time the client tries to initialize, the stored cookie will be used even if browser_cookie3 raises an error.

If your client initialized successfully but browser_cookie3 raised an error, it shouldn't affect the client's functionality and can be ignored (or even uninstalled)

a-sajjad72 commented 1 week ago

Actually the current mechanism does exactly what you suggested: when client is successfully initialized, the valid cookie value will be stored to a local file and keeps updating in background when client is running. Later every time the client tries to initialize, the stored cookie will be used even if browser_cookie3 raises an error.

If your client initialized successfully but browser_cookie3 raised an error, it shouldn't affect the client's functionality and can be ignored (or even uninstalled)

but the client not inialize when the chromium browsers are running. it failed to get the cookie files. the client only successfully initialized when the browser is closed successfully. This behaviour is happened when gemini_webapi.GeminiClient() used instead of providing cookie values manually.

HanaokaYuzu commented 2 days ago

Implemented an enhancement in v1.6.1. Now after a successful initialization, GeminiClient() should automatically use cached cookies regardless of whether cookie values are passed manually. Note that you may still need to quit browser for the first time.