はじめに
最近Instagram のAPI を使った処理を書いているのですが、特定のユーザーの投稿情報を取得する時の処理が、他のWebAPIでのそれとちょっと異なる感じがありちょっとハマったので、簡単にまとめておきます。
ポイントは
- usernameとuser_idという2つの項目があるがAPIで指定するユーザー名というのはuser_id になる
- (ちょっと調べた感じだと)InstagramのWeb上の画面からはuser_idを簡単に見つける方法がなさそう。そのためInstagram APIの/searchを使ってusernameからuser_idを取得した上で処理を行う必要ある
です。
実際の作業手順
無印良品のアカウントをフォローしていたので、そのアカウントで投稿された写真を取得する流れについてまとめます。
※ 開発者登録の一連の流れについては説明割愛してますのでその辺りの作業はQiita内などで情報探してもらえればと思います
まずはアクセストークンを取得する
Browserで以下のようなURLを入力する
https://instagram.com/oauth/authorize/?client_id=自分のクライアントIDを指定&redirect_uri=リダイレクト先のURL&response_type=token
アクセスすると、redirect_uriに指定したURLにリダイレクトされつつ、access_tokenのパラーメーターに実際のアクセストークンが付与されるのでこれをメモしておきます
トークンを利用してユーザー名からuser_idを検索する
特定のユーザーが投稿した写真を取得する時には user_id の情報が必要になるが、このIDを知るために、まずは/v1/users/search?q=usernameを利用してuser_idを調べる必要がある
https://api.instagram.com/v1/users/search?q=muji_global&access_token=取得したアクセストークン
{
"meta": {
"code": 200
},
"data": {
"username": "muji_global",
"bio": "Share your MUJI Life with #muji \nMUJI official Instagram. 無印良品公式Instagramです。",
"website": "http://muji.lu/1IuQyfp",
"profile_picture": "https://igcdn-photos-b-a.akamaihd.net/hphotos-ak-xaf1/t51.2885-19/11379928_1598841523719305_1744704265_a.jpg",
"full_name": "MUJI無印良品",
"counts": {
"media": 59,
"followed_by": 56251,
"follows": 9
},
"id": "1771059415"
}
}
取得したuser_idをキーにして投稿された写真データーを取得する
MUJI無印良品の場合
- username: muji_global
- user_id: 1771059415
ということのようなので
https://api.instagram.com/v1/users/1771059415/media/recent?access_token=取得したアクセストークン
という感じでアクセスすると以下のJSONが得られます
{
"pagination": {
"next_url": "https://api.instagram.com/v1/users/1771059415/media/recent?access_token=189601.57eacc5.903fc3e5ed0c4ffd97d9559be09d181b&count=3&max_id=1080215958242787438_1771059415",
"next_max_id": "1080215958242787438_1771059415"
},
"meta": {
"code": 200
},
"data": [
{
"attribution": null,
"tags": [
"カレー",
"muji",
"curry",
"無印良品"
],
"type": "image",
"location": null,
"comments": {
"count": 3,
"data": [
{
"created_time": "1443152237",
"text": "Are those lentils ?",
"from": {
"username": "ronvldor",
"profile_picture": "https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-19/11242405_847854985302475_913754906_a.jpg",
"id": "21544339",
"full_name": ""
},
"id": "1081561818985726022"
},
{
"created_time": "1443152760",
"text": "@faithinfudge",
"from": {
"username": "kstereolab",
"profile_picture": "https://scontent.cdninstagram.com/hphotos-xfa1/t51.2885-19/11093113_451435538365872_199038254_a.jpg",
"id": "1347166545",
"full_name": "Kevin Bautista"
},
"id": "1081566206185767213"
},
{
"created_time": "1443156365",
"text": "gross some things r better not muji",
"from": {
"username": "kpxshygirl",
"profile_picture": "https://scontent.cdninstagram.com/hphotos-xfa1/t51.2885-19/s150x150/11881709_1792000734359907_1057496360_a.jpg",
"from": {
"username": "contrail_4",
"profile_picture": "https://scontent.cdninstagram.com/hphotos-xfa1/t51.2885-19/11378147_413713952145547_1399536866_a.jpg",
"id": "1554643053",
"full_name": ""
},
"id": "1081119605973647907"
},
{
"created_time": "1443114542",
"text": "@radaeze what are these??",
"from": {
"username": "gracegrr",
"profile_picture": "https://igcdn-photos-d-a.akamaihd.net/hphotos-ak-xaf1/t51.2885-19/11875357_1615821182011075_1373152724_a.jpg",
"id": "21209919",
"full_name": "Grace"
},
"id": "1081245609979347938"
}
]
},
"filter": "Normal",
"created_time": "1443076043",
"link": "https://instagram.com/p/8ANW5yxQP4/",
"likes": {
"count": 2165,
"data": [
{
"username": "tnmi3",
"profile_picture": "https://igcdn-photos-a-a.akamaihd.net/hphotos-ak-xfa1/t51.2885-19/s150x150/11910482_1641532012802872_318705167_a.jpg",
"id": "2202070408",
"full_name": "小林富子"
},
{
"username": "my_panda_lee",
"profile_picture": "https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-19/s150x150/11336031_471713886368949_124226892_a.jpg",
"id": "2202011860",
"full_name": "Amanda Panda Lee"
},
{
"username": "hajinyan06",
"profile_picture": "https://instagramimages-a.akamaihd.net/profiles/anonymousUser.jpg",
"id": "2202100444",
"full_name": "アイ"
},
{
"username": "bakeblonde",
"profile_picture": "https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-19/s150x150/11821237_555996537882365_341558551_a.jpg",
"id": "2204028180",
"full_name": "The Blonde Baker"
}
]
},
"images": {
"low_resolution": {
"url": "https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-15/s320x320/e35/11909101_753345441437873_239436216_n.jpg",
"width": 320,
"height": 320
},
"thumbnail": {
"url": "https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-15/s150x150/e35/11909101_753345441437873_239436216_n.jpg",
"width": 150,
"height": 150
},
"standard_resolution": {
"url": "https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-15/s640x640/sh0.08/e35/11909101_753345441437873_239436216_n.jpg",
"width": 640,
"height": 640
}
},
"users_in_photo": [],
"caption": {
"created_time": "1443076043",
"text": "ポーランドとセルビア産のフランボワーズを使った「フランボワーズのフィナンシェ」と、濃厚で力強い味が特長のイタリア産の栗を使った「マロンのフィナンシェ」。\n生地との相性を考えてバターにもこだわり、小さくてもしっとりとした食感と 豊潤な風味が味わえるフィナンシェです。\nFRAMBOISE FINANCIER / MARRON FINANCIER\n#muji #無印良品 #お菓子 #おやつ #sweets",
"from": {
"username": "muji_global",
"profile_picture": "https://igcdn-photos-b-a.akamaihd.net/hphotos-ak-xaf1/t51.2885-19/11379928_1598841523719305_1744704265_a.jpg",
"id": "1771059415",
"full_name": "MUJI無印良品"
},
"id": "1080922661850514055"
},
"user_has_liked": false,
"id": "1080922659057107960_1771059415",
"user": {
"username": "muji_global",
"profile_picture": "https://igcdn-photos-b-a.akamaihd.net/hphotos-ak-xaf1/t51.2885-19/11379928_1598841523719305_1744704265_a.jpg",
"id": "1771059415",
"full_name": "MUJI無印良品"
}
},
{
"attribution": null,
"tags": [
"regram",
"muji",
"無印良品"
],
"type": "image",
"location": null,
"comments": {
"count": 17,
"data": [
{
"created_time": "1442997666",
"text": "@houssineb sahda",
"from": {
"username": "sofiat",
"profile_picture": "https://scontent.cdninstagram.com/hphotos-xfa1/t51.2885-19/1530880_1619442644970899_591312113_a.jpg",
"id": "14964",
"full_name": "Sofia"
},
"id": "1080265177544589837"
},
{
"created_time": "1442998130",
"text": "@licn3pbh @jo_essential",
"from": {
"username": "minokong",
"profile_picture": "https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-19/s150x150/11909949_765610736880891_1607038378_a.jpg",
"id": "1937255",
"full_name": "MinokoNg"
},
"id": "1080269076854801053"
},
"id": "2197451059",
"full_name": "Hikari Hara"
},
{
"username": "my_panda_lee",
"profile_picture": "https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-19/s150x150/11336031_471713886368949_124226892_a.jpg",
"id": "2202011860",
"full_name": "Amanda Panda Lee"
}
]
},
"images": {
"low_resolution": {
"url": "https://scontent.cdninstagram.com/hphotos-xfa1/t51.2885-15/s320x320/e35/11427425_1503649359948365_1368765384_n.jpg",
"width": 320,
"height": 320
},
"thumbnail": {
"url": "https://scontent.cdninstagram.com/hphotos-xfa1/t51.2885-15/s150x150/e35/11427425_1503649359948365_1368765384_n.jpg",
"width": 150,
"height": 150
},
"standard_resolution": {
"url": "https://scontent.cdninstagram.com/hphotos-xfa1/t51.2885-15/e35/11427425_1503649359948365_1368765384_n.jpg",
"width": 640,
"height": 640
}
},
"users_in_photo": [],
"caption": {
"created_time": "1442991798",
"text": "シンガポールでのCafe&Meal MUJI一号店「Cafe&Meal MUJI Paragon」が、9/5にオープンしました。シンガポール有
数のショッピングモールParagon内の「MUJI Paragon」に隣接。シンガポールやマレーシアで調達した食材を主に使用し、日本のレシピを基本にしながら、現地の調味料による味付けのアレンジを加えています。\nCafe&Meal MUJI Paragon, our first Cafe&Meal MUJI in
Singapore, recently opened on Sep 5th.\n@mujisg\n#muji #無印良品 #regram",
"from": {
"username": "muji_global",
"profile_picture": "https://igcdn-photos-b-a.akamaihd.net/hphotos-ak-xaf1/t51.2885-19/11379928_1598841523719305_1744704265_a.jpg",
"id": "1771059415",
"full_name": "MUJI無印良品"
},
"id": "1080215960667095839"
},
"user_has_liked": false,
"id": "1080215958242787438_1771059415",
"user": {
"username": "muji_global",
"profile_picture": "https://igcdn-photos-b-a.akamaihd.net/hphotos-ak-xaf1/t51.2885-19/11379928_1598841523719305_1744704265_a.jpg",
"id": "1771059415",
"full_name": "MUJI無印良品"
}
}
]
}
以下余談
ちなみにQiita のAPIでは
http://qiita.com/api/v2/users/h5y1m141@github
のように、/v2/users/:user_idでアクセスするとそのユーザーについての情報が取得できます。
こういう使い方のほうが自分はしっくり来ますね^^;