urschrei / pyzotero

Pyzotero: a Python client for the Zotero API
https://pyzotero.readthedocs.org
Other
908 stars 99 forks source link

report requests.exceptions.HTTPError: 403, when use attachment_simple() function #152

Closed MoPinghui closed 2 years ago

MoPinghui commented 2 years ago

When I used the pyzotero to add the attached file (*.pdf) into a item, the program broke and threw an exception (403). Can you help me to fixed the error? Thank you.

Python code

from pyzotero import zotero

library_id = '9315227'
library_type = 'user' 
api_key = 'W1QpR9h0XaWUcQwpONMZV1Oi'

# get zotero 
zot = zotero.Zotero(library_id, library_type, api_key)
inf = zot.key_info() 
print(inf)

# attach file to item
itemID = 'EYJL7GUM'
zot.attachment_simple(['D:/Download/test1.pdf'], itemID )

output

{'key': 'W1QpR9h0XaWUcQwpONMZV1Oi', 'userID': 9315227, 'username': 'dead_fox', 'displayName': '', 'access': {'user': {'library': True, 'files': True}}}
Traceback (most recent call last):
  File "D:\InstallPackage\Applications\Python37\lib\site-packages\pyzotero\zotero.py", line 1883, in _create_prelim
    req.raise_for_status()
  File "C:\Users\mz\AppData\Roaming\Python\Python37\site-packages\requests\models.py", line 940, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://api.zotero.org/users/9315227/items

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File ".\run.py", line 35, in <module>
    zot.attachment_simple(['D:/Download/test1.pdf'], 'EYJL7GUM')
  File "D:\InstallPackage\Applications\Python37\lib\site-packages\pyzotero\zotero.py", line 1343, in attachment_simple
    return self._attachment(to_add, parentid)
  File "D:\InstallPackage\Applications\Python37\lib\site-packages\pyzotero\zotero.py", line 981, in _attachment
    res = attachment.upload()
  File "D:\InstallPackage\Applications\Python37\lib\site-packages\pyzotero\zotero.py", line 2016, in upload
    self._create_prelim()
  File "D:\InstallPackage\Applications\Python37\lib\site-packages\pyzotero\zotero.py", line 1885, in _create_prelim
    error_handler(self.zinstance, req)
  File "D:\InstallPackage\Applications\Python37\lib\site-packages\pyzotero\zotero.py", line 1633, in error_handler
    raise error_codes.get(req.status_code)(err_msg(req))
pyzotero.zotero_errors.UserNotAuthorised:
Code: 403
URL: https://api.zotero.org/users/9315227/items
Method: POST
Response: Write access denied
urschrei commented 2 years ago

This isn't a Pyzotero error: it seems that you don't have write access to the Zotero library you want to add the PDF to. Please ask how to fix it on the Zotero forums!