본문 바로가기

Spring Boot

[SpringBoot] Spring Boot에서 Naver Blog 글쓰기를 구현

반응형

swiftui에서 admod 소스가 필요해Naver Blog 글쓰기를 구현하기 위해서는 Naver Open API를 사용해야 합니다. Naver Open API는 네이버 계정으로 로그인한 사용자가 개인 블로그에 글을 작성할 수 있는 기능을 제공합니다.

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

public class NaverBlogPostExample {
    private static final String NAVER_OPEN_API_URL = "https://openapi.naver.com/v1/blog/{blogId}/post";

    public static void main(String[] args) {
        RestTemplate restTemplate = new RestTemplate();

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.add("X-Naver-Client-Id", "YOUR_NAVER_CLIENT_ID");
        headers.add("X-Naver-Client-Secret", "YOUR_NAVER_CLIENT_SECRET");

        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("title", "Test Title");
        map.add("contents", "Test Contents");

        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);

        String response = restTemplate.postForObject(NAVER_OPEN_API_URL, request, String.class, "YOUR_BLOG_ID");

        System.out.println(response);
    }
}

 Spring Boot의 RestTemplate을 사용하여 Naver Open API를 호출하고 있습니다. 

반응형