Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with conf.get_page_id - AttributeError: 'str' object has no attribute 'get' - Python

Jozef Hanek June 1, 2023

Hello,

 

please forgive me if this is a badly formulated post or a stupid question. I am fairly new to creating/debugging codes.

 

I have a problem when I try to update the existing Confluence page via Python code.

The code is really simple, just to test the rivers :

from atlassian import Confluence

conf_site = 'https://confluence.x-x-x.com'
conf_user = 'x-x-x'
conf_pass = 'x-x-x'

page_title = 'x-x-x'
page_space = 'x-x-x'

# connect to Confluence
conf = Confluence(url=conf_site, username=conf_user, password=conf_pass)

# resolve page ID
page_id = conf.get_page_id(page_space, page_title)

# new page content
page_content = '<p>This is the new updated text of the page</p>'

# update page with new content
conf.update_page(page_id, page_title, page_content)


The problem is, this plain and simple code gives me an error:

Traceback (most recent call last):
File "C:/Users/x-x-x/test2 dpc.py", line 14, in <module>
page_id = conf.get_page_id(page_space, page_title)
File "C:\Program Files (x86)\Python310-32\lib\site-packages\atlassian\confluence.py", line 219, in get_page_id
return (self.get_page_by_title(space, title, type=type) or {}).get("id")
File "C:\Program Files (x86)\Python310-32\lib\site-packages\atlassian\confluence.py", line 314, in get_page_by_title
return response.get("results")[0]
AttributeError: 'str' object has no attribute 'get'

Therefore, I suspect the problem is with conf.get_page_id(page_space, page_title). I am quite certain page_title = 'x-x-x' and page_space = 'x-x-x' are correct, so is the https://confluence.x-x-x.com

When I tried to print what is returned by conf.get_page_id, it returns 2500 lines long HTML. Should it be this way? What am I doing wrong?

2 answers

0 votes
Jozef Hanek June 2, 2023

So, I just tried to use Requests instead of Atlassian library and the response output is a google gateway that asks me to log in. 

 

Therefore, I think the problem might be in the authentication method?  I have also tried to authenticate via ApiToken and username, but does not seem to have any effect.

0 votes
Graham Twine
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 1, 2023

Hello @Jozef Hanek ,

 

conf.get_page_id(space, title) should only return a number(the ID of the page) You can find this ID in the URL when you edit the page in a browser.

I am not familiar with the python API for Confluence but I think your analysis is correct if the html is coming from Confluence and not some gate keeper between yourself and Confluence. You can confirm this by analyzing the html returned.

 

Two things you can try.

1. Get the ID and hard code it for your first test.

2. Are you using the Space Name or the Space key? Use the space key

 

Check if you get a boolean from this.

As you get the content of the page already then this should return true

confluence.page_exists(space, title, type=None)
Jozef Hanek June 2, 2023

Hello and thank you for the answer. I tried to use the space key, but the result is the same.

Honestly, now I am not even so sure about the HTML it returned. 

Even when I try really really simple code, like :

# connect to Confluence
conf = Confluence(url=conf_site, username=conf_user, password=conf_pass)

# Resolve page ID
page_id = 617158380

# Get page space
space_key = conf.get_page_space(page_id)

print(space_key)

it ends up with:

Traceback (most recent call last):
File "C:/Users/name/plswork.py", line 17, in <module>
space_key = conf.get_page_space(page_id)
File "C:\Program Files (x86)\Python310-32\lib\site-packages\atlassian\confluence.py", line 257, in get_page_space
return ((self.get_page_by_id(page_id, expand="space") or {}).get("space") or {}).get("key") or None
AttributeError: 'str' object has no attribute 'get'

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events