In [1]:
import requests
import pprint

In [2]:
with open('data/temp/qiita_access_token.txt') as f:
    qiita_access_token = f.read().strip()

In [3]:
headers = {'Authorization': 'Bearer {}'.format(qiita_access_token)}

In [4]:
url_items = 'https://qiita.com/api/v2/items'

In [5]:
item_data = {
    'title': 'テスト記事',
    'body': 'テスト',
    'private': True,
    'tags': [{'name': 'test'}],
    'coediting': False,
    'gist': False,
    'tweet': False
}

In [6]:
r_post = requests.post(url_items, headers=headers, json=item_data)

In [7]:
print(r_post.status_code)


201

In [8]:
pprint.pprint(r_post.json())


{'body': 'テスト\n',
 'coediting': False,
 'comments_count': 0,
 'created_at': '2018-07-12T22:05:19+09:00',
 'group': None,
 'id': '93ead2568150009de5f1',
 'likes_count': 0,
 'page_views_count': None,
 'private': True,
 'reactions_count': 0,
 'rendered_body': '<p>テスト</p>\n',
 'tags': [{'name': 'test', 'versions': []}],
 'title': 'テスト記事',
 'updated_at': '2018-07-12T22:05:19+09:00',
 'url': 'https://qiita.com/nkmk/private/93ead2568150009de5f1',
 'user': {'description': '',
          'facebook_id': '',
          'followees_count': 0,
          'followers_count': 0,
          'github_login_name': None,
          'id': 'nkmk',
          'items_count': 0,
          'linkedin_id': '',
          'location': '',
          'name': '',
          'organization': '',
          'permanent_id': 53096,
          'profile_image_url': 'https://qiita-image-store.s3.amazonaws.com/0/53096/profile-images/1473692950',
          'twitter_screen_name': None,
          'website_url': ''}}

In [9]:
item_id = r_post.json()['id']
print(item_id)


93ead2568150009de5f1

In [10]:
r_get = requests.get(url_items + '/' + item_id, headers=headers)

In [11]:
print(r_get.status_code)


200

In [12]:
pprint.pprint(r_get.json())


{'body': 'テスト\n',
 'coediting': False,
 'comments_count': 0,
 'created_at': '2018-07-12T22:05:19+09:00',
 'group': None,
 'id': '93ead2568150009de5f1',
 'likes_count': 0,
 'page_views_count': 0,
 'private': True,
 'reactions_count': 0,
 'rendered_body': '<p>テスト</p>\n',
 'tags': [{'name': 'test', 'versions': []}],
 'title': 'テスト記事',
 'updated_at': '2018-07-12T22:05:19+09:00',
 'url': 'https://qiita.com/nkmk/private/93ead2568150009de5f1',
 'user': {'description': '',
          'facebook_id': '',
          'followees_count': 0,
          'followers_count': 0,
          'github_login_name': None,
          'id': 'nkmk',
          'items_count': 0,
          'linkedin_id': '',
          'location': '',
          'name': '',
          'organization': '',
          'permanent_id': 53096,
          'profile_image_url': 'https://qiita-image-store.s3.amazonaws.com/0/53096/profile-images/1473692950',
          'twitter_screen_name': None,
          'website_url': ''}}

In [13]:
item_data_updated = item_data.copy()
item_data_updated['title'] = 'テスト記事更新'
print(item_data_updated)


{'title': 'テスト記事更新', 'body': 'テスト', 'private': True, 'tags': [{'name': 'test'}], 'coediting': False, 'gist': False, 'tweet': False}

In [14]:
r_patch = requests.patch(url_items + '/' + item_id, headers=headers, json=item_data_updated)

In [15]:
print(r_patch.status_code)


200

In [16]:
pprint.pprint(r_patch.json())


{'body': 'テスト\n',
 'coediting': False,
 'comments_count': 0,
 'created_at': '2018-07-12T22:05:19+09:00',
 'group': None,
 'id': '93ead2568150009de5f1',
 'likes_count': 0,
 'page_views_count': None,
 'private': True,
 'reactions_count': 0,
 'rendered_body': '<p>テスト</p>\n',
 'tags': [{'name': 'test', 'versions': []}],
 'title': 'テスト記事更新',
 'updated_at': '2018-07-12T22:05:19+09:00',
 'url': 'https://qiita.com/nkmk/private/93ead2568150009de5f1',
 'user': {'description': '',
          'facebook_id': '',
          'followees_count': 0,
          'followers_count': 0,
          'github_login_name': None,
          'id': 'nkmk',
          'items_count': 0,
          'linkedin_id': '',
          'location': '',
          'name': '',
          'organization': '',
          'permanent_id': 53096,
          'profile_image_url': 'https://qiita-image-store.s3.amazonaws.com/0/53096/profile-images/1473692950',
          'twitter_screen_name': None,
          'website_url': ''}}

In [17]:
r_delete = requests.delete(url_items + '/' + item_id, headers=headers)

In [18]:
print(r_delete.status_code)


204

In [19]:
url_tag = 'https://qiita.com/api/v2/tags/{}/following'

In [20]:
tag = 'python'

In [21]:
r_put = requests.put(url_tag.format(tag), headers=headers)

In [22]:
print(r_put.status_code)


204

In [23]:
r_delete_tag = requests.delete(url_tag.format(tag), headers=headers)

In [24]:
print(r_delete_tag.status_code)


204