const blogId = ''; // Ganti dengan ID blog Anda const refreshToken = ''; // Ganti dengan refresh token Anda const clientId = ''; // Ganti dengan Client ID Anda const clientSecret = ''; // Ganti dengan Client Secret Anda let accessToken = ''; // Variabel untuk menyimpan token akses async function getAccessToken() { const url = 'https://oauth2.googleapis.com/token'; const bodyData = new URLSearchParams({ client_id: clientId, client_secret: clientSecret, refresh_token: refreshToken, grant_type: 'refresh_token' }); try { const response = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: bodyData }); if (!response.ok) { throw new Error(`Error: ${response.statusText}`); } const data = await response.json(); accessToken = data.access_token; // Simpan access token console.log('Access token obtained:', accessToken); } catch (error) { console.error('Failed to obtain access token:', error); } } async function uploadPost(title, content) { await getAccessToken(); // Dapatkan access token sebelum upload const url = `https://www.googleapis.com/blogger/v3/blogs/${blogId}/posts/`; const postData = { kind: "blogger#post", title: title, content: content, status: "live" // Untuk menerbitkan segera }; try { const response = await fetch(url, { method: 'POST', headers: { 'Authorization': `Bearer ${accessToken}`, 'Content-Type': 'application/json' }, body: JSON.stringify(postData) }); if (!response.ok) { throw new Error(`Error: ${response.statusText}`); } const data = await response.json(); console.log('Post uploaded successfully:', data); } catch (error) { console.error('Failed to upload post:', error); } } // Menggunakan fungsi uploadPost const postTitle = 'Judul Postingan Anda'; const postContent = '
Ini adalah konten dari postingan Anda.
'; // Gunakan HTML untuk konten uploadPost(postTitle, postContent);