1

I am having a very odd issue that I cannot seem to solve:

I have set up the REST API and an Oauth1 server on my wordpress site.

I had previously been able to use the python requests library to connect to the server and upload new posts, however, I revisted the code 2 months later and it now doesn't work. The error response says: b'{"code":"json_oauth1_signature_mismatch","message":"OAuth signature does not match","data":{"status":401}}'

I can connect to the Oauth1 server and upload new posts to my site via the Postman HTTP request builder, so it seems that the site is not the problem.

The code cannot be the problem either as I have not changed it in the past 2 months since it was last working well. Also I have tested it on a locally hosted wordpress installation and it works fine.

A stripped down version of the code looks like this:

import requests
from requests_oauthlib import OAuth1

OAuth_credentials = {
'client_key': 'X',
'client_secret': 'X',
'resource_owner_key': 'X',
'resource_owner_secret': 'X'
}

auth = OAuth1(*OAuth_credentials.values())

headers = {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
}

data = {
    'title': 'title'
}

response = requests.request(
    'POST',
    'https://www.X.com/wp-json/WP/v2/posts/',
    headers=headers,
    auth=auth,
    json = data
)

It seems so strange to me that Postman is working but the python requests library isn't working. I can only think that I must have changed something on my site such as htaccess, but I have had no luck with that line of enquiry.

My .htaccess looks like this:

# BEGIN HTTPS Redirection Plugin
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
RewriteBase /
RewriteRule ^index\.php$ - [E=X-HTTP_AUTHORIZATION:%{HTTP:Authorization},QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [E=X-HTTP_AUTHORIZATION:%{HTTP:Authorization},QSA,L]
</IfModule>
# END HTTPS Redirection Plugin


# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Can anyone suggest what the problem might be?

1 Answers1

1

I have resolved this now. I found that for some reason wordpress didn't like the requests user-agent in the http request. I change it and now it works.