hyeonga_code

[JAVA/SPRING] GetToken()_포트원/아임포트 결제 적용 IamportClient<> 이해하기 본문

Spring

[JAVA/SPRING] GetToken()_포트원/아임포트 결제 적용 IamportClient<> 이해하기

hyeonga 2024. 2. 15. 07:59
반응형

2024.02.14

portone에서 제공하는 github의 IamportRestTest.java

https://github.com/iamport/iamport-rest-client-java/blob/master/src/test/java/com/siot/IamportRestClient/IamportRestTest.java

 

직접 실행해보면서 어떻게 돌아가는지 확인해봄

 

-- GetToken()

---- IamportAPI로부터 AccessToken을 받아오는 메소드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    public void testGetToken() {
        System.err.println(">>> testGetToken");
        client = new IamportClient("[ REST API Key ]"" [ Rest API Secret ] ");
 
        try {
            IamportResponse<AccessToken> auth_response = client.getAuth();
            System.err.println("getMessage : " + auth_response.getMessage());
            System.err.println("getResponse : " + auth_response.getResponse());
            
            AccessToken result = auth_response.getResponse();
            System.err.println("___getToken : " + result.getToken());
            System.err.println("___getClass : " + result.getClass());
        } catch (IamportResponseException e) {
            System.err.println(e.getMessage());
 
            switch (e.getHttpStatusCode()) {
                case 401:
                    System.err.println("401");
                    break;
                case 500:
                    System.err.println("500");
                    break;
            }
        } catch (IOException e) {
            //서버 연결 실패
            e.printStackTrace();
        }

 

@@ 실행결과

>>> testGetToken
getMessage : null
getResponse : com.siot.IamportRestClient.response.AccessToken@2f0d43d0
___getToken : ad8afd471dbc6ab0bb43c238f095c3ceac41ae8d
___getClass : class com.siot.IamportRestClient.response.AccessToken


// AccessToken.class

package com.siot.IamportRestClient.response;

import com.google.gson.annotations.SerializedName;

public class AccessToken {

	@SerializedName("access_token")
	String token;
	
	@SerializedName("expired_at")
	int expired_at;
	
	@SerializedName("now")
	int now;

	public String getToken() {
		return this.token;
	}
}

 

반응형