π Issue: WordPress REST API Authentication Failed (Error 401)
Your WordPress API request failed because the user does not have permission to create posts via the REST API.
Even though the user can create posts manually, WordPress may block REST API access due to:
- Incorrect authentication method.
- REST API disabled by a security plugin.
- WordPress not accepting application passwords.
π§ Fix 1: Enable Application Passwords (Recommended)
If your WordPress does not support basic authentication, you need to use Application Passwords.
β
Step 1: Enable Application Passwords
- Log into WordPress Admin.
- Go to Users > Your Profile.
- Scroll to "Application Passwords".
- Enter a name (e.g., Reddit Scraper) and click Generate Password.
- Copy the generated password.
β
Step 2: Update Your Script to Use the Application Password
Modify your post_to_wordpress()
function and replace:
auth=(WORDPRESS_USER, WORDPRESS_PASSWORD)
With:
auth=(WORDPRESS_USER, "your_application_password")
π Now, your function should look like this:
def post_to_wordpress(title, content):
print(f"π DEBUG: Entering `post_to_wordpress()` for: {title}")
data = {
"title": title,
"content": content,
"status": "publish" # Change to "draft" if needed
}
try:
response = requests.post(
WORDPRESS_URL,
json=data,
auth=(WORDPRESS_USER, "your_application_password"), # β
Use Application Password
verify=False # β
Bypass SSL temporarily if needed
)
if response.status_code == 201:
print(f"β
Successfully posted to WordPress: {title}")
return True
else:
print(f"β WordPress Error: {response.text}")
return False
except Exception as e:
print(f"β ERROR in `post_to_wordpress()`: {e}")
return False
π§ Fix 2: Install Basic Authentication Plugin (Alternative)
If Fix 1 doesnβt work, install the Basic Authentication plugin:
Download & Install Plugin:
- Go to Plugins > Add New.
- Search for: "Basic Auth".
- Install "Application Passwords for REST API" (by George Stephanis).
- Activate it.
Use Regular Login Credentials (No need for application passwords now).
Run the Script Again:
python C:/Users/Administrator/AppData/Local/Programs/Python/Python313/Scripts/SEO-PRAW.py
π§ Fix 3: Check If a Security Plugin is Blocking the REST API
Some plugins block the REST API for security reasons. To check:
Try opening this in a browser:
https://hubs.uk/wp-json/wp/v2/posts
- If you get a 401 error, a plugin is blocking it.
- If you see JSON data, the API is working.
Disable plugins like:
- Wordfence
- iThemes Security
- All In One WP Security
- Disable REST API Plugin
π Next Steps
1οΈβ£ Try Fix #1 (Use Application Passwords).
2οΈβ£ If still failing, try Fix #2 (Install Basic Authentication Plugin).
3οΈβ£ Check if security plugins are blocking REST API (Fix #3).
4οΈβ£ Run the script again and check WordPress for posts! ππ₯
Let me know what happens! π―