OAuth를 활용한 Blogger 자동 글쓰기 테스트
OAuth 2.0을 이용하여 Blogger에 자동으로 글을 게시하는 방법을 테스트합니다
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
# OAuth 2.0 클라이언트 ID 파일 경로
CLIENT_SECRET_FILE = "C:/Users/User/Desktop/kdrcoop-auto/client_secret.json" # JSON 파일 경로 수정
SCOPES = ["https://www.googleapis.com/auth/blogger"] # 필요한 권한 범위 설정
# OAuth 2.0 인증
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
credentials = flow.run_local_server(port=0)
# Blogger API 서비스 생성
service = build("blogger", "v3", credentials=credentials)
# Blogger에 게시할 함수 정의
def post_to_blogger(blog_id, title, content):
post = {
"kind": "blogger#post",
"title": title,
"content": content
}
try:
posts = service.posts()
result = posts.insert(blogId=blog_id, body=post, isDraft=False).execute()
print(f"✅ 성공적으로 게시되었습니다! 글 ID: {result.get('id')}")
except Exception as e:
print(f"❌ 오류가 발생했습니다: {e}")
# 'posts.txt'에서 내용 읽기
def get_content_from_file(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
return file.read()
# 'posts.txt' 경로 설정
file_path = "C:/Users/User/Desktop/kdrcoop-auto/posts.txt" # posts.txt 경로 확인
# 자동 글쓰기 실행
title = "자동 글쓰기 테스트: 쪽갈비 창업의 장점"
content = get_content_from_file(file_path) # posts.txt에서 내용 읽기
# Blogger 블로그 ID 설정 (본인의 블로그 ID로 변경)
blog_id = "5472077345069793872"
# 게시 함수 호출
post_to_blogger(blog_id, title, content)
자동글쓰기 파이썬으로 구조를 수정을 거듭해서 가능하게 만들었네..
답글삭제