stylight / python-wordpress-json

Thin Python wrapper for the Wordpress REST API
MIT License
42 stars 19 forks source link

No error response for invalid credentials #19

Open hygull opened 6 years ago

hygull commented 6 years ago

Today I tried to use this package. I tried to execute the below codes.

I got the same result in all 3 cases, o/p is also available at very bottom.

And this is not allowing me to figure out my issues with authentication, as dir(wp) is not containing the working methods defined to work.

CODE SNIPPET 1

from wordpress_json import WordpressJsonWrapper

# WordpressJsonWrapper('http://example.com/wp-json/wp/v2', 'wp_user', 'wp_password')
wp = WordpressJsonWrapper('http://demo4.sjainventures.com/Incredible/wp-json/wp/v2', 'myuser', \
'mypass');
print dir(wp)

CODE SNIPPET 2

from wordpress_json import WordpressJsonWrapper

# WordpressJsonWrapper('http://example.com/wp-json/wp/v2', 'wp_user', 'wp_password')
wp = WordpressJsonWrapper('http://demo4.sjainventures.com/Incredible/wp-json/wp/v2', \
'myuser1', 'mypass');
print dir(wp)

CODE SNIPPET 3

from wordpress_json import WordpressJsonWrapper

# WordpressJsonWrapper('http://example.com/wp-json/wp/v2', 'wp_user', 'wp_password')
wp = WordpressJsonWrapper('http://demo4.sjainventures.com/Incredible/', 'myuser', 'mypass');
print dir(wp)

O/P

(wordpress_api) E:\RishikeshAgrawani\projects\Python3\PyLaTeX\wordpress-api>python basic_auth.py
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattr__', '__getattribute__', '__hash__', 

'__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', 

'__subclasshook__', '__weakref__', '_build_endpoint', '_determine_method', '_expand_url_components', 

'_get_ids', '_prepare_req', '_request', 'auth', 'site']
droustchev commented 6 years ago

Hey @hygull, the wrapper is not defining all methods explicitly, but rather using __getattr__ to dynamically construct the requests. Check out line 67

How do you know you have issues with authentication? The wrapper should bubble up any errors returned from the API, which should give you more insights as to why something is not working.

What happens if you call, e.g.

wp.get_posts()

after initializing the wrapper?

hygull commented 6 years ago

Statement

@droustchev , I have set Permalinks as Post name and its value is http://demo4.sjainventures.com/Incredible/sample-post/.

I am trying to request in the following 2 ways, suggest me which one is ok.

Error

Because of json_oauth1_missing_parameter - Missing OAuth parameter oauth_token - {u'status': 401}

Python codes

from wordpress_json import *

# I should try this one
wp = WordpressJsonWrapper("http://demo4.sjainventures.com/Incredible/wp-json/wp/v2", "myuser", "mypass")
posts = wp.get_posts()
print posts

# or this one
wp = WordpressJsonWrapper("http://demo4.sjainventures.com/Incredible/sample-post/", "myuser", "mypass")
posts = wp.get_posts()
print posts

Console

I'm getting the following on screen.

CODE: 401
RESPONSE:<html>
 <body>
  <p>
   {"code":"json_oauth1_missing_parameter","message":"Missing OAuth parameter oauth_token","data":{"status":401}}
  </p>
 </body>
</html>
HEADERS: {'X-Robots-Tag': 'noindex', 'X-Content-Type-Options': 'nosniff', 'X-Powered-By': 'PHP/5.6.31', 'Transfer-Encoding': 'chunked', 'Access-Control-Expose-Headers': 'X-WP-Total, X-WP-TotalPages', 'Server': 'Apache', 'Connection': 'close', 'Link': '<http://demo4.sjainventures.com/Incredible/wp-json/>; rel="https://api.w.org/"', 'Date': 'Thu, 15 Mar 2018 04:40:44 GMT', 'Access-Control-Allow-Headers': 'Authorization, Content-Type', 'Content-Type': 'application/json; charset=UTF-8'}
REQ_BODY:None
Because of json_oauth1_missing_parameter - Missing OAuth parameter oauth_token - {u'status': 401}
droustchev commented 6 years ago

Currently, this wrapper only supports Basic Auth, not OAuth, as stated under the limitations of the repo's README.md. If you want to get it working, you'll have to download the Basic Auth plugin and install it in your WP instance.

When this wrapper was originally written, WP did not support any other authentication method other than Basic Auth. Nowadays it does and using Basic Auth in production is not advised, so keep that in mind please.

Others have previously requested to add support for other authentication methods, but we unfortunately don't have enough time to add this. Any pull requests in that regard are encouraged and welcome!