Discussion:
Pyzotero attachment functions fail on group library
Pavan Atri
2014-07-31 21:05:15 UTC
Permalink
Hello,

I am trying to create a HTML file attachment in my group library, using the
*attachment_simple* and *attachment_both* functions in the Pyzotero wrapper
for Zotero API. *It works fine on the user library*(my personal library)
but fails when I use it on a group library, giving a *UserNotAuthorised *response.
I have the Admin permission in the group library. Below is the code and the
error response:
The below code fetches HTML content from an XML file and creates a File
object out of it, which is the file attachment to be created
from pyzotero import zotero
import xml.etree.ElementTree as exml
import codecs



















*def addHTMLAtchmnt(itemId):#Code to locate XML file from where HTML
content is to be fetched item = zot.item(itemId) extra
=str(item[0][u'extra']) filename = extra[extra.rfind(".")+1:] +
"-atom.xml" doc = exml.parse(fileloc + filename) root =
doc.getroot() #FetchHTML content from the XML htmltext =
root.find('{http://www.w3.org/2005/Atom}content').text if htmltext !=
None: htmlfile =
codecs.open('htmlattchmnt.html','w',encoding='utf-8')
htmlfile.write(htmltext) htmlfile.close()
zot.attachment_simple(['htmlattchmnt.html'], itemId) print
'Attachment created'zot = zotero.Zotero(<groupId>, 'group',
<apiKey>)fileloc = "D:\\GitHub\\awol-backup\\"#Add HTML attachment to the
Parent record with ID: *
*2GMSK2MJaddHTMLAtchmnt('2GMSK2MJ')*

*Error Response:*
Traceback (most recent call last):
File
"C:\Users\Pavan\workspace2\ZoteroTest\atri\zotero\test\zot_html_attachment.py",
line 51, in <module>
addHTMLAtchmnt('9U9NBFEF')
File
"C:\Users\Pavan\workspace2\ZoteroTest\atri\zotero\test\zot_html_attachment.py",
line 25, in addHTMLAtchmnt
zot.attachment_simple(['htmlattchmnt.html'], itemId)
File "C:\Python27\lib\site-packages\pyzotero\zotero.py", line 1031, in
attachment_simple
return self._attachment(to_add, parentid)
File "C:\Python27\lib\site-packages\pyzotero\zotero.py", line 782, in
_attachment
created = create_prelim(payload)
File "C:\Python27\lib\site-packages\pyzotero\zotero.py", line 692, in
create_prelim
error_handler(req)
File "C:\Python27\lib\site-packages\pyzotero\zotero.py", line 1238, 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/<groupID>/items/9U9NBFEF/children?key=<
apiKey>
Method: POST
Response: Forbidden

