Problem with WordPress API (401 code)

ciphercipher1

Power Member
Joined
Jul 26, 2022
Messages
530
Reaction score
181
Hello guys so I am trying to build an auto uploader that will send HTML content and make it a post

However I am struggling to establish connection with the site. I am providing correct credentials but i still get 401 error. This is my python code


site_url = 'https://sharmaharda.000webhostapp.com/wp-json/wp/v2/'
#authentication part
wordpress_user = "admin"
wordpress_password =
wordpress_credentials = wordpress_user + ':' + wordpress_password
wordpress_token = base64.b64encode(wordpress_credentials.encode())
wordpress_header = {'Authorization': 'Basic' + wordpress_token.decode('utf-8')}
response = requests.get(site_url + 'posts' , verify=False)
if response.status_code == 200:
data = response.json()
print("Connection successful")

for post in data:
print("Title: ", post['title']['rendered'])
else:
print("Connection failed with status code:", response.status_code)


I have added the following to my .htaaccess file :

RewriteEngine on
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]


Please take a look and point me in the right direction.
 
Last edited by a moderator:
Use the Wordpress API to post to your site, this is by far the easiest way. Google how to do that for whatever language you want to use. Also, add the Wordpress API plugin first and enable Basic Authentication.

And there is a separate plugin that will turn off the API for users that are not logged in, use that too if you want to.
 
Use the Wordpress API to post to your site, this is by far the easiest way.
That's exactly what he is trying to do there. Read the OP first I guess?

Btw, OP you have exposed your admin password there. Perhaps change it as soon as you can.

Edit: For the actual problem, try this:
Code:
https://stackoverflow.com/a/72335498/1437261
Looks like you can send a basic auth header (without the encoding part) to get it working. For the one you are trying, you probably need some specific plugin.
 
Last edited:
Use the Wordpress API to post to your site, this is by far the easiest way. Google how to do that for whatever language you want to use. Also, add the Wordpress API plugin first and enable Basic Authentication.

And there is a separate plugin that will turn off the API for users that are not logged in, use that too if you want to.
thats what I am trying to do in this coode
That's exactly what he is trying to do there. Read the OP first I guess?

Btw, OP you have exposed your admin password there. Perhaps change it as soon as you can.

Edit: For the actual problem, try this:
Code:
https://stackoverflow.com/a/72335498/1437261
Looks like you can send a basic auth header (without the encoding part) to get it working. For the one you are trying, you probably need some specific plugin.
this is not a site I am worried about it is a test one because i tried developing in local env but still got the same results. thanks anyways
 
Yeah, I kinda skimmed the post.

I have it working 100% in C#. Again, install the "WordPress REST API Authentication" plugin first and configure it to enable Basic Authentication. And then run your code, I couldn't get it to work before isntalling the plugin, didn't really dig too deep why.

And the payload (C# code):

var payload = new
{
title = post.Title,
content = contentBuilder.ToString(),
status = "publish", // or "draft" if you want to save as a draft
featured_media = attachmentId,
categories = new int[] { categoryId },
};

Youc can skip "featured_media" and "categories" if you don't have any.

And you are making a get request? It should be post (C#):
var response = await client.PostAsync("wp-json/wp/v2/posts", content);
 
Hello guys so I am trying to build an auto uploader that will send HTML content and make it a post

However I am struggling to establish connection with the site. I am providing correct credentials but i still get 401 error. This is my python code


site_url = 'https://sharmaharda.000webhostapp.com/wp-json/wp/v2/'
#authentication part
wordpress_user = "admin"
wordpress_password =
wordpress_credentials = wordpress_user + ':' + wordpress_password
wordpress_token = base64.b64encode(wordpress_credentials.encode())
wordpress_header = {'Authorization': 'Basic' + wordpress_token.decode('utf-8')}
response = requests.get(site_url + 'posts' , verify=False)
if response.status_code == 200:
data = response.json()
print("Connection successful")

for post in data:
print("Title: ", post['title']['rendered'])
else:
print("Connection failed with status code:", response.status_code)


I have added the following to my .htaaccess file :

RewriteEngine on
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]


Please take a look and point me in the right direction.
It seems like you're trying to interact with the WordPress REST API to create a post on a WordPress site. The 401 error you're encountering is likely due to authentication issues.

Here's a modified version of your code with some suggestions:

import requests
import base64

site_url = 'https://sharmaharda.000webhostapp.com/wp-json/wp/v2/'
wordpress_user = "admin"
wordpress_password = "your_password_here" # Make sure to provide your actual password

# Create your authentication token
wordpress_credentials = f"{wordpress_user}:{wordpress_password}"
wordpress_token = base64.b64encode(wordpress_credentials.encode()).decode('utf-8')

# Set the headers properly
wordpress_header = {'Authorization': 'Basic ' + wordpress_token}

# Make a POST request to create a new post, for example
new_post = {
"title": "Your Post Title",
"content": "Your post content here",
}

response = requests.post(site_url + 'posts', json=new_post, headers=wordpress_header)

if response.status_code == 201:
print("Post created successfully")
else:
print("Failed to create a post with status code:", response.status_code)
print(response.text)

A few important things to note:
  1. Ensure you provide your actual password in wordpress_password.
  2. Make a POST request to create a new post. Your code was making a GET request to retrieve posts.
  3. The code provided will attempt to create a new post with the given title and content. You can modify the new_post dictionary to include other fields you need.
This code should help you create a post on your WordPress site. If you still encounter issues, double-check your credentials, and ensure that the URL and endpoints are correct.
 
If my memory serves, you need to install this auth plugin from the official website (attached it to this message)
 

Attachments

Back
Top