browser_cookie3 icon indicating copy to clipboard operation
browser_cookie3 copied to clipboard

Script fails to run in MacOS crontab.

Open Shu-Ji opened this issue 1 year ago • 3 comments

Everything is OK, if I manual run in the iterm.

Chrome 119.0.6045.199 (arm64) MacOS Sonama 14.1.2

But when I add the script in crontab, it fails with the errors:

* * * * * /Users/xxx/.virtualenvs/scripts/bin/python -u /Users/xxx/.virtualenvs/scripts/scripts/cron/xxx.py >> /tmp/xxx.log 2>&1
Traceback (most recent call last):
  File "/Users/xxx/.virtualenvs/scripts/lib/python3.11/site-packages/browser_cookie3/__init__.py", line 580, in _decrypt
    decrypted = unpad(cipher.decrypt(
                ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/xxx/.virtualenvs/scripts/lib/python3.11/site-packages/Cryptodome/Util/Padding.py", line 92, in unpad
    raise ValueError("Padding is incorrect.")
ValueError: Padding is incorrect.
Traceback (most recent call last):
  File "/Users/xxx/.virtualenvs/scripts/scripts/cron/xxx.py", line 5, in <module>
    from browser_session import get, post
  File "/Users/xxx/.virtualenvs/scripts/scripts/cron/yyy.py", line 5, in <module>
    s.cookies = browser_cookie3.chrome()
                ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/xxx/.virtualenvs/scripts/lib/python3.11/site-packages/browser_cookie3/__init__.py", line 1161, in chrome
    return Chrome(cookie_file, domain_name, key_file).load()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/xxx/.virtualenvs/scripts/lib/python3.11/site-packages/browser_cookie3/__init__.py", line 515, in load
    value = self._decrypt(value, enc_value)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/xxx/.virtualenvs/scripts/lib/python3.11/site-packages/browser_cookie3/__init__.py", line 586, in _decrypt
    raise BrowserCookieError('Unable to get key for cookie decryption')
browser_cookie3.BrowserCookieError: Unable to get key for cookie decryption

If I change to Safary:

PermissionError: [Errno 1] Operation not permitted: '/Users/xxx/Library/Containers/com.apple.Safari/Data/Library/Cookies/Cookies.binarycookies'

Shu-Ji avatar Dec 05 '23 07:12 Shu-Ji

Same issue when I'm using Safari on macOS and it shows PermissionError. I can't even manually sudo cd to the cookie path .../Containers/com.apple.Safari/Data/Library/Cookies/Cookies.binarycookies.

iamyifan avatar Mar 23 '24 07:03 iamyifan

running into the same problem, have you found any solutions?

hl105 avatar Jul 19 '24 14:07 hl105

running into the same problem, have you found any solutions?

no,I switch to use nodejs/playwright now 💔

Shu-Ji avatar Jul 26 '24 00:07 Shu-Ji

I'm not familiar with MacOS but I assume that the script isn't running in the same environment as when you launch it manually. Perhaps it runs as a different user, different environment variables, or something? If the script works normally then I don't think this is a browser cookie issue

borisbabic avatar Dec 19 '24 23:12 borisbabic

I had this same issue. I solved giving cron disk access.

On newer macOS versions, cron may need permissions:

  1. Open System Preferences → Security & Privacy → Privacy.
  2. Under Full Disk Access, add /usr/sbin/cron.

konecnyna avatar Mar 03 '25 22:03 konecnyna