• Resolved Scott Bolinger

    (@scottopolis)


    Hi, thanks for this plugin!

    I have no trouble generating the cookie and getting the user info, but I can’t seem to create a post through the api/create_post/ method. It keeps giving me the error: You need to login with a user that has ‘edit_posts’ capacity.

    The user/pass I’m using is an admin user with the proper capabilities.

    Here’s my code:

    // already got the nonce and cookie, use it to create post

    $.ajax({
    type: ‘POST’,
    url: ‘http://mysite.com/api/create_post/’,
    data: { cookie: app.authCookie, nonce: app.nonce, title: $(‘#post_title’).val(), content: $(‘#post_content’).val(), status: ‘publish’ },
    success: function(data) {
    console.log(data);
    }
    });

    I’d really appreciate some help, thanks!

    https://wordpress.org/plugins/json-api-auth/

Viewing 15 replies - 1 through 15 (of 27 total)
  • Plugin Author Ali Qureshi

    (@parorrey)

    Issue is that JSON API create_post check the current_user which is logged in and that can’t be done via REST.

    for that, you need to send the ‘cookie’ var.

    To fix it, open json-api/controllers/posts.php

    Find:

    if (!current_user_can(‘edit_posts’)) {
    $json_api->error(“You need to login with a user that has ‘edit_posts’ capacity.”);
    }

    Replace it with:

    if (!$json_api->query->cookie) {
    $json_api->error(“You must include a ‘cookie’ var in your request. Use the generate_auth_cookie Auth API method.”);
    }

    And you will also have to provide ‘author’, so add following too underneath above code:

    if (!$json_api->query->author) {
    $json_api->error(“You must include ‘author’ var in your request.”);
    }

    To set the author id for new post:

    Find:

    $post = new JSON_API_Post();

    Add below:

    $post->set_author_value($json_api->query->author);

    That will allow you to create post with valid cookie for any user.

    I have created another plugin for doing all this user related stuff which is not doable via JSON-API. get_messages, send_message, get_msg_count, create post, add_comment, and some more.

    I will release them in my other plugin JSON-API-User soon.

    Hope that helps!

    @ali this method is not working, any idea why?

    Plugin Author Ali Qureshi

    (@parorrey)

    Please mention what is not working? and what are you trying to achieve?

    Rather than editing the json-api code, I was able to get the ‘create_post’ json api call working by doing the following:

    1. Determine my site logged in cookie name, something like: wordpress_logged_in_09451d069f48070a06dbaa1d519fd5b8. It seems that this does not change for your site.

    2. get nonce for ‘generate_auth_cookie’, eg:
    curl -vd “controller=auth&method=generate_auth_cookie” “http://yourhost.com/wordpress/?json=get_nonce”

    3. generate the auth cookie, eg:
    curl -vd “nonce=nonceValFromAbove&username=username&password=pass” “http://yourhost.com/wordpress/?json=auth.generate_auth_cookie”

    4. get nonce for ‘create_posts’, eg:
    COOKIE_HEADER=wordpress_logged_in_09451d069f48070a06dbaa1d519fd5b8
    curl -b “$COOKIE_HEADER=cookieValueFromAbove” -vd “controller=posts&method=create_post” “http://yourhost.com/wordpress/?json=get_nonce”

    5. create post, eg:
    COOKIE_HEADER=wordpress_logged_in_09451d069f48070a06dbaa1d519fd5b8
    curl -b “$COOKIE_HEADER=cookieValueFromAbove” -vd “nonce=nonceVal&title=TestTitle&content=TestContent&author=usernameForCookie” “http://yourhost.com/wordpress/?json=create_post”

    Shijo64

    (@shijo64)

    Hi, Im trying to use this api in WordPress and an IOS application to send and receive posts, but when Im trying to create a post I get this error, can somebody help me?

    Hi, I’m trying to create a post from mobile app. But no way. I’ve the same problem. When I try all solutions suggested, I got a new error : “Your ‘nonce’ value was incorrect. Use the ‘get_nonce’ API method.”

    Ali, you talk about another plugin ! Is there available ?

    Any help would be welcome.
    Thanks

    I believe the plugin he mentions is this:

    https://wordpress.org/plugins/json-api-user

    However, using the json-api plugin, you should be able to create a post, but you must first generate a nonce value, as I indicated in my post above, are you doing that? Perhaps you can share the api hits you are making and I can help pinpoint your trouble.

    Thanks…yes, i believe too. I’m trying to connect my app mobile to wordpress. So i’m using json api plugin and php script. In my script, i’m sending two json url (curl).

    I’m sending requests below:

    1-
    http://localhost/api/get_nonce/?controller=posts&method=create_post

    2-
    http://localhost/api/create_post/?nonce=e04af86eec&author=iScot&title=Test&content=Hello%20Mc%20Fly&status=publish

    But it’s not working. I get this error : You need to login with a user that has ‘edit_posts’ capacity

    When I try to make changes as mentionned higher, i got this error : Your ‘nonce’ value was incorrect. Use the ‘get_nonce’ API method.

    Hope to be clear enough !

    WordPress requires that a user be logged in in order to create a post. The edit suggested by Ali is not required for the steps I indicated to work.

    In the json api, login done by the ‘generate_auth_cookie’ call I indicated in step #3 of my post above.

    One thing I wasn’t clear on was in my step #1 above, how to get the “site logged in cookie name”. To get this, you can use your web browser to login to your wordpress site. Then, view your cookies, and look for a cookie with a ‘name’ as I indicated above.

    Please try all 5 steps as I indicated above and let me know if you still have trouble.

    Plugin Author Ali Qureshi

    (@parorrey)

    First question, but a silly one.. does your user has create posts rights? try with admin user.

    tfischer203 : I do not find that information. Is this a fixed value or reset periodically ?

    Ali I guess Yes. I check it several time ! I also tried with administrator. There is no problem with user capability

    Plugin Author Ali Qureshi

    (@parorrey)

    issue with JSON API plugin is that it uses session var to authenticate. if you try and use ‘Advanced REST client’ extension for chrome to create_post, it won;t allow you because you are not providing cookie value or anything for the server to authenticate if you are allowed or not. But if you try the endpoint in browser with logged in already, then create_post works.

    I will do a detailed testing tomorrow and provide you a solution or tell you how it can be done. The best thing would be to add create_post endpoint in json api user plugin.

    Thank you Ali. I’m trying to create a post from my mobile app android (client side). Server side (wordpress) is hosted on a Godaddy server. I’m not using a browser. Mobile app communicate with backend from PHP & Json Api.

    iScot, I believe the logged in cookie name is based on your site name, and will not change unless you change your site name. More info:

    http://codex.wordpress.org/WordPress_Cookies#WordPress_.3E_3.0

    Also, how to find cookies in your web browser:
    https://kb.iu.edu/d/ajfi

    The name of the cookie is all you want, and should look something like ‘wordpress_logged_in_68548322…’.

    -Tom

Viewing 15 replies - 1 through 15 (of 27 total)
  • The topic ‘Error: You need to login with a user that has 'edit_posts' capacity.’ is closed to new replies.