yamaken1343’s blog

技術ブログもどき

GrowiのAPIを叩きたい

研究じゃなくてこういうことだけ無限にやりたい

まえがき

研究室でGrowiというWikiを使ってます.
github.com 自動でSlackの特定のチャンネルの投稿内容をWikiアーカイブすることになったので, その調査をします.

一応ドキュメントは存在しますが, docs.growi.org 手が回っていないようなのでソースを見ます.

API捜索

おそらくここにあるものがすべてかと思われます.*1
https://github.com/weseek/growi/blob/master/src/server/routes/index.js API名からあたりをつけます.

今回はページの更新をしたいので
https://github.com/weseek/growi/blob/master/src/server/routes/page.js
を見れば良さそうです.

仕様調査

ページの更新がしたいので, 旧ページのデータをもってくる Getpage API とページの更新を行う UpdatePage API を調査します.

Getpage API

https://github.com/weseek/growi/blob/master/src/server/routes/page.js

  • method: GET
  • endpoint: /_api/pages.get
> @api {get} /pages.get Get page data
> @apiName GetPage
> @apiGroup Page
>
> @apiParam {String} page_id
> @apiParam {String} path
> @apiParam {String} revision_id

とりあえずpathを指定しておけば良さそう

認証済みのブラウザからなら

https://demo.growi.org/_api/pages.get?path=/

認証してないブラウザ等からはAPI KEYをパラメータに渡してやれば良さそうです.

https://demo.growi.org/_api/pages.get?path=/&access_token=[ユーザー設定から確認できるAPI KEY]

ただし, API KEYに"+"等のURLに使えない値が含まれる場合はエンコードが必須です.
pythonのrequestsは勝手にやってくれました.

認証済みのブラウザからならトークンなしでも素直に通るので, もっと良いやり方があるかもしれません.

ちなみに叩くと以下のようなJSONが返ってきます.

{
  "page": {
    "status": "published",
    "grant": 1,
    "grantedUsers": [],
    "liker": [],
    "seenUsers": [
      "592f8f117f616a0019f77e74",
      "592f853a7f616a0019f77e6e",
      "592f9dd47f616a0019f77e7c"
    ],
    "commentCount": 0,
    "createdAt": "2019-03-20T21:57:34.092Z",
    "updatedAt": "2019-05-09T07:42:46.324Z",
    "_id": "5c9403b8ef06c40058a243e8",
    "path": "/",
    "creator": {
      "isGravatarEnabled": false,
      "isEmailPublished": true,
      "lang": "ja",
      "status": 2,
      "admin": true,
      "_id": "592f8f117f616a0019f77e74",
      "email": "admin1@crowi-plus.org",
      "username": "admin1",
      "name": "Admin 1",
      "createdAt": "2017-06-01T03:50:41.524Z"
    },
    "lastUpdateUser": {
      "isGravatarEnabled": false,
      "isEmailPublished": true,
      "lang": "ja",
      "status": 2,
      "admin": false,
      "_id": "592f9dd47f616a0019f77e7c",
      "email": "guest1@crowi-plus.org",
      "username": "guest1",
      "name": "Guest 1",
      "createdAt": "2017-06-01T04:53:40.461Z",
      "image": null
    },
    "redirectTo": null,
    "grantedGroup": null,
    "__v": 58,
    "revision": {
      "format": "markdown",
      "_id": "5cd3d9f6ef06c40058a245f7",
      "createdAt": "2019-05-09T07:42:46.323Z",
      "path": "/",
      "body": "# Overview\n\n ..."
      "author": {
        "isGravatarEnabled": false,
        "isEmailPublished": true,
        "lang": "ja",
        "status": 2,
        "admin": false,
        "_id": "592f9dd47f616a0019f77e7c",
        "email": "guest1@crowi-plus.org",
        "username": "guest1",
        "name": "Guest 1",
        "createdAt": "2017-06-01T04:53:40.461Z",
        "image": null
      },
      "hasDiffToPrev": true,
      "__v": 0
    },
    "hasDraftOnHackmd": false,
    "pageIdOnHackmd": "lcFmaxSxRXel2uwnGIWgIQ",
    "revisionHackmdSynced": "5cd3d9f6ef06c40058a245f7",
    "id": "5c9403b8ef06c40058a243e8"
  },
  "ok": true
}

UpdatePage API

  • method: POST
  • endpoint: /_api/pages.update
> @api {post} /pages.update Update page
> @apiName UpdatePage
> @apiGroup Page
>
> @apiParam {String} body
> @apiParam {String} page_id
> @apiParam {String} revision_id
> @apiParam {String} grant
>
> In the case of the page exists:
> - If revision_id is specified => update the page,
> - If revision_id is not specified => force update by the new contents.

全部さっきの Getpage API で取得できます.
revison_idを指定しないと新しい内容で強制的に上書きするって書いてますが, ないと通りませんでした.
grantは1が全体公開っぽい. ないとnullが指定されます. 青枠が表示される以外は全体公開と同様?

ページの更新に失敗するとokカラムにfalseの入ったjsonが返ってきます.
成功すると先程のgetで取得可能なjsonが返ってきます.

次回は実際に使ってみます.

*1:node.jsわからない