3
3

More than 3 years have passed since last update.

Qiita API サンプル

Last updated at Posted at 2021-03-28

Qiita API を fetch API で叩くサンプルです。

環境

JSON Schema の取得

fetch('https://qiita.com/api/v2/schema?locale=ja')
.then(response => response.json())
.then(data => console.log(data));

データ取得系(GET)

認証不要

ユーザーの投稿記事を取得 api/v2/items

fetch('https://qiita.com/api/v2/items')
  .then(response => response.json())
  .then(data => console.log(data));
#!/bin/bash

echo `curl 'https://qiita.com/api/v2/items'`

クエリ(ユーザー指定)

fetch('https://qiita.com/api/v2/items?query=qiita user:sy250f')
  .then(response => response.json())
  .then(data => console.log(data));
get_item01.sh
#!/bin/bash

query='qiita user:sy250f'
function urlencode {
    echo "$1" | nkf -WwMQ | sed 's/=$//g' | tr = % | tr -d '\n'
}
urlencode "$query" | \
{
    encoded_query=$(cat) ;
    curl -s "https://qiita.com/api/v2/items?query=${encoded_query}"
}
$ ./get_item01.sh | jq '.[] | .title'

記事を指定して取得 /api/v2/items/:item_id

fetch('https://qiita.com/api/v2/items/c35aa4b1e8324c2bf21e')
  .then(response => response.json())
  .then(data => console.log(data));

認証必要

認証ユーザーの投稿記事を取得 api/v2/authenticated_user/items

fetch('https://qiita.com/api/v2/authenticated_user/items',
{
  method: 'GET',
  credentials: 'include',
  headers: {
    'Content-Type': 'application/json',
     Authorization: 'Bearer <アクセストークン>'
  }
})
  .then(response => response.json())
  .then(data => console.log(data));
get_item02.sh
#!/bin/bash

source ./.env
access_token="$ACCESSTOKEN"
auth="Authorization: Bearer ${access_token}"
curl -sH "${auth}" "https://qiita.com/api/v2/authenticated_user/items"
$ ./get_item02.sh | jq '.[] | .title'
$ ./get_item02.sh | jq '.[] | .title, .created_at, .id, .likes_count'

記事を指定して取得 /api/v2/items/:item_id

fetch('https://qiita.com/api/v2/items/c35aa4b1e8324c2bf21e',
{
  method: 'GET',
  credentials: 'include',
  headers: {
    'Content-Type': 'application/json',
     Authorization: 'Bearer <アクセストークン>'
  }
})
  .then(response => response.json())
  .then(data => console.log(data));
get_item04.sh
#!/bin/bash

source ./.env
access_token="$ACCESSTOKEN"
auth="Authorization: Bearer ${access_token}"
curl -sH "${auth}" "https://qiita.com/api/v2/authenticated_user/items?page=1&per_page=5" | \
jq -r '.[] | [.id,.title] | @csv' | \
awk -v AUTH="${auth}" -F, \
'
{
    gsub("\"", "", $1)
    url = "https://qiita.com/api/v2/items/" $1
    cmd_curl = "-sH" " \"" AUTH "\" " "\"" url "\""
    system("curl "cmd_curl"")
    cmd_sleep = "sleep 5"
    system(cmd_sleep)
}
'
exit 0
$ ./get_item04.sh | jq -r '[.page_views_count, .title, .id, .created_at] | @csv'
69,"Qiita API サンプル","c35aa4b1e8324c2bf21e","2021-03-28T10:52:22+09:00"
118,"Salesforceの添付ファイルをkintoneに移行する具体的な方法について","147af9159ebac368e2a5","2021-03-26T13:20:57+09:00"
72,"kintone.proxy を IPアドレス制限の元で利用する際の注意点について","50cbfc84fbf5988b2d38","2021-03-20T20:56:33+09:00"
103,"cli-kintone を使った添付ファイルの移行方法のメモ","bb8b4323eb01b1ed820c","2021-03-10T22:16:27+09:00"
122,"awk スクリプト メモ","60b6b114372f9b26c1f9","2021-03-10T09:18:33+09:00"
$ ./get_item04.sh | jq -r '[.likes_count, .page_views_count, .title, .id, .created_at] | @csv'
0,69,"Qiita API サンプル","c35aa4b1e8324c2bf21e","2021-03-28T10:52:22+09:00"
0,118,"Salesforceの添付ファイルをkintoneに移行する具体的な方法について","147af9159ebac368e2a5","2021-03-26T13:20:57+09:00"
0,72,"kintone.proxy を IPアドレス制限の元で利用する際の注意点について","50cbfc84fbf5988b2d38","2021-03-20T20:56:33+09:00"
0,103,"cli-kintone を使った添付ファイルの移行方法のメモ","bb8b4323eb01b1ed820c","2021-03-10T22:16:27+09:00"
0,122,"awk スクリプト メモ","60b6b114372f9b26c1f9","2021-03-10T09:18:33+09:00"

ユーザ情報を取得する /api/v2/authenticated_user

アクセストークンに紐付いたユーザ情報を取得する。

get_authenticated_user.sh
#!/bin/bash

source ./.env
access_token="$ACCESSTOKEN"
auth="Authorization: Bearer ${access_token}"
curl -sH "${auth}" "https://qiita.com/api/v2/authenticated_user"
exit 0
$ ./get_authenticated_user.sh | jq  '.items_count'
129

jq エラー

「jq: error (at :0): Cannot index string with string "xxxx"」

jqで指定したJSONのキーが存在しない場合に出力される。

$ curl -sH "${auth}" "https://qiita.com/api/v2/authenticated_user/items?page=1&per_page=105" | jq
{
  "message": "Bad request",
  "type": "bad_request"
}

$ curl -sH "${auth}" "https://qiita.com/api/v2/authenticated_user/items?page=1&per_page=105" | \
jq -r '.[] | [.id,.title]'
jq: error (at <stdin>:0): Cannot index string with string "id"

参考

3
3
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
3
3