board logo
Posted by: drdread - 14 February 2025 19:14:25
WordPress Error: {"code":"rest_cannot_create","message":"Sorry, you are not allowed to create posts as this user.","data":{"status":401}}' even though that user definitely can and I have checked the login details

Posted by: sirclesadmin - 14 February 2025 19:14:56

πŸš€ 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:

  1. Incorrect authentication method.
  2. REST API disabled by a security plugin.
  3. 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

  1. Log into WordPress Admin.
  2. Go to Users > Your Profile.
  3. Scroll to "Application Passwords".
  4. Enter a name (e.g., Reddit Scraper) and click Generate Password.
  5. 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:

  1. Download & Install Plugin:

    • Go to Plugins > Add New.
    • Search for: "Basic Auth".
    • Install "Application Passwords for REST API" (by George Stephanis).
    • Activate it.
  2. Use Regular Login Credentials (No need for application passwords now).

  3. 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:

  1. 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.
  2. 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! 🎯