」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 使用 WordPress API 的綜合指南:身份驗證和後期調度

使用 WordPress API 的綜合指南:身份驗證和後期調度

發佈於2024-11-17
瀏覽:778

A Comprehensive Guide to Using the WordPress API: Authentication and Post Scheduling

在本指南中,我們將探討如何使用 WordPress API 進行驗證並安排特定發佈時間的貼文。這些步驟將幫助您以程式設計方式安全地管理您的 WordPress 內容。

使用 WordPress API 進行身份驗證

要安全地與 WordPress API 交互,您需要對您的要求進行身份驗證。讓我們深入研究兩種常見的方法:

應用程式密碼

應用程式密碼是 WordPress 中的內建功能,可讓您產生用於 API 存取的安全密碼,而不會洩露您的主帳戶密碼。

  1. 登入您的 WordPress 管理儀表板。
  2. 導覽至使用者 → 個人資料
  3. 向下捲動到「應用程式密碼」部分。
  4. 輸入應用程式的名稱(例如「API Access」)。
  5. 點選「新增新的應用程式密碼」。
  6. 複製產生的密碼(您將無法再次看到它)。

使用應用程式密碼:


import requests

url = "https://your-wordpress-site.com/wp-json/wp/v2/posts"
username = "your_username"
app_password = "your_application_password"

headers = {
"Content-Type": "application/json"
}

response = requests.get(url, auth=(username, app_password), headers=headers)




基本驗證插件

對於較舊的 WordPress 版本或如果您喜歡替代方法:

  1. 從 WordPress.org GitHub 儲存庫下載基本驗證外掛程式。
  2. 在您的 WordPress 網站上安裝並啟用該外掛程式。
  3. 使用您的常規 WordPress 使用者名稱和密碼進行身份驗證。

import requests

url = "https://your-wordpress-site.com/wp-json/wp/v2/posts"
username = "your_username"
password = "your_password"

headers = {
"Content-Type": "application/json"
}

response = requests.get(url, auth=(username, password), headers=headers)




在特定時間發布貼文

若要安排貼文在特定時間發布,請在建立或更新貼文時使用日期參數。方法如下:

建立預定帖子


import requests
from datetime import datetime, timedelta

url = "https://your-wordpress-site.com/wp-json/wp/v2/posts"
username = "your_username"
app_password = "your_application_password"

# Schedule the post for 2 days from now at 10:00 AM
scheduled_time = datetime.now() timedelta(days=2)
scheduled_time = scheduled_time.replace(hour=10, minute=0, second=0, microsecond=0)
scheduled_time_str = scheduled_time.isoformat()

data = {
"title": "Scheduled Post Example",
"content": "This is the content of the scheduled post.",
"status": "future",
"date": scheduled_time_str
}

response = requests.post(url, auth=(username, app_password), json=data)

if response.status_code == 201:
print("Post scheduled successfully!")
else:
print("Error scheduling post:", response.text)




更新現有貼文的時間表

要重新安排現有帖子,您需要其帖子 ID:


import requests
from datetime import datetime, timedelta

post_id = 123 # Replace with the actual post ID
url = f"https://your-wordpress-site.com/wp-json/wp/v2/posts/{post_id}"
username = "your_username"
app_password = "your_application_password"

# Reschedule the post for 1 week from now at 2:00 PM
new_scheduled_time = datetime.now() timedelta(weeks=1)
new_scheduled_time = new_scheduled_time.replace(hour=14, minute=0, second=0, microsecond=0)
new_scheduled_time_str = new_scheduled_time.isoformat()

data = {
"status": "future",
"date": new_scheduled_time_str
}

response = requests.post(url, auth=(username, app_password), json=data)

if response.status_code == 200:
print("Post rescheduled successfully!")
else:
print("Error rescheduling post:", response.text)




重要提示

  • 確保您的 WordPress 網站使用 HTTPS 進行安全通訊。
  • 妥善保管您的應用程式密碼或常規密碼,切勿分享。
  • 日期參數應採用 ISO 8601 格式 (YYYY-MM-DDTHH:MM:SS)。
  • WordPress API 使用 UTC 時間,因此請相應地調整您的計劃時間。
  • 將預定貼文的貼文狀態設定為「未來」。
  • 您也可以使用date_gmt參數直接指定GMT/UTC時間。

透過遵循本指南,您應該能夠使用 WordPress API 進行身份驗證,並以程式設計方式安排特定發佈時間的貼文。

引用:

  1. 驗證 – REST API 手冊 | Developer.WordPress.org
  2. WordPress REST API:如何存取、使用和保護它(完整教學)
  3. WordPress REST API 驗證 – WordPress 外掛 | WordPress.org
  4. WordPress API 基礎知識初學者指南 - GetDevDone 部落格
  5. WP REST API 以及如何保護它 | WordPress Rest API
  6. WordPress REST API 驗證 | WordPress 外掛
版本聲明 本文轉載於:https://dev.to/bramburn/a-comprehensive-guide-to-using-the-wordpress-api-authentication-and-post-scheduling-27me?1如有侵犯,請洽study_golang@163 .com刪除
最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3