Instagram OAuth API
Instagram Business 계정 연동 API (Facebook Graph API 기반)
Instagram OAuth API
Instagram Business 계정을 연동하기 위한 OAuth API입니다. Facebook Graph API를 통해 Instagram Business Account에 접근합니다.
Base URL: /ai/instagram
인증 정보
| 항목 | 값 |
|---|---|
| 인증 필요 | 일부 필요 |
| 인증 방식 | JWT Bearer Token |
OAuth 연동 플로우
Instagram 연동은 프론트엔드 Callback 방식을 사용합니다.
1. OAuth URL 요청
프론트엔드에서 백엔드 API를 호출하여 Facebook OAuth URL을 받습니다.
GET /ai/instagram/oauth/url?memberId=user123응답:
{
"status": 200,
"message": "OAuth URL 생성 완료",
"data": "https://www.facebook.com/v18.0/dialog/oauth?client_id=xxx&redirect_uri=xxx&state=xxx&scope=xxx"
}2. Facebook 인증 페이지로 리다이렉트
프론트엔드에서 받은 OAuth URL로 사용자를 리다이렉트합니다.
window.location.href = oauthUrl;사용자는 Facebook 로그인 후 Instagram Business 계정 접근 권한을 허용합니다.
3. 프론트엔드 Callback 수신
Facebook이 사용자를 프론트엔드 callback URL로 리다이렉트합니다.
https://your-frontend.com/instagram/callback?code=AUTH_CODE&state=STATE_VALUE| 파라미터 | 설명 |
|---|---|
code | Authorization code (토큰 교환용) |
state | CSRF 방지용 state 값 (memberId 포함) |
4. 백엔드에 code 전달
프론트엔드가 받은 code와 state를 백엔드 API로 전달합니다.
POST /ai/instagram/oauth/connect?code=AUTH_CODE&state=STATE_VALUE응답:
{
"status": 200,
"message": "Instagram 계정이 연결되었습니다.",
"data": [
{
"accountId": 1,
"username": "my_instagram",
"name": "My Instagram Account",
"profilePictureUrl": "https://...",
"followersCount": 10000,
"connectionStatus": "CONNECTED"
}
]
}플로우 다이어그램
┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ 프론트 │ │ 백엔드 │ │ Facebook │ │ Instagram │
└──────┬──────┘ └──────┬──────┘ └──────┬──────┘ └──────┬──────┘
│ │ │ │
│ 1. GET /oauth/url │ │ │
│──────────────────>│ │ │
│ │ │ │
│ OAuth URL 반환 │ │ │
│<──────────────────│ │ │
│ │ │ │
│ 2. 리다이렉트 │ │ │
│───────────────────────────────────────> │
│ │ │ │
│ │ 사용자 인증 │ │
│ │ 권한 허용 │ │
│ │ │ │
│ 3. Callback (code, state) │ │
│<──────────────────────────────────────│ │
│ │ │ │
│ 4. POST /oauth/connect │ │
│──────────────────>│ │ │
│ │ │ │
│ │ 5. Token 교환 │ │
│ │──────────────────>│ │
│ │ │ │
│ │ Access Token │ │
│ │<──────────────────│ │
│ │ │ │
│ │ 6. 프로필 조회 │ │
│ │───────────────────────────────────────>
│ │ │ │
│ │ Instagram 정보 │ │
│ │<──────────────────────────────────────│
│ │ │ │
│ 연결된 계정 반환 │ │ │
│<──────────────────│ │ │
│ │ │ │엔드포인트 목록
| 메서드 | 경로 | 설명 | 인증 |
|---|---|---|---|
GET | /ai/instagram/oauth/url | OAuth URL 조회 | 불필요 |
POST | /ai/instagram/oauth/connect | Instagram 계정 연결 | 불필요 |
GET | /ai/instagram/accounts | 연결된 계정 목록 조회 | 불필요 |
DELETE | /ai/instagram/accounts/{accountNo} | 계정 연결 해제 | 불필요 |
POST | /ai/instagram/accounts/{accountNo}/sync | 계정 정보 동기화 | 불필요 |
API 상세
OAuth URL 조회
Instagram 연결을 위한 Facebook OAuth URL을 생성합니다.
HTTP 요청
GET /ai/instagram/oauth/urlQuery Parameters
| 파라미터 | 타입 | 필수 | 설명 |
|---|---|---|---|
memberId | string | 예 | 회원 ID |
응답
성공 응답 (200 OK)
{
"status": 200,
"code": null,
"message": "OAuth URL 생성 완료",
"data": "https://www.facebook.com/v18.0/dialog/oauth?client_id=123456&redirect_uri=https://your-frontend.com/instagram/callback&state=base64encodedstate&scope=instagram_basic,instagram_manage_insights,pages_show_list,pages_read_engagement,business_management&response_type=code"
}Instagram 계정 연결
프론트엔드에서 받은 authorization code로 Instagram 계정을 연결합니다.
HTTP 요청
POST /ai/instagram/oauth/connectQuery Parameters
| 파라미터 | 타입 | 필수 | 설명 |
|---|---|---|---|
code | string | 예 | Facebook에서 받은 authorization code |
state | string | 예 | OAuth URL 생성 시 포함된 state 값 |
응답
성공 응답 (200 OK)
{
"status": 200,
"code": null,
"message": "Instagram 계정이 연결되었습니다.",
"data": [
{
"accountId": 1,
"platform": 1,
"platformUserId": "17841400000000000",
"username": "my_instagram_account",
"name": "My Instagram",
"profilePictureUrl": "https://scontent.cdninstagram.com/...",
"biography": "인스타그램 소개글",
"website": "https://mywebsite.com",
"followersCount": 15000,
"followingCount": 500,
"mediaCount": 120,
"connectionStatus": "CONNECTED",
"priceTier": "B",
"price": 150000
}
]
}에러 응답
| 상태 코드 | 에러 코드 | 설명 |
|---|---|---|
400 | INVALID_REQUEST | code 또는 state 누락 |
500 | CONNECTION_FAILED | 연결 실패 (토큰 교환 오류 등) |
연결된 계정 목록 조회
회원에게 연결된 Instagram 계정 목록을 조회합니다.
HTTP 요청
GET /ai/instagram/accountsQuery Parameters
| 파라미터 | 타입 | 필수 | 설명 |
|---|---|---|---|
memberId | string | 예 | 회원 ID |
응답
성공 응답 (200 OK)
{
"status": 200,
"code": null,
"message": "연결된 계정 조회 완료",
"data": [
{
"accountId": 1,
"platform": 1,
"platformUserId": "17841400000000000",
"username": "my_instagram_account",
"name": "My Instagram",
"profilePictureUrl": "https://scontent.cdninstagram.com/...",
"followersCount": 15000,
"connectionStatus": "CONNECTED",
"priceTier": "B",
"price": 150000
}
]
}계정 연결 해제
지정된 Instagram 계정의 연결을 해제합니다.
HTTP 요청
DELETE /ai/instagram/accounts/{accountNo}Path Parameters
| 파라미터 | 타입 | 필수 | 설명 |
|---|---|---|---|
accountNo | long | 예 | Instagram 계정 번호 |
Query Parameters
| 파라미터 | 타입 | 필수 | 설명 |
|---|---|---|---|
memberId | string | 예 | 회원 ID |
응답
성공 응답 (200 OK)
{
"status": 200,
"code": null,
"message": "Instagram 계정 연결이 해제되었습니다.",
"data": null
}계정 정보 동기화
Instagram 계정의 프로필 정보를 최신으로 업데이트합니다.
HTTP 요청
POST /ai/instagram/accounts/{accountNo}/syncPath Parameters
| 파라미터 | 타입 | 필수 | 설명 |
|---|---|---|---|
accountNo | long | 예 | Instagram 계정 번호 |
Query Parameters
| 파라미터 | 타입 | 필수 | 설명 |
|---|---|---|---|
memberId | string | 예 | 회원 ID |
응답
성공 응답 (200 OK)
{
"status": 200,
"code": null,
"message": "동기화 완료",
"data": {
"accountId": 1,
"username": "my_instagram_account",
"followersCount": 15500,
"connectionStatus": "CONNECTED"
}
}Response 스키마
SocialAccountDto
| 필드명 | 타입 | 설명 |
|---|---|---|
accountId | long | 계정 ID |
platform | int | 플랫폼 (1: Instagram) |
platformUserId | string | Instagram Business Account ID |
username | string | Instagram 사용자명 (@없이) |
name | string | 계정 이름 |
profilePictureUrl | string | 프로필 이미지 URL |
biography | string | 소개글 |
website | string | 웹사이트 URL |
followersCount | long | 팔로워 수 |
followingCount | long | 팔로잉 수 |
mediaCount | long | 게시물 수 |
connectionStatus | SocialConnectionStatus | 연결 상태 |
priceTier | string | 가격 티어 (N/S/A/B/C 등) |
price | int | 산정 가격 |
SocialConnectionStatus
| 값 | 설명 |
|---|---|
CONNECTED | 연결됨 |
DISCONNECTED | 연결 해제됨 |
EXPIRED | 토큰 만료 |
ERROR | 오류 |
설정 (application.yml)
Instagram OAuth를 사용하려면 다음 설정이 필요합니다:
sns:
instagram:
client:
id: YOUR_FACEBOOK_APP_ID # Facebook App ID
secret: YOUR_FACEBOOK_APP_SECRET # Facebook App Secret
callback:
url: https://your-frontend.com/instagram/callback # 프론트엔드 callback URL중요: callback.url은 프론트엔드 URL이어야 합니다.
이 URL을 Facebook Developer Console의 "Valid OAuth Redirect URIs"에도 동일하게 등록해야 합니다.
Facebook Developer Console 설정
1. 앱 생성
Facebook Developer Console에서 Business 타입 앱을 생성합니다.
2. 제품 추가
- Facebook 로그인
- Instagram Graph API
3. OAuth 설정
Facebook 로그인 > 설정에서 다음을 설정:
- Valid OAuth Redirect URIs: 프론트엔드 callback URL 등록
- 개발:
http://localhost:3000/instagram/callback - 운영:
https://your-frontend.com/instagram/callback
- 개발:
4. 권한 요청
다음 권한이 필요합니다:
| 권한 | 설명 |
|---|---|
instagram_basic | Instagram 기본 정보 접근 |
instagram_manage_insights | 인사이트 데이터 접근 |
pages_show_list | Facebook 페이지 목록 조회 |
pages_read_engagement | 페이지 참여 데이터 읽기 |
business_management | 비즈니스 계정 관리 |
프론트엔드 구현 예시
React 예시
// 1. OAuth 시작
const startOAuth = async () => {
const { data } = await api.get('/ai/instagram/oauth/url', {
params: { memberId: user.id }
});
// Facebook으로 리다이렉트
window.location.href = data.data;
};
// 2. Callback 페이지 (/instagram/callback)
const InstagramCallback = () => {
const searchParams = useSearchParams();
const code = searchParams.get('code');
const state = searchParams.get('state');
const error = searchParams.get('error');
useEffect(() => {
if (error) {
// 에러 처리 (사용자가 취소한 경우 등)
alert('Instagram 연결이 취소되었습니다.');
router.push('/settings');
return;
}
if (code && state) {
connectInstagram(code, state);
}
}, [code, state, error]);
const connectInstagram = async (code: string, state: string) => {
try {
const { data } = await api.post('/ai/instagram/oauth/connect', null, {
params: { code, state }
});
// 연결 성공
alert(`${data.data.length}개의 Instagram 계정이 연결되었습니다.`);
router.push('/settings/instagram');
} catch (error) {
alert('Instagram 연결에 실패했습니다.');
router.push('/settings');
}
};
return <div>Instagram 연결 중...</div>;
};에러 처리
OAuth 에러
Facebook OAuth 과정에서 에러가 발생하면 프론트엔드 callback URL에 에러 파라미터가 전달됩니다:
https://your-frontend.com/instagram/callback?error=access_denied&error_description=User%20denied| 에러 코드 | 설명 |
|---|---|
access_denied | 사용자가 권한 허용을 거부함 |
invalid_request | 잘못된 요청 |
API 에러
| 상태 코드 | 에러 코드 | 설명 | 해결 방법 |
|---|---|---|---|
400 | INVALID_REQUEST | 필수 파라미터 누락 | code, state 확인 |
500 | CONNECTION_FAILED | 연결 실패 | 재시도 또는 관리자 문의 |