본문 바로가기
해외 결제 - API/PayPal

[PayPal] Authentication - OAuth 2.0 Get access token

by Johnny's 2023. 6. 30.

PayPal - Access token 발급 받는 방법

PayPal REST API는 Oauth 2.0 방식을 따른다. Access token을 발급 받기 위해서는 CLIENT_ID, CLIENT_SECRET 가 필요하다. 개발자 대시보드에서 확인할 수 있다.

 

 

개발자 대시보드 화면

당연히 Client ID와 Secret이 바로 생성되어있는 것은 아니고, 우측 Create App을 만들면 생성된다.

node 환경에서 axios를 활용해서 API를 호출해보자

client_id:client_secret를 base64로 인코딩을 해줘야하는데 npm btoa를 사용했다.

const axios = require('axios');
const btoa = require('btoa');
require('dotenv').config();

const getAccessToken = () => {

    let authorization = 'Basic ' + btoa(`${process.env.CLIENT_ID}:${process.env.CLIENT_SECRET}`);
    let headers = {
        'Content-Type' : 'application/x-www-form-urlencoded',
        'Authorization' : authorization
    };
    
    return axios({
        method: 'POST',
        url: 'https://api-m.paypal.com/v1/oauth2/token',
        headers: headers,
        data : {
            grant_type: 'client_credentials'
        }
    
    }).then((response) => {
        console.log("response", response.data);
    }).catch((err) => {
        console.log(err);
    });

}

getAccessToken();

Response

access token을 발급받을 수있다. 보통 Oauth 2.0 방식에서 Access token 과 Refresh token을 같이 발급을 해주는데 PayPal REST API는 Access token만 발급해준다. 전문 맨 마지막에 나와 있듯이 토큰 유효기간이 만료되면,  /v1/oauth2/token 로 다시 호출하여 새로 발급받으면 된다. 참고로 Access token 유효기간은 9시간이다.

* 참고

- PayPal Devloper Authentication

댓글