• Please help, I have tried everything I can. I need a working example to create a new post.

    I even asked on Stakoverflow, but no one helped. I feel I am doing something very stupid.

    I have even tried wp_remote_post like in this solution:
    http://stackoverflow.com/questions/37243266/wordpress-rest-api-how-to-post

    I want to post on my wordpress blog remotely using curl. (or PHP, Python, Perl)

    I am using this curl command to post to wordpress

    
    curl -D- -X POST -H "Authorization: Basic [auth key redacted]" -H "Content-Type: application/json" --data '{"title": "this is my new post sent from curl","content": "This is the content","status": "publish"}' "http://mywebsite.com/wp-json/wp/v2/posts

    The response I am getting is

    HTTP/1.1 401 Unauthorized
    Date: Thu, 13 Apr 2017 19:47:42 GMT
    Content-Type: application/json; charset=UTF-8
    Transfer-Encoding: chunked
    Connection: keep-alive
    Set-Cookie: __cfduid=d2221a0a91149358bf8744eb3d5816cea1492112861; expires=Fri, 13-Apr-18 19:47:41 GMT; path=/; domain=.mywebsite.com; HttpOnly
    X-Powered-By: PHP/5.6.30
    X-Robots-Tag: noindex
    Link: <http://mywebsite.com/wp-json/>; rel="https://api.w.org/"
    X-Content-Type-Options: nosniff
    Access-Control-Expose-Headers: X-WP-Total, X-WP-TotalPages
    Access-Control-Allow-Headers: Authorization, Content-Type
    Allow: GET
    Vary: Accept-Encoding,User-Agent
    Server: cloudflare-nginx
    CF-RAY: 34f0f046a79f081d-SIN

    {"code":"rest_cannot_create","message":"Sorry, you are not allowed to create posts as this user.","data":{"status":401}}

    I have tried everything that I could but couldn’t get it to work. Someone suggested adding the following to htaccess which didn’t work

    
    # BEGIN WP BASIC Auth
    <IfModule mod_rewrite.c>
    RewriteCond %{HTTP:Authorization} ^(.*)
    RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
    </IfModule>
    # END WP BASIC Auth

    I tried installing https://wordpress.org/plugins/rest-api/ plugin as well.

    Current PHP version: 5.6 hosted on Godaddy economy hosting. WordPress version:WordPress 4.7.3

    Please help, I have been trying for few days now and none of the solutions seem to work.

    • This topic was modified 9 years, 2 months ago by bcworkz. Reason: Auth key redacted
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    I did’t know if the auth key in your cURL example was real or not, so I removed it. If it was real, even though it didn’t work in this case, it could still allow unwanted access in other ways. To be safe you should remove this key. I realize you haven’t provided a valid domain, but just in case it should come up later, someone could put the two together to gain access.

    What is the nature of the app you are using for requests? If it’s a theme or plugin on the same site, the user must be logged in and have publish_posts capability. Cookies are then used for authentication. You just need to send a proper nonce with your request.

    Otherwise you need an authentication plugin of some sort. Cookie authentication is the only authentication mechanism available natively within WordPress. Plugins may be added to support alternative modes of authentication that will work from remote applications. Some example plugins are OAuth 1.0a Server, Application Passwords, and JSON Web Tokens.

Viewing 1 replies (of 1 total)

The topic ‘unabe to post using wordpress api’ is closed to new replies.