Recommanded Free YOUTUBE Lecture: <% selectedImage[1] %>

Contents

네이버 Callback URL 테스트

로그인을 성공적으로 끝내고, 네이버 코드 을 가져왔다. Joinc 사이트는 이 코드를 이용해서 네이버 회원 서버에 access token과 refresh token을 가져올 수 있다. 아래 버튼을 클릭해 보자.

Get Access Token & Access Token
Access token과 refresh token을 확인 할 수 있을 것이다. 여기에 사용한 코드는 아래와 같다.
func (self *Plugin) Function_navertoken(naver_code string) interface{} {
    const (
        GRANT_TYPE    = "authorization_code"
        CLIENT_ID     = "JOINC_CLIENT_ID"
        CLIENT_SECRET = "JOINC_CLIENT_SECRET"
        AUTH_URL      = "https://nid.naver.com/oauth2.0/token"
    )
    req, err := http.NewRequest("GET", AUTH_URL, nil)
    if err != nil {
        return Response{Code: http.StatusNonAuthoritativeInfo, Message: err.Error()}
    }

    // URL 파라메터를 설정한다.
    // grant_type, client_id, client_secret
    // 그리고 네이버 코드를 넘기면 된다.
    q := req.URL.Query()
    q.Add("grant_type", GRANT_TYPE)
    q.Add("client_id", CLIENT_ID)
    q.Add("client_secret", CLIENT_SECRET)
    q.Add("code", naver_code)

    req.URL.RawQuery = q.Encode()
    client := &http.Client{}
    res, err := client.Do(req)
    if err != nil {
        return Response{Code: http.StatusNonAuthoritativeInfo, Message: err.Error()}
    }
    defer res.Body.Close()

    // 네이버 인증 서버는 네이버 코드에 대한
    // access token, refresh_token
    // 만료시간 정보를 반환한다.
    val := struct {
        Access_token  string `josn:"access_token"`
        Refresh_token string `json:"refresh_token"`
        Token_type    string `json:"token_type"`
        Expires_in    string `json:"expires_in"`
    }{}
    json.NewDecoder(res.Body).Decode(&val)

    return Response{Code: http.StatusOK, Message: "SUCCESS", Node: val}
}
golang으로 구현했는데, curl로 바꿔봤다. 간단하다.
# curl -XGET https://nid.naver.com/oauth2.0/token?grant_type=authorization_code \
\&client_id=CLIENT_ID\&client_secret=CLIENT_SECRET\&code=zrNMbqvBGr8OOop4

{
    "access_token":"AAAAQosjWDJieBiQZc3to9YQp6HDLvrmyKC+6+iZ3gq7qrkqf50ljZC+Lgoqrg",
    "refresh_token":"c8ceMEJisO4Se7uGisHoX0f5JEii7JnipglQipkOn5Zp3tyP7dHQoP0zNKHUq2gY",
    "token_type":"bearer",
    "expires_in":"3600"
}

Access token을 이용한 OpenAPI 호출

이제 Access token을 이용해서 네이버 OpenAPI를 호출 할 수 있다. 가장 간단한 유저정보 가져오는 코드를 만들어보자.

유저 정보 가져오기

이렇게 joinc 서비스는 네이버로 로그인한 유저를 인증했다. 이제 로그인한 유저의 정보를 읽은 다음 joinc의 회원 테이블에 저장하고 세션을 발급하면 네이버 로그인 연동이 끝난다. 유저 정보(프로파일)은 access token으로 요청한다. 요청 API 주소는 https://openapi.naver.com/v1/nid/me 다.
GET v1/nid/me HTTP/1.1
Host: openapi.naver.com
User-Agent: curl/7.43.0
Accept: */*
Content-Type: application/xml
Authorization: Bearer <Naver access token> 
curl요청은 아래와 같다.
# curl -XGET https://openapi.naver.com/v1/nid/me -H "Authorization: Bearer AAAAQosjWDJieBiQZc3to9YQp6HDLvrmyKC+6+iZ3gq7qrkqf50ljZC+Lgoqrg"
{
  "resultcode": "00",
  "message": "success",
  "response": {
    "email": "openapi@naver.com",
    "nickname": "OpenAPI",
    "profile_image": "https://ssl.pstatic.net/static/pwe/address/nodata_33x33.gif",
    "age": "40-49",
    "gender": "F",
    "id": "32742776",
    "name": "오픈 API",
    "birthday": "10-01"
  }
}

자 이제 앞서 얻은 access token 으로 유저 프로파일 정보를 가져오자.