Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
iamyifan opened this issue Mar 23, 2024 · 4 comments
Closed

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

iamyifan opened this issue Mar 23, 2024 · 4 comments
Labels
enhancement New feature or request

Comments

@iamyifan
Copy link

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 iamyifan changed the title PermissionError when initialising a client with browser-cookie3 PermissionError when initialising a client with browser-cookie3 Mar 23, 2024
@iamyifan iamyifan changed the title PermissionError when initialising a client with browser-cookie3 "PermissionError" when Initialising a Client w/ "browser-cookie3" Mar 23, 2024
@iamyifan
Copy link
Author

iamyifan commented Mar 23, 2024

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
Copy link
Author

iamyifan commented Mar 23, 2024

❌ 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
Copy link
Owner

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 HanaokaYuzu added the enhancement New feature or request label Mar 23, 2024
@HanaokaYuzu
Copy link
Owner

HanaokaYuzu commented Mar 23, 2024

(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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants