Instagram APIを使って何ができるか[準備編]

# 事前準備
- instagramアカウントをbusinessアカウントにする
- FB Developerページと、instagramアカウントを紐付ける

## Access Tokenを取得する 
- FB Developerページでアプリカウントの有効期限付きaccess_tokenを発行する
- 有効期限無しのaccess_tokenを取得
- Instagram APIへのaccess_tokenを取得する

### FB Developersページでアプリカウントの有効期限付きaccess_tokenを発行する

ツール>グラフAPIエクスプローラーで発行する
![ツール.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/695313/2df12e6c-be1a-a38e-e06e-c6fab31886a9.png)

Generate Access Tokenボタンで発行する
![1st_access_token.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/695313/cb42254e-f1cc-23cf-763f-0d9771045e5b.png)

有効期限を確認する
![1st_access_token_期限確認.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/695313/93c02d6d-b59c-7cb4-6628-6e89ddfa7c8a.png)


### 有効期限無しのaccess_tokenを取得

上記のアクセストークンツールで開くボタンから、下部にスクロールしアクセストークンを延長する
![access_token_延長.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/695313/514152dd-58bb-e8d4-d8f2-f0bb8b9e6a35.png)

発行されたトークンを確認する
![access_token_no_limit.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/695313/b535caaf-b6a3-7b37-7427-d6afbbddd493.png)

デバッグボタンで期限を確認。有効期限が**受け取らない**になってることを確認
![access_token_nolimit_確認.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/695313/52d475c3-507b-419e-4b23-5bca72eba313.png)

### Instagram APIへのaccess_tokenを取得する

上記で取得したアクセストークンを入力し、URLを`me/accounts`として送信する
レスポンスのaccess_tokenを使って、Instagram APIをコールする準備完了
![me:accounts_img.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/695313/b0dd8780-9b83-8fe5-2774-83bcaeb90b8a.png)

## Instagram ビジネスアカウントIDを取得する
上記取得したaccess_tokenをテキストボックスに入力し、URLを`me?fields=instagram_business_account`として送信する
![business_account_id.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/695313/b894d849-f9ee-0d82-75dd-d7b77a8fd065.png)

## 投稿情報を取得してみる
[Apple](https://www.instagram.com/apple/) のInstagram投稿を取得する
fieldsパラメータに`business_discovery.username(apple)`と指定する
`id,media_url,permalink`など他に取得したいパラメータを適宜指定する

グラフAPIエクスプローラやpostmanのキャプチャではfieldsパラメータが切れてしまうので、一旦コードで記載する
```php
<?php

$request = new HttpRequest();
$request->setUrl('https://graph.facebook.com/{ビジネスアカウントID}');
$request->setMethod(HTTP_METH_GET);

$request->setQueryData(array(
  'fields' => 'business_discovery.username(apple){media.limit(10){id,media_type,media_url,owner,timestamp,caption,permalink,children{id,media_url,media_type}}}',
  'access_token' => '{取得したaccesss_token}'
));

$request->setHeaders(array(
  'cache-control' => 'no-cache'
));

try {
  $response = $request->send();

  echo $response->getBody();
} catch (HttpException $ex) {
  echo $ex;
}
```

Response例
```
{
    "business_discovery": {
        "media": {
            "data": [
                {
                    "id": "17947874257979434",
                    "media_type": "IMAGE",
                    "media_url": "https://scontent-sjc3-1.cdninstagram.com/v/t51.29350-15/287028450_792951071872152_2439096661985520478_n.jpg?_nc_cat=1&ccb=1-7&_nc_sid=8ae9d6&_nc_ohc=qLDz38I2bIgAX95ogxL&_nc_ht=scontent-sjc3-1.cdninstagram.com&edm=AL-3X8kEAAAA&oh=00_AT9tf5HR6BvaLVoTQD6dXwXkvbS-1f12NdmOmj31DkN2Xg&oe=62A91F9C",
                    "owner": {
                        "id": "17841405945557682"
                    },
                    "timestamp": "2022-06-11T16:00:46+0000",
                    "caption": "“I was walking my dog in mid January and decided to walk around these corn fields. As we walked, I noticed someone up ahead and liked the composition, so I managed to quickly take the photo. The curve of the path leading to the tree helps make for a naturally beautiful photo.\" #ShotoniPhone by Alan R. alanincornwall",
                    "permalink": "https://www.instagram.com/p/Ceq8H47lf7Y/"
                },
                {
                    "id": "17949714253938046",
                    "media_type": "CAROUSEL_ALBUM",
                    "media_url": "https://scontent-sjc3-1.cdninstagram.com/v/t51.29350-15/286429948_5858670724146508_8991816610208583769_n.jpg?_nc_cat=1&ccb=1-7&_nc_sid=8ae9d6&_nc_ohc=qhqwru21CS4AX9zo6G3&_nc_ht=scontent-sjc3-1.cdninstagram.com&edm=AL-3X8kEAAAA&oh=00_AT-Yg4esXX7Vh621qaAU8x7HnswxOnmMeL1Sx062mMsbsA&oe=62A91504",
                    "owner": {
                        "id": "17841405945557682"
                    },
                    "timestamp": "2022-06-10T16:06:28+0000",
                    "caption": "Commissioned by Apple. As Told By Us: Honoring Pride’s past, present and future through the historic places that started the celebration. Five artists. Five locations. Location #2 — Plaza de Chueca, Madrid.\n\n“This piece of work is a tribute to a very historical place that has helped the queer community get more rights and be recognized. It is important to remember all those who have helped us be privileged, who have fought for our rights. It is also a tribute to our present community—to tell them that they are not alone; we are everywhere, and we are united. We are beautiful and powerful.” #Pride #ShotoniPhone by Lydia M. lydiametral \n-\n1. Adrian is a multidisciplinary artist who has found many opportunities in the city of Madrid and has built strong friendships there. He is now thriving and he is a restless soul who always needs to be creating and experimenting. \n-\n2. Lolita Watson and Claret Castell met in 2016 during a photoshoot. They fell in love and got married in 2020 in Madrid and the city has always been an example of tolerance and opportunities for them. Claret remembers feeling very intimidated by Lolita's deep and intense gaze.\n-\n3. Michelle Nicholas is a trans woman born in La Havana who has discovered herself through dance and theatre. For her, Chueca has always represented freedom. Shehas never felt as safe as she does in this neighborhood, walking through the streets, feeling confident in who she is. \n-\n4. Alex was born in Madrid and educated in England. He started working on stage through magic and later became interested in acting, dramaturgy and movement. He has been involved in numerous feminist and queer projects.",
                    "permalink": "https://www.instagram.com/p/CeoX-2Blh5k/",
                    "children": {
                        "data": [
                            {
                                "id": "17963010520689526",
                                "media_url": "https://scontent-sjc3-1.cdninstagram.com/v/t51.29350-15/286429948_5858670724146508_8991816610208583769_n.jpg?_nc_cat=1&ccb=1-7&_nc_sid=8ae9d6&_nc_ohc=qhqwru21CS4AX9zo6G3&_nc_ht=scontent-sjc3-1.cdninstagram.com&edm=AL-3X8kEAAAA&oh=00_AT-Yg4esXX7Vh621qaAU8x7HnswxOnmMeL1Sx062mMsbsA&oe=62A91504",
                                "media_type": "IMAGE"
                            },
                            {
                                "id": "18042046456344461",
                                "media_url": "https://scontent-sjc3-1.cdninstagram.com/v/t51.29350-15/286758553_3156773694564688_6110607338513869234_n.jpg?_nc_cat=1&ccb=1-7&_nc_sid=8ae9d6&_nc_ohc=0JR49CQdcEAAX8q_2tQ&_nc_ht=scontent-sjc3-1.cdninstagram.com&edm=AL-3X8kEAAAA&oh=00_AT9luTGNga6ZQ_0rXMcZoIIzcYigU_2h_HOIS90twvHe1w&oe=62A9F074",
                                "media_type": "IMAGE"
                            },
                            {
                                "id": "17916515858374746",
                                "media_url": "https://scontent-sjc3-1.cdninstagram.com/v/t51.29350-15/286780713_417781413537093_3077723023882756046_n.jpg?_nc_cat=1&ccb=1-7&_nc_sid=8ae9d6&_nc_ohc=Gi7dHgAONekAX8AbpH3&_nc_ht=scontent-sjc3-1.cdninstagram.com&edm=AL-3X8kEAAAA&oh=00_AT9iZESkojB76p-Atvx15lrUA38z99UtWMZI9XRGlIwJMg&oe=62A9BD39",
                                "media_type": "IMAGE"
                            },
                            {
                                "id": "17947410127977016",
                                "media_url": "https://scontent-sjc3-1.cdninstagram.com/v/t51.29350-15/287271851_756442318691257_429219939475216221_n.jpg?_nc_cat=100&ccb=1-7&_nc_sid=8ae9d6&_nc_ohc=i2SIoHB5hLAAX8XMBsu&_nc_ht=scontent-sjc3-1.cdninstagram.com&edm=AL-3X8kEAAAA&oh=00_AT-qsNQPgl589Sh1kqOu9IWXmgnxyx0VFyZN6hgVAVffwg&oe=62AA267D",
                                "media_type": "IMAGE"
                            }
                        ]
                    }
                },
以下割愛
```

## 応用編

#### ハッシュタグ検索なども可能

https://developers.facebook.com/docs/instagram-api/guides/hashtag-search

投稿データを保存したり、画像やハッシュタグを検索するツール等APIを使って何か作れそうなのでまたの機会に投稿します