首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用python请求更新网页,返回错误响应代码400

无法使用python请求更新网页,返回错误响应代码400
EN

Stack Overflow用户
提问于 2021-04-24 08:49:05
回答 1查看 168关注 0票数 0

我正在尝试通过Python requests模块更新Atlassian confluence页面中已经存在的页面。我使用requests.put()方法发送HTTP请求来更新我的页面。该页面已具有标题"Update Status“。我正在尝试输入一行作为页面的内容。页面id和JSON有效负载中的其他信息是我直接从rest/API/content中复制的……我正在尝试访问的网页的输出。注意:我已经可以通过python requests.get访问网页上的信息,但我不能将信息发布到网页上。

用于从网页访问信息的方法,其工作方式为:

代码语言:javascript
复制
response = requests.get('https://confluence.ai.com/rest/api/content/525424594?expand=body.storage',
                        auth=HTTPBasicAuth('svc-Automation@ai.com', 'AIengineering1@ai')).json()

下面是我用来更新返回错误代码400的网页的代码。

代码语言:javascript
复制
import requests
from requests.auth import HTTPBasicAuth
import json

url = "https://confluence.ai.com/rest/api/content/525424594"
payload =  {"id":"525424594","type":"page", "title":"new page-Update Status","space":{"key":"TST"},"body":{"storage":{"value": "<p>This is the updated text for the new page</p>","representation":"storage"}}, "version":{"number":2}}

result = requests.put(url, json=payload, auth=HTTPBasicAuth('svc-Automation@ai.com', 'AIengineering1@ai'))
print (result.content)

result.content输出以下状态:

代码语言:javascript
复制
{"statusCode":400, "data":{"authorized":true, "valid":false, "allowedInReadOnlyMode":true, "errors can't change an existing page's space.", "args":[]}}], "successful":false}, "message":"Could not update content of type : class com.atlassian.confluence.pages.Page with id 525424594","reason": "Bad Request"} 

当我转到链接https://confluence.ai.com/rest/api/content/525424594?expand=body.storage时,页面的rest api内容如下:

代码语言:javascript
复制
{"id":"525424594","type":"page","status":"current","title":"Update Status","space":{"id":152698959,"key":"AIENG","name":"Consumer Solutions (AI) ENG","type":"global","_links":{"webui":"/display/CSSFW","self":"https://confluence.ai.com/rest/api/space/AIENG"},"_expandable":{"metadata":"","icon":"","description":"","homepage":"/rest/api/content/155503904"}},"history":{"latest":true,"createdBy":{"type":"known","username":"Ashish.Joshi@ai.com","userKey":"8a78e5ac71e441c5017250468bbc0259","profilePicture":{"path":"/images/icons/profilepics/default.svg","width":48,"height":48,"isDefault":true},"displayName":"Ashish.Joshi@ai.com","_links":{"self":"https://confluence.ai.com/rest/api/user?key=8a78e5ac71e441c5017250468bbc0259"},"_expandable":{"status":""}},"createdDate":"2021-04-18T00:14:32.732Z","_links":{"self":"https://confluence.ai.com/rest/api/content/525424594/history"},"_expandable":{"lastUpdated":"","previousVersion":"","contributors":"","nextVersion":""}},"version":{"by":{"type":"known","username":"Ashish.Joshi@ai.com","userKey":"8a78e5ac71e441c5017250468bbc0259","profilePicture":{"path":"/images/icons/profilepics/default.svg","width":48,"height":48,"isDefault":true},"displayName":"Ashish.Joshi@ai.com","_links":{"self":"https://confluence.ai.com/rest/api/user?key=8a78e5ac71e441c5017250468bbc0259"},"_expandable":{"status":""}},"when":"2021-04-18T00:16:18.762Z","message":"","number":1,"minorEdit":false,"hidden":false,"_links":{"self":"https://confluence.wdc.com/rest/experimental/content/525424594/version/1"},"_expandable":{"content":"/rest/api/content/525424594"}},"extensions":{"position":"none"},"_links":{"webui":"/display/CSSFW/Update+Status","edit":"/pages/resumedraft.action?draftId=525424594&draftShareId=0984750c-3696-4918-b9ce-4358fefc26c9","tinyui":"/x/0ldRHw","collection":"/rest/api/content","base":"https://confluence.ai.com","context":"","self":"https://confluence.ai.com/rest/api/content/525424594"},"_expandable":{"container":"/rest/api/space/CSSFW","metadata":"","operations":"","children":"/rest/api/content/525424594/child","restrictions":"/rest/api/content/525424594/restriction/byOperation","ancestors":"","body":"","descendants":"/rest/api/content/525424594/descendant"}}

上面是当这个页面没有更新时,我在chrome上得到的输出。但是我想更新它的内容,也就是它的存储。我猜我现在使用的有效载荷的格式不正确。有什么建议吗?注意:这里显示的链接、用户名和密码都是虚构的。

EN

回答 1

Stack Overflow用户

发布于 2021-08-24 08:49:34

如果你仍然坚持这一点,仅仅基于你的错误信息:

"errors can't change an existing page's space."

我会仔细检查你所识别的空间。如果您只是直接从Confluence示例中复制它,那么它具有"key":"TST",但是您的get请求提供了"key":"AIENG"。当我尝试同样的方法时,我也遇到了类似的问题,但同样地,我不知道您是否在这里的示例中更改了它,只是为了显示。

注意:auth也可以在没有HTTPBasicAuth的情况下工作(但我对编码也很陌生,所以这可能是一个很好的实践!),我发现data=payload更适合我。

我也赞同atlassian-python-api上的建议,因为它确实让生活变得更容易。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67238370

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档