HanaokaYuzu / Gemini-API

✨ An elegant async Python wrapper for Google Gemini web app
https://pypi.org/project/gemini-webapi/
GNU Affero General Public License v3.0
134 stars 19 forks source link

"PermissionError" when Initialising a Client w/ "browser-cookie3" #10

Closed iamyifan closed 2 months ago

iamyifan commented 3 months ago

Hey developer,

When I am using browser-cookie3 to initialise a client:

client = GeminiClient()
await client.init(timeout=30, auto_close=False, close_delay=300, auto_refresh=False)

It shows up the following error: PermissionError: [Errno 1] Operation not permitted: '/Users/<my_user_name>/Library/Containers/com.apple.Safari/Data/Library/Cookies/Cookies.binarycookies'

It seems like an error related to Safari. My current OS is macOS 14.3.1 23D60 arm64, and I'm pretty sure my default browser is not Safari.

(When I manually use __Secure-1PSID and __Secure-1PSIDTS, it works fine but the IPSIDTS still expires very quickly. It can last 5 mins when I'm using an incognito tab on Chrome.)

Is there any way to customise browser options from `browser-cookie3? Or is there any way to grant permissions to avoid the error?

Thanks for your awsome work : )

Yifan

iamyifan commented 3 months ago

The full log:

Traceback (most recent call last):
  File "/Users/<my_user_name>/<path_to_file>/ask_gemini.py", line 23, in <module>
    asyncio.run(main())
  File "/Users/<my_user_name>/anaconda3/envs/<conda_env_name>/lib/python3.12/asyncio/runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/Users/<my_user_name>/anaconda3/envs/<conda_env_name>/lib/python3.12/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/<my_user_name>/anaconda3/envs/<conda_env_name>/lib/python3.12/asyncio/base_events.py", line 685, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/Users/<my_user_name>/<path_to_file>/ask_gemini.py", line 11, in main
    client = GeminiClient()
             ^^^^^^^^^^^^^^
  File "/Users/<my_user_name>/anaconda3/envs/<conda_env_name>/lib/python3.12/site-packages/gemini_webapi/client.py", line 129, in __init__
    cookies = browser_cookie3.load(domain_name="google.com")
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/<my_user_name>/anaconda3/envs/<conda_env_name>/lib/python3.12/site-packages/browser_cookie3/__init__.py", line 1233, in load
    for cookie in cookie_fn(domain_name=domain_name):
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/<my_user_name>/anaconda3/envs/<conda_env_name>/lib/python3.12/site-packages/browser_cookie3/__init__.py", line 1223, in safari
    return Safari(cookie_file, domain_name).load()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/<my_user_name>/anaconda3/envs/<conda_env_name>/lib/python3.12/site-packages/browser_cookie3/__init__.py", line 1040, in __init__
    self.__open_file(cookie_file)
  File "/Users/<my_user_name>/anaconda3/envs/<conda_env_name>/lib/python3.12/site-packages/browser_cookie3/__init__.py", line 1051, in __open_file
    self.__buffer = open(cookie_file, 'rb')
                    ^^^^^^^^^^^^^^^^^^^^^^^
PermissionError: [Errno 1] Operation not permitted: '/Users/<my_user_name>/Library/Containers/com.apple.Safari/Data/Library/Cookies/Cookies.binarycookies'
iamyifan commented 3 months ago

❌ Update:

Tried sudo cd to change permission but don't even have permission to .../Cookies/.

(Cookie's path: /Users/<my_user_name>/Library/Containers/com.apple.Safari/Data/Library/Cookies/Cookies.binarycookies)

✅ Update 2:

Tried modifying the following function from browser-cookie3/__init__.py and it works now : )

# __init__.py in browser-cookie3

def load(domain_name=""):
    """Try to load cookies from all supported browsers and return combined cookiejar
    Optionally pass in a domain name to only load cookies from the specified domain
    """
    cj = http.cookiejar.CookieJar()
    for cookie_fn in [chrome, chromium, opera, opera_gx, brave, edge, vivaldi, firefox, librewolf, safari]:
        try:
            if cookie_fn == safari:  # ignore safari
                continue
            for cookie in cookie_fn(domain_name=domain_name):
                cj.set_cookie(cookie)
        except BrowserCookieError:
            pass
    return cj
HanaokaYuzu commented 3 months ago

On macOS, grant your IDE with full disk access permission in "System Settings" - "Privacy & Security" can fix this error. I'll consider adding an option to choose browser type in the future update.

image
HanaokaYuzu commented 3 months ago

(When I manually use Secure-1PSID and Secure-1PSIDTS, it works fine but the IPSIDTS still expires very quickly. It can last 5 mins when I'm using an incognito tab on Chrome.)

Did this happen while initializing client with auto_refresh=True? If yes, can you try the following code and show me the log? (Please uninstall browser-cookie3 during this testing)

from gemini_webapi import GeminiClient, set_log_level

set_log_level("DEBUG")

client = GeminiClient(Secure_1PSID, Secure_1PSIDTS)
await client.init(auto_refresh=True)