In the Error response I see that the POST URL has* /users/* which I
actually think should be* /groups/* since am trying to create it in a group.
Please let me know if I've missed out on anything.

Thank you!

Regards,
Pavan
--
You received this message because you are subscribed to the Google Groups "zotero-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to zotero-dev+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To post to this group, send email to zotero-dev-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
Visit this group at http://groups.google.com/group/zotero-dev.
For more options, visit https://groups.google.com/d/optout.
Tom Elliott
2014-08-01 14:32:44 UTC
Permalink
Hi Pavan and other folks watching this thread:

I've just had a look at this (sorry I couldn't help sooner). From what I can see, "/users/" seems to be hardwired into the creation of the POST url for attachments in pyzotero:

https://github.com/urschrei/pyzotero/blob/master/pyzotero/zotero.py#L673

I think we'll need to open an issue there and see if we can contribute a code fix.

Tom

Tom Elliott, Ph.D.
Associate Director for Digital Programs and Senior Research Scholar
Institute for the Study of the Ancient World (NYU)
http://isaw.nyu.edu/people/staff/tom-elliott
Post by Pavan Atri
Hello,
The below code fetches HTML content from an XML file and creates a File object out of it, which is the file attachment to be created
from pyzotero import zotero
import xml.etree.ElementTree as exml
import codecs
#Code to locate XML file from where HTML content is to be fetched
item = zot.item(itemId)
extra =str(item[0][u'extra'])
filename = extra[extra.rfind(".")+1:] + "-atom.xml"
doc = exml.parse(fileloc + filename)
root = doc.getroot()
#FetchHTML content from the XML
htmltext = root.find('{http://www.w3.org/2005/Atom}content').text
htmlfile = codecs.open('htmlattchmnt.html','w',encoding='utf-8')
htmlfile.write(htmltext)
htmlfile.close()
zot.attachment_simple(['htmlattchmnt.html'], itemId)
print 'Attachment created'
zot = zotero.Zotero(<groupId>, 'group', <apiKey>)
fileloc = "D:\\GitHub\\awol-backup\\"
#Add HTML attachment to the Parent record with ID: 2GMSK2MJ
addHTMLAtchmnt('2GMSK2MJ')
File "C:\Users\Pavan\workspace2\ZoteroTest\atri\zotero\test\zot_html_attachment.py", line 51, in <module>
addHTMLAtchmnt('9U9NBFEF')
File "C:\Users\Pavan\workspace2\ZoteroTest\atri\zotero\test\zot_html_attachment.py", line 25, in addHTMLAtchmnt
zot.attachment_simple(['htmlattchmnt.html'], itemId)
File "C:\Python27\lib\site-packages\pyzotero\zotero.py", line 1031, in attachment_simple
return self._attachment(to_add, parentid)
File "C:\Python27\lib\site-packages\pyzotero\zotero.py", line 782, in _attachment
created = create_prelim(payload)
File "C:\Python27\lib\site-packages\pyzotero\zotero.py", line 692, in create_prelim
error_handler(req)
File "C:\Python27\lib\site-packages\pyzotero\zotero.py", line 1238, in error_handler
raise error_codes.get(req.status_code)(err_msg(req))
Code: 403
URL: https://api.zotero.org/users/<groupID>/items/9U9NBFEF/children?key=<apiKey>
Method: POST
Response: Forbidden
In the Error response I see that the POST URL has /users/ which I actually think should be /groups/ since am trying to create it in a group.
Please let me know if I've missed out on anything.
Thank you!
Regards,
Pavan
--
You received this message because you are subscribed to the Google Groups "zotero-dev" group.
Visit this group at http://groups.google.com/group/zotero-dev.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "zotero-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to zotero-dev+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To post to this group, send email to zotero-dev-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
Visit this group at http://groups.google.com/group/zotero-dev.
For more options, visit https://groups.google.com/d/optout.
Tom Elliott
2014-08-01 16:59:07 UTC
Permalink
All:

In case anyone else on the list is interested or stumbles across this thread later, I offer the following pointer. We have opened a ticket for this issue on the pyzotero tracker: https://github.com/urschrei/pyzotero/issues/33 . We'll conduct all further discussion there, so that should close out this thread.

Best,
Tom

Tom Elliott, Ph.D.
Associate Director for Digital Programs and Senior Research Scholar
Institute for the Study of the Ancient World (NYU)
http://isaw.nyu.edu/people/staff/tom-elliott
Post by Tom Elliott
https://github.com/urschrei/pyzotero/blob/master/pyzotero/zotero.py#L673
I think we'll need to open an issue there and see if we can contribute a code fix.
Tom
Tom Elliott, Ph.D.
Associate Director for Digital Programs and Senior Research Scholar
Institute for the Study of the Ancient World (NYU)
http://isaw.nyu.edu/people/staff/tom-elliott
Post by Pavan Atri
Hello,
The below code fetches HTML content from an XML file and creates a File object out of it, which is the file attachment to be created
from pyzotero import zotero
import xml.etree.ElementTree as exml
import codecs
#Code to locate XML file from where HTML content is to be fetched
item = zot.item(itemId)
extra =str(item[0][u'extra'])
filename = extra[extra.rfind(".")+1:] + "-atom.xml"
doc = exml.parse(fileloc + filename)
root = doc.getroot()
#FetchHTML content from the XML
htmltext = root.find('{http://www.w3.org/2005/Atom}content').text
htmlfile = codecs.open('htmlattchmnt.html','w',encoding='utf-8')
htmlfile.write(htmltext)
htmlfile.close()
zot.attachment_simple(['htmlattchmnt.html'], itemId)
print 'Attachment created'
zot = zotero.Zotero(<groupId>, 'group', <apiKey>)
fileloc = "D:\\GitHub\\awol-backup\\"
#Add HTML attachment to the Parent record with ID: 2GMSK2MJ
addHTMLAtchmnt('2GMSK2MJ')
File "C:\Users\Pavan\workspace2\ZoteroTest\atri\zotero\test\zot_html_attachment.py", line 51, in <module>
addHTMLAtchmnt('9U9NBFEF')
File "C:\Users\Pavan\workspace2\ZoteroTest\atri\zotero\test\zot_html_attachment.py", line 25, in addHTMLAtchmnt
zot.attachment_simple(['htmlattchmnt.html'], itemId)
File "C:\Python27\lib\site-packages\pyzotero\zotero.py", line 1031, in attachment_simple
return self._attachment(to_add, parentid)
File "C:\Python27\lib\site-packages\pyzotero\zotero.py", line 782, in _attachment
created = create_prelim(payload)
File "C:\Python27\lib\site-packages\pyzotero\zotero.py", line 692, in create_prelim
error_handler(req)
File "C:\Python27\lib\site-packages\pyzotero\zotero.py", line 1238, in error_handler
raise error_codes.get(req.status_code)(err_msg(req))
Code: 403
URL: https://api.zotero.org/users/<groupID>/items/9U9NBFEF/children?key=<apiKey>
Method: POST
Response: Forbidden
In the Error response I see that the POST URL has /users/ which I actually think should be /groups/ since am trying to create it in a group.
Please let me know if I've missed out on anything.
Thank you!
Regards,
Pavan
--
You received this message because you are subscribed to the Google Groups "zotero-dev" group.
Visit this group at http://groups.google.com/group/zotero-dev.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "zotero-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to zotero-dev+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To post to this group, send email to zotero-dev-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
Visit this group at http://groups.google.com/group/zotero-dev.
For more options, visit https://groups.google.com/d/optout.
Loading...