Blogger API를 사용해 블로그 글을 작성하는 방법

반응형

Blogger API를 사용해 블로그 글을 작성하는 방법

 

Blogger API를 사용해 블로그 글을 작성하는 방법은 다음과 같습니다. 이 예제에서는 Python 언어와 Google API Client Library for Python을 사용하여 작성합니다. 먼저 API 클라이언트 라이브러리를 설치해야 합니다.

pip install --upgrade google-api-python-client

다음으로, OAuth 2.0 인증을 설정해야 하는데, 이렇게 하려면 Google Developers Console에서 프로젝트를 만들고 Blogger API를 활성화해야 합니다. API 인증 정보를 다운로드하여 'client_secret.json' 파일로 저장합니다. 이제 다음 코드를 사용하여 블로그에 새로운 글을 작성해볼 수 있습니다.

python
import os
from google.oauth2 import service_account
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError

def get_credentials():
#본인의client_secret.json파일 경로 지정
client_secret_path = 'path/to/client_secret.json'

#필요한 스코프 지정
scopes = ['https://www.googleapis.com/auth/blogger']

# OAuth2인증 플로우 진행
flow = InstalledAppFlow.from_client_secrets_file(client_secret_path, scopes)
credentials = flow.run_local_server(port=0)

return credentials

def create_post(your_blog_id, title, content):
try:
# API인증 및 블로거 서비스 빌드
credentials = get_credentials()
service = build('blogger', 'v3', credentials=credentials)

#블로그 글 작성 요청
body = {
'kind': 'blogger#post',
'title': title,
'content': content,
}

request = service.posts().insert(blogId=your_blog_id, body=body)
response = request.execute()

print('Post ID: %s' % response['id'])

except HttpError as error:
print("An error occurred: %s" % error)
response = None

return response

if __name__ == '__main__':
your_blog_id = "your_blog_id"
title = "블로그 글 제목"
content = "<h1>Hello, Blogger API!</h1><p>이 글은Blogger API를 사용하여 작성되었습니다.</p>"

create_post(your_blog_id, title, content)

위 코드에서 본인의 블로그 ID, 글 제목 및 글 내용을 지정하십시오. 'your_blog_id'를 자신의 블로그 ID로 대체합니다. 'title'과 'content' 변수로 새로운 글의 제목과 내용을 지정합니다. 애플리케이션을 실행한 다음 출력된 인증 URL을 사용하여 인증 프로세스를 완료하고 블로그에 글이 작성되었는지 확인합니다. 다른 프로그래밍 언어를 사용하여 Blogger API를 사용하는 방법에 대한 자세한 내용은 Google API 개발자 도움말을 참조하십시오.

  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유