issue with WP-API authentication - wordpress

I have a wordpress page from which I am trying to create a post using the WP AJAX REST API. My expectation is that if the user is already logged in the auth cookie will be in place and I'll be able to do the AJAX POST with the user's context. Yet I'm getting an error that the user is not authorized. What am I missing? Thanks

This plugin is not kept up to date but adding a new post should work as follows:
Go to the settings -> json api and activate posts controller
get nonce
http://www.example.com/?json=core.get_nonce&controller=posts&method=create_post&callback=?
Grab the nonce from the reponse, Response should be:
?({"status":"ok","controller":"posts","method":"create_post","nonce":"38nonce84"})
4.Post a post
www.example.com/?json=posts.create_post&nonce=383nonce84&title=newpost&content=blahblabh&status=publish

Related

WP REST API - User registration without authentication

I am just starting out with using the WP REST API.
For authentication, I use JSON Web Tokens.
The only question I have is how I can give users the possibility to register by themselves, since registering a user also requires an authentication key.
Since the user has not yet logged in, this key cannot yet be retrieved.
I came up with the following two options, but cannot figure out how to do either of them.
The application itself has an authorization key with which the request can be made.
Disabling authentication requirement for user creation.
If I'm looking at this in the wrong way, any answers are welcome!
Thanks!
If your application is a web page then the easiest is to do this separately from the WordPress REST API. WordPress has a web page http://aaa.bbb.ccc/wp-login.php?action=register that allows you to register new users. To enable this web page check the Dashboard -> Settings -> General -> Membership -> 'Anyone can register' option.
If your application is a mobile app then your mobile can just sent the same HTTP request that http://aaa.bbb.ccc/wp-login.php?action=register sends. I.E. a POST request with query parameter action=register with POST parameters user_email, user_login, wp-submit=Register.
If you really insists on doing this using the REST API I think the following will work. (Disclaimer: I have not actually implemented this.)
You will need to override the WordPress REST authentication. First create a new role with the capability 'create_users'. Second create a user with this role. Create a nonce that specifies that a new user is to be registered. When your app returns this nonce and the user credentials to the http://aaa.bbb.ccc/wp-json/wp/v2/users endpoint you should override the WordPress authentication to set the current user to the user you created with the role 'create_users'.

wordpress REST API draft posts not showing

Using the Wordpress REST API I am trying to get draft posts only. However the API responds with only the published posts when I pass the query in both cases.
A: http://localhost/wp-json/wp/v2/posts?status=draft
B: http://localhost/wp-json/wp/v2/posts?filter[post_status]=draft
How can I see only the draft posts? I am using the JWT Authentication for WP-API plugin for authentication but disabling this doesn't seem to change anything.
This is because just installing the JWT Auth plugin doesn't solve the problem. There are also some modifications to be performed on the .htaccess file as well as in wp-config.php.
I hit a similar problem but with private posts, so I created a video detailing the steps you need to follow to install and configure the JWT Authentication plugin.
https://youtu.be/Mp7T7x1oxDk
for getting draft posts you should send an Authorization token, so you can use
JWT Authentication for WP-API plugin for getting it and use it anywhere,
and for getting posts with drafts status you should send 'status': 'draft' as a param in your request, also if you want to get all of status you should send 'status': 'any' in your params in request. :)

Wordpress login system for phonegap

I am building phonegap application to show post and other pages. Logged in users can only access these pages. So, I am creating login system in phonegap with wordpress api. I am using json api,json api user for api.
After username, password entered in login form authentication cookie is generated by 'generate_auth_cookie' api and stored in local storage. It is stored as 'wordpress_logged_in_5....'=>'username|1453458845|J500gDe.....'
I am struck here. Please tell me the next steps after this. Also I need to check the user is logged in on every api call.
FOR JSON API LOGIN
Method: generate_auth_cookie
First get the nonce: http://localhost/api/get_nonce/?controller=user&method=generate_auth_cookie
Then generate cookie: http://localhost/api/user/generate_auth_cookie/?username=john&password=PASSWORD-HERE
Method: validate_auth_cookie
It needs 'cookie' var.
http://localhost/api/user/validate_auth_cookie/?cookie=admin|43089754375034fjwfn39u8
so when user sent req. of login that time you need to create nonce and then gen. auth cookie and so on. and if all good then log in to user. and save it to your site. so once user click on logout. there session will be removed.
you can also set session from here
generate cookie for 1 minute: http://localhost/api/user/generate_auth_cookie/?username=john&password=PASSWORD-HERE&seconds=60
60 means 1 minute.
details
https://wordpress.org/plugins/json-api-user/other_notes/
OR
TRY - WP REST API V2
http://v2.wp-api.org/
downlaod plugin and install it.
https://wordpress.org/plugins/json-rest-api/
just type /wp-json/wp/v2/posts in end of your site url and you will get all post.
for get all post
www.mysite.com/wp-json/wp/v2/posts
For the search functionality - search test post
/wp-json/wp/v2/posts?filter[s]=test
much more details from this link http://v2.wp-api.org/reference/posts/
Note for best result use latest version of WP
so using this you can easily get post page etc.

Create post with WordPress JSON API Plugin when not logged as ADMIN

I have recently started working with WordPress & had my first task using JSON API. Now I know that after getting nonce from get_nonce method, I can create post but it is only possible if I am logged in also as an admin.
My question is: How to create post using JSON API when you aren't logged in in the WP Admin Panel?
I am receiving the data from another server in JSON format and using that I want to create a post, but due the nonce factor I can't create post if I am not logged in.

twitter oauth api with wordpress multisite?

I have a WP multisite installation with various users each having their own installation
Ie
www.mysite.com/fearghal
www.mysite.com/john
etc.
Is there a way I can use one Twitter application that will allow each user to update their status? I know with one site it is simple, but with multiple url's like WP MS - could be tricky with various callback urls.
Any ideas folks?
You can do that. When you get a request token you will have to pass the callback url for that site to Twitter and callback URL will have a verifier passed to it that will need to be sent when you get an access token. You can read more about the process at http://dev.twitter.com/pages/auth

Resources