인스타 태그 구조에 대해 정리한다. 데스크탑 기준
최악의 경우: 동적으로 구현해야 함.
...라고 생각했으나 다행히도 facebook 에서 만든 instagram API가 있다.
https://developers.facebook.com/docs/instagram-basic-display-api
페이스북 개발자 설정, Instagram 기본 디스플레이를 구성한다.
client id, client secret 을 확인한다.
1시간 유효기간 짜리 인증코드를 받는다.
https://api.instagram.com/oauth/authorize 로 들어가 인증을 신청한다.
https://api.instagram.com/oauth/authorize
?client_id=684477648739411
&redirect_uri=https://socialsizzle.herokuapp.com/auth/
&scope=user_profile,user_media
&response_type=code
1번에서 생성한 url로 들어가면 아래와 같은 화면이 뜬다.
여기서 allow를 클릭하면 위에서 url에 포함시켰던 redirect_uri로 1시간짜리 유효기간의 코드와 함께 리디렉션된다.
redirect_uri가 만약 http://helloworld.com/auth/
라면
http://helloworld.com/auth/?code=abcdefg#_
가 리턴이 된다. 여기서 맨 뒤에 #_
를 제외한 abcdefg
가 코드이다. 유효기간은 1시간이다.
코드를 단기 실행 액세스 토큰으로 교환한다.(이것도 1시간 짜리다.)
url: https://api.instagram.com/oauth/access_token
client_id, client_secret, code, grant_type, redirect_uri 를 보낸다.
여기서 grant_type 은 문자열 'authorization_code'이고, redirect_uri는 아까 사용했던 똑같은 리디렉션 uri다.
curl -X POST \
https://api.instagram.com/oauth/access_token \
-F client_id=990602627938098 \
-F client_secret=eb8c7... \
-F grant_type=authorization_code \
-F redirect_uri=https://socialsizzle.herokuapp.com/auth/ \
-F code=AQBx-hBsH3...
이 요청을 보내면 아래와 같은 응답이 온다.
// 성공 시
{
"access_token": "IGQVJ...",
"user_id": 17841405793187218
}
// 실패 시
{
"error_type": "OAuthException",
"code": 400,
"error_message": "Matching code was not found or was already used"
}
0 ~ 1은 수동으로 해야 한다.
리디렉션 url로 들어가면 code를 받아서 바로 장기로 만들어 DB 테이블에 저장하는 컨트롤러를 하나 만든다.
테이블에는 client_id, client_secret 이 저장되어 있어야 함.
GET /{user-id}/media
스토리 제외, 최근 10,000개를 가져온다고 한다.
연예인도 1,000개 넘기기 힘들다.
GET https://graph.instagram.com/v20.0/abcdefg/media?access_token=please-let-me-in&fields=id,media_type,media_url,permalink,timestamp&since=1234567
since를 이용해 마지막으로 저장된 데이터를 가져온다.
{
"data": [
{
"id": "17895695668004550"
},
{
"id": "17899305451014820"
},
{
"id": "17896450804038745"
},
{
"id": "17881042411086627"
}
],
"paging": {
"cursors": {
"after": "MTAxN...",
"before": "NDMyN..."
},
"next": "https://graph.faceb..."
}
}
한 페이지에 25개씩 준다.
paging.next가 있으면 다음 페이지가 있다는 거고, 그게 없고 previous가 있으면 마지막 페이지이다.