Forum Replies Created

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter rogeriopavan1984

    (@rogeriopavan1984)

    ok. I install “JSON Basic Authentication”. I created this code:

    $headers = array (
    	'Authorization' => 'Basic ' . base64_encode( 'rogerio_pavan' . ':' . '270184' ));
    
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, 'http://localhost:8080/blog/wp-json/wp/v2/posts');
    $data = array(
    	'title' => 'Hello World',
    	'content_raw'=>'no matters',
    	'date'=>'2016-08-01T14:00:00+10:00'
    );
    
    curl_setopt($curl, CURLOPT_POSTFIELDS, array(
        'method' => 'POST',
        'headers' => $headers,
        'body'    =>  $data));
    
    $post = curl_exec($curl );

    and return {"code":"rest_cannot_create","message":"Sorry, you are not allowed to create new posts.","data":{"status":401}}

    Thread Starter rogeriopavan1984

    (@rogeriopavan1984)

    within the wordpress / plugins

    Thread Starter rogeriopavan1984

    (@rogeriopavan1984)

    ok

    Thread Starter rogeriopavan1984

    (@rogeriopavan1984)

    thanks!

    I try:

    //get token
    $curl = curl_init( 'localhost:8080/blog/oauth/token' );
    curl_setopt( $curl, CURLOPT_POST, true );
    curl_setopt( $curl, CURLOPT_POSTFIELDS, array(
        'client_id' => 'GT9DiTPY6WNjGOp7aGvm6hsvCQ4xi4',
        'client_secret' => 'HTebdz1TV1KrGd0xQDUofe1JeusMEm',
        'grant_type' => 'password',
        'username' => 'rogerio_pavan',
        'password' => '270184',
    ) );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1);
    $auth = curl_exec( $curl );
    print_r ($auth);
    
    $auth = json_decode($auth);
    $access_key = $auth->access_token;	
    
    //call api wprestapi
    $headers = array (
    	'Authorization' => 'Bearer ' . $access_key);
    
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, 'http://localhost:8080/blog/wp-json/wp/v2/posts');
    $data = array(
    	'title' => 'Hello World'
    );
    
    curl_setopt($curl, CURLOPT_POSTFIELDS, array(
        'method' => 'POST',
        'headers' => $headers,
        'body'    =>  $data)
    	);
    
    $post = curl_exec($curl );

    server answer: “{“code”:”rest_cannot_create”,”message”:”Sorry, you are not allowed to create new posts.”,”data”:{“status”:401}}”

    The params ‘method’, ‘headers’, ‘body’ i see in documentation wp rest api 2.
    I’ll see the code plugin wp rest api 2.

Viewing 4 replies - 1 through 4 (of 4 total)