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で自分の記事を取得する手順

Last updated at Posted at 2025-10-11

Qiita APIで自分の記事を取得する手順

Qiita API v2を使用して、認証されたユーザー(ご自身)の記事一覧を取得する基本的な手順は以下の通りです。

1. Qiitaのアクセストークンを取得する

APIを利用するためには、認証が必要になります。

  1. Qiitaにログインします。
  2. [設定] 画面に移動し、左側のメニューから [アプリケーション] -> [個人用アクセストークン] を選択します。
  3. [新しくトークンを発行する] ボタンをクリックします。
  4. スコープ(権限)として、read_qiita にチェックを入れます。記事の読み取りのみであれば、このスコープがあれば十分です。
  5. [発行する] ボタンをクリックすると、アクセストークンが表示されます。
    • このトークンは一度しか表示されないため、安全な場所に控えてください。

2. APIリクエストを準備する

取得したアクセストークンと、Qiita APIの適切なエンドポイントを使ってリクエストを準備します。

エンドポイント

認証済みのユーザー自身の記事一覧を取得するためのエンドポイントを使用します。

  • HTTPメソッド: GET
  • URL: https://qiita.com/api/v2/authenticated_user/items

リクエストヘッダー

APIに認証情報を提供するために、取得したアクセストークンをヘッダーに含めます。

  • キー: Authorization
  • : Bearer [ここに取得したアクセストークンを挿入]
    • Bearer の後に半角スペースを入れ、トークンを続けます。

パラメータ(任意)

取得する記事の数やページを指定したい場合は、以下のクエリパラメータをURLに追加します。

パラメータ 説明 範囲 デフォルト
page 取得するページ番号 $1$ から $100$ $1$
per_page 1ページあたりに含まれる要素数 $1$ から $100$ $20$

例: $2$ページ目の記事を $50$件取得する場合:
https://qiita.com/api/v2/authenticated_user/items?page=2&per_page=50

3. APIリクエストを実行し、結果を処理する

準備したリクエストをプログラムから実行します。

  1. リクエストの送信: 準備したHTTPリクエスト(GETメソッド、指定のURL、Authorizationヘッダー)をAPIサーバーに送信します。
  2. レスポンスの受信: サーバーから記事データを含むレスポンスが返されます。
  3. JSONデータの解析: レスポンスの本文は通常 JSON 形式で提供されます。これをプログラムで解析し、必要な記事情報(タイトル、URL、本文、投稿日時など)を取り出します。
  4. データの利用: 抽出したデータを任意の用途(表示、保存、集計など)に利用します。

注記:

  • 上記の /authenticated_user/items エンドポイントは、認証されたユーザーの記事のみを取得します。
  • 特定ユーザーの公開記事を取得したい場合は、/users/:user_id/items エンドポイントを使用し、Authorizationヘッダーは不要ですが、そのユーザーのuser_idを知っている必要があります。

最後にLaravel12で記載したコードを記載します.

バックエンド側

<?php

namespace App\Http\Controllers;

use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use stdClass;

class QiitaController extends Controller
{
    //
    public function index(){

        $datas = [];
        $isData = (object)[];

        $curl = curl_init('https://qiita.com/api/v2/authenticated_user/items?page=1&per_page=10');
        $option = [
          CURLOPT_CUSTOMREQUEST => 'GET',
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_HTTPHEADER => [
            'Authorization: Bearer '.config('app.token'),
            'Content-Type: application/json',
          ],
        ];
        curl_setopt_array($curl, $option);

        $result = curl_exec($curl);
        $response = json_decode($result);

        if(json_last_error() === JSON_ERROR_NONE){
            foreach($response as $key=>$val){
                $isData->title = $val?->title;
                $isData->body = $val?->rendered_body;
                $isData->url = $val?->url;
                $datas[$key] = $isData;
            }
        }

        return View('qiita',['datas'=>$datas]);
    }

}

フロント側

qiita.blade.php
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="/assets/js/tailwindcss.3.3.5.js"></script>
    <title>Qiita</title>
</head>
<body class="bg-[#4cca2e]">
    <div class="mt-6 container mx-auto px-4 text-white">
        <p class="mb-4">
            <a href="/" class="text-white hover:underline">top</a>
        </p>

        <div class="container bg-[#555555] p-4 rounded shadow">

            @foreach($datas as $val)
                <div class="border-t border-gray-600 pt-4 mb-4">
                    <h2 class="text-xl font-bold mb-2">
                        <a href="{{$val?->url}}"
                           data-title='{{ $val?->title }}'
                           class="block">
                            {{ $val?->title }}
                        </a>
                    </h2>

                    <div class="p-3 bg-[#ffffff] rounded overflow-hidden">
                        @if(isset($val->body))
                            <iframe
                                srcdoc="{!! htmlspecialchars($val?->body) !!}"
                                sandbox="allow-scripts allow-same-origin"
                                class="w-full h-64 border border-gray-500 rounded"
                                title="記事本文"
                            >
                                本文の表示にはiframe対応ブラウザが必要です。
                            </iframe>
                        @else
                            <p>本文がありません。</p>
                        @endif
                    </div>
                </div>
            @endforeach
        </div>
    </div>
</body>
</html>


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?