0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

QiitaのAPIを使いたい

Posted at

APIアクセストークン 取得手順

go this page

write_qiita にチェック

pythonのみver + 解説

 import requests

url = "https://qiita.com/api/v2/items"
headers = {
    "Authorization": "Bearer あなたのアクセストークン",
    "Content-Type": "application/json"
}

data = {
    "title": "テスト下書き",
    "body": "これはPythonから送信した下書きです。",
    "tags": [],
    "private": True,
    "draft": True
}

response = requests.post(url, headers=headers, json=data)
print(response.status_code)
print(response.json())



AuthorizationQiitaのアクセストークン
draft: Trueこれが下書きの指定
private: True非公開本番記事ならFalse
requests.postQiitaにPOSTして下書きを作成

flaskのみver + 解説

from flask import Flask, request, jsonify
import requests

app = Flask(__name__)

@app.route("/create_draft", methods=["POST"])
def create_draft():
    data = request.json
    qiita_url = "https://qiita.com/api/v2/items"
    headers = {
        "Authorization": "Bearer あなたのアクセストークン",
        "Content-Type": "application/json"
    }

    draft_data = {
        "title": data.get("title", "無題"),
        "body": data.get("body", ""),
        "tags": data.get("tags", []),
        "private": True,
        "draft": True
    }

    res = requests.post(qiita_url, headers=headers, json=draft_data)
    return jsonify(res.json()), res.status_code

Flask + ajax version

flask
from flask import Flask, jsonify
import requests

app = Flask(__name__)

@app.route("/create_draft", methods=["GET"])
def create_draft():
    qiita_url = "https://qiita.com/api/v2/items"
    headers = {
        "Authorization": "Bearer あなたのアクセストークン",  # ここにQiitaアクセストークン
        "Content-Type": "application/json"
    }
    draft_data = {
        "title": "自動作成した下書き",
        "body": "これはページ読み込み時に自動作成されたQiita下書きです。",
        "tags": [{"name": "Flask"}, {"name": "Ajax"}],
        "private": True,
        "draft": True
    }
    res = requests.post(qiita_url, headers=headers, json=draft_data)
    if res.status_code == 201:
        data = res.json()
        return jsonify({
            "title": data.get("title"),
            "body": data.get("body")
        }), 201
    else:
        return jsonify({"error": res.text}), res.status_code

html
<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8" />
  <title>Qiita下書き自動作成</title>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>

  <h1>Qiita下書き作成結果</h1>
  <div id="draftInfo">
    読み込み中
  </div>

  <script>
    $(document).ready(function() {
      $.ajax({
        url: "/create_draft",
        type: "GET",
        success: function(res) {
          if (res.title && res.body) {
            $("#draftInfo").html(
              `<h2>${res.title}</h2><pre>${res.body}</pre>`
            );
          } else if(res.error) {
            $("#draftInfo").text("エラー: " + res.error);
          }
        },
        error: function(xhr) {
          $("#draftInfo").text("通信エラー: " + xhr.status);
        }
      });
    });
  </script>

</body>
</html>


解説
Flaskの /create_draft はGETリクエストでQiita下書きを自動作成しタイトルと本文bodyだけ返す
ページ読み込み時に $(document).ready() でAjax GETリクエストをFlaskに送り下書き作成を実行
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?