Atlassian API を利用して、 Confluence にページを作成する

<!-----

Yay, no errors, warnings, or alerts!

Conversion time: 0.49 seconds.


Using this Markdown file:

1. Paste this output into your source file.
2. See the notes and action items below regarding this conversion run.
3. Check the rendered output (headings, lists, code blocks, tables) for proper
   formatting and use a linkchecker before you publish this page.

Conversion notes:

* Docs to Markdown version 1.0β33
* Thu Oct 13 2022 00:58:57 GMT-0700 (PDT)
* Source doc: 2022-10-11 Atlassian API を利用して、 Confluence にページを作成する
* Tables are currently converted to HTML tables.
----->


## 方法


### API トークンの作成

[Manage API tokens for your Atlassian account | Atlassian Support](https://support.atlassian.com/atlassian-account/docs/manage-api-tokens-for-your-atlassian-account/)


### API を利用したページの作成

[The Confluence Cloud REST API (atlassian.com)](https://developer.atlassian.com/cloud/confluence/rest/v1/api-group-content/#api-wiki-rest-api-content-post) の例を利用してページを作成することができた。

この例はパラメタが多いので、独自のクラスを作成。


```
import json

import requests
from requests.auth import HTTPBasicAuth


class Confluence:
   def __init__(self, user_name, api_key, url):
       self.user_name = user_name
       self.api_key = api_key
       self.url = f'https://{url}'

   def create_page(self, title, content, space, parent_page_id):
       url = f"https://{self.url}/wiki/rest/api/content"
       payload = json.dumps({
           "title": title,
           "type": "page",
           "status": "current",
           "space": {
               "key": space
           },
           "ancestors": [
               {
                   "id": parent_page_id
               }
           ],
           "body": {
               "storage": {
                   "value": content,
                   "representation": "storage"
               },
           }
       })
       return self.post(payload, url)

   def post(self, payload, url):
       auth = HTTPBasicAuth(self.user_name, self.api_key)
       headers = {
           "Accept": "application/json",
           "Content-Type": "application/json"
       }
       return requests.request(
           "POST",
           url,
           data=payload,
           headers=headers,
           auth=auth
       )

   def get_page_by(self, page_id):
       url = f"{self.url}/wiki/rest/api/content/{page_id}?expand=body.storage"
       auth = HTTPBasicAuth(self.user_name, self.api_key)
       headers = {
           "Accept": "application/json",
           "Content-Type": "application/json"
       }
       return requests.request(
           "GET",
           url,
           headers=headers,
           auth=auth
       )

   def get_page_content_by(self, page_id):
       response = self.get_page_by(page_id)
       return json.loads(response.text)['body']['storage']['value']
```


下記のように実行することで、ページをとりあえず作成することができた。


```
confluence = confluence.Confluence(
   '<user name>',
   '<api key>',
   '<url>'
)
response = confluence.create_page('ページのタイトル', 'コンテンツ', 'スペース名', <親ページのID>)
```



## Ref

[How to create new page in Confluence using their REST API? - Stack Overflow](https://stackoverflow.com/questions/23523705/how-to-create-new-page-in-confluence-using-their-rest-api)

[Solved: how to get full content body (html) using confluen... (atlassian.com)](https://community.atlassian.com/t5/Confluence-questions/how-to-get-full-content-body-html-using-confluence-REST-API/qaq-p/710532)

なんかうまく使えなかった。

[Welcome to Confluence’s documentation! — confluence 0.1 documentation (pythonhosted.org)](https://pythonhosted.org/confluence/)

Forge App。これはこれで利用できたが、今回の目的には適さなかった。

[Getting started (atlassian.com)](https://developer.atlassian.com/platform/forge/getting-started/)