Open3

notion APIを使ってみる

yjiro0403yjiro0403

動作確認はapidogを使用
https://apidog.com/jp/blog/how-to-work-with-notion-api/

Databaseの操作

https://developers.notion.com/docs/working-with-databases

DBの更新

{
    "properties": {
        "Wine Pairing": {
            "rich_text": {}
        }
    },
    "title": [
        {
            "text": {
                "content": "修正後のDB名"
            }
        }
    ]
}

DBにアイテムを追加する

ページを作成するAPIを使うらしい

  • 事前にDBにカラムを追加しておく
{
    "parent": {"database_id": "e295f8f267d6475890e89a7d4634363e"},
    "properties": {
      "Name": {
        "title": [
          {
            "text": {
              "content": "新しいレコード"
            }
          }
        ]
      },
      "担当者":{
        "rich_text": [
           {
               "text": {
               "content": "山口央士朗"
               }
           }
          ]
      },
      "Status":{
        "multi_select":[
            {
              "name": "対応中"
            }
        ]
      }
    },
    "children": [
      {
        "object": "block",
        "type": "paragraph",
        "paragraph": {
          "rich_text": [
            {
              "text": {
                "content": "これはテストです"
              }
            }
          ]
        }
      }
    ]
}

https://www.6666666.jp/productivity/20210617/
https://qiita.com/nyoroko/items/452a8db449862f31ab2b?utm_campaign=post_article&utm_medium=twitter&utm_source=twitter_share

プロパティなど
https://zenn.dev/team_zenn/articles/117424abb5605b

エラー

Could not find database with ID: [データベースのID]. Make sure the relevant pages and databases are shared with your integration

share->connectionからインテグレーションを行なっていなかったために発現。
しばらくconnectionから選択できなかったので時間がかかるかも。

https://waken.hatenablog.com/entry/2022/10/21/102449

参考

https://chigusa-web.com/blog/notion-api/

yjiro0403yjiro0403

gasを使用してnotion APIの中身を変更する

  • 機密情報を扱う時のためにtokenなどの扱いも考えておく

手順の流れ

スクリプトプロパティに下記データの対応

  • NOTION_KEY
    • notionのシークレットキーを対応
  • NOTION_DB_ID
    • notionのDBのID
  • GAS_KEY
    • 外部からAPIアクセスするのに必要なキー情報
    • 任意の文字列を保存

gas keyのtokenが一定時間後に切り替わってしまう

https://developers.google.com/apps-script/reference/script/script-app?hl=ja#getoauthtoken
より

トークンは一定時間(最低でも数分)後に期限切れになります。

これだと外部のアプリケーションから認証付きで呼び出せない
そこでchatworkの記事を参考にquerystringに認証情報を追加する

https://creators-note.chatwork.com/entry/2017/12/20/163128

またはVerification Tokenを使用する
https://qiita.com/seratch/items/2158cb0abed5b8e12809

参考

https://www.ka-net.org/blog/?p=12258

https://somachob.com/gas-properties-service/
https://qiita.com/massa-potato/items/2209ff367d65c5dd6181

https://www.aura-office.co.jp/blog/notion-gas/

https://zenn.dev/5t111111/articles/785fec8d21d82b

https://qiita.com/riversun/items/c924cfe70e16ee3fe3ba

yjiro0403yjiro0403

エラー

gasのAuthorizationで401エラーが発生

  1. プロジェクト設定を選択
  2. 「appsscript.json」マニフェスト ファイルをエディタで表示するをチェック
  3. エディタにあるappsscript.jsonを開く
  4. 下記のようにoauthScopesを追加
{
  "timeZone": "Asia/Tokyo",
  "dependencies": {},
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8",
  "webapp": {
    "executeAs": "USER_DEPLOYING",
    "access": "MYSELF"
  },
  "oauthScopes": [
    "https://www.googleapis.com/auth/script.external_request",
    "https://www.googleapis.com/auth/drive"
  ]
}

https://stackoverflow.com/questions/48812174/unauthorized-401-error-while-execute-as-me