Part 3. API๋ ๋ฌด์์ธ๊ฐ
๋ณธ ํฌ์คํ ์ ํจ์คํธ์บ ํผ์ค(FastCampus)์ ๋ฐ์ดํฐ ์์ง๋์ด๋ง ์ฌ์ธ์ ํจํค์ง Online์ ์ฐธ๊ณ ํ์์ต๋๋ค.
1. API ์ ์
- Application Programming Interface
- ๋ ๊ฐ์ ์์คํ ์ด ์๋ก ์ํธ์์ฉํ๊ธฐ ์ํ ์ธํฐํ์ด์ค(๋ฐ์ดํฐ ์ฃผ๊ณ ๋ฐ๊ธฐ!)
- ์ผ๋ฐ์ ์ผ๋ก API๋ REST API๋ฅผ ์ง์นญํ๋ค.
- ex) Web API: ์น์ ํตํด ์ธ๋ถ ์๋น์ค๋ค๋ก๋ถํฐ ์ ๋ณด๋ฅผ ๋ถ๋ฌ์ค๋ API
2. API ์ ๊ทผ ๊ถํ
- Authentication: Identity๊ฐ ๋ง๋ค๋ ์ฆ๋ช
- Authorization: API๋ฅผ ํตํ ์ด๋ ํ ์ก์ ์ ํ์ฉ
- ๋์ ๋ค๋ฅด๋ค! Athentication์ ํ์๋ค๊ณ ํ๋๋ผ๋ Authorization์ ํ์ฉํ์ง ์์ ์ ์๋ค!
- Security ์ด์๊ฐ ์ค์ํ๋ค.
API Key?
- ๋ณดํต
Request URLํน์Request Header์ ํฌํจ๋๋ ๊ธด ์คํธ๋ง - ex) Google Maps Platform > Geocoding API
- https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=
YOUR_API_KEY - ์ฌ๊ธฐ์
YOUR_API-KEY์ด๋ถ๋ถ์ ์ฑ์์ฃผ์ง ์์ผ๋ฉด, request denied๊ฐ ๋จ๊ฒ ๋๋ค.
- https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=
Baisc Auth
- username:password์ ๊ฐ์ credential์ Base64๋ก ์ธ์ฝ๋ฉํ ๊ฐ์
Request Header์์ ํฌํจ
OAuth 2.0
- End User <=> My App <=> Server(ex. Spotify)
- (1) App์์ End Userํํ ์์ผ, ์ ํ๋ฒํธ, ํ๋ ์ด๋ฆฌ์คํธ์ ๊ฐ์ ์ ๋ณด๋ฅผ ๊ฐ์ ธ๊ฐ๋ ๊ฒ์ ๋ํด์ ๋์๋ฅผ ๋ฐ๋๋ค.
- (2) ๋์์๋ฅผ ๋ฐ์์์ผ๋ Serverํํ API ์์ฒญ์ ํ๊ณ ๊ทธ์ ๋ง๋ ๋ฐ์ดํฐ๋ฅผ ์๊ตฌํ๋ค.
3. Spotify Web API
- Spotify ์ง์ ๋ฐฉ๋ฌธํด๋ณด๊ธฐ
4. Endpoints & Methods
- Resource: API๋ฅผ ํตํด ๋ฆฌํด๋ ์ ๋ณด
- Endpoint: Resource ์์๋ ์ฌ๋ฌ ๊ฐ์ Endpoints๊ฐ ์กด์ฌ
- Method: ์์ ์ ๊ทผ์ ํ์ฉ๋ ํ์(GET, POST, PUT, DELETE)
| Method | Action |
|---|---|
| GET | ํด๋น ๋ฆฌ์์ค๋ฅผ ์กฐํํ๊ณ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์จ๋ค. |
| HEAD | ์๋ต์ฝ๋์ HEAD๋ง ๊ฐ์ ธ์จ๋ค. |
| POST | ์์ฒญ๋ ๋ฆฌ์์ค๋ฅผ ์์ฑํ๋ค. |
| PUT | ์์ฒญ๋ ๋ฆฌ์์ค๋ฅผ ์ ๋ฐ์ดํธ ํ๋ค. |
| DELETE | ์์ฒญ๋ ๋ฆฌ์์ค๋ฅผ ์ญ์ ํ๋ค. |
5. Parameters
- Parameters: Endpoint๋ฅผ ํตํด Requestํ ๋ ๊ฐ์ด ์ ๋ฌํ๋ ์ต์ ๋ค
| Type | ๋ด์ฉ |
|---|---|
| Header | Request Header์ ํฌํจ. ์ฃผ๋ก Authorization์ ๊ด๋ จ |
| Path | Query String(?) ์ด์ ์ Endpoint Path ์์ ํฌํจ. ex) id |
| Query String | Query STring(?) ์ดํ์ ํฌํจ. ex) ?utm_source=facebook&… |
| Request Body | Request Body ์์ ํฌํจ. ์ฃผ๋ก JSON ํํ |