νƒœκ·Έ 보관물: rest-api

rest-api

곡개적으둜 WordPress REST API v2 μ—”λ“œ 포인트 숨기기 등둝 된 λͺ¨λ“  μ‚¬μš©μžλ₯Ό λ³΄μ—¬μ£ΌλŠ” μ‚¬λžŒκ³Ό

λ‚΄κ°€ μ‚¬μš©μ„ μ‹œμž‘ν•˜κ³ μžν•˜λŠ” μ›Œλ“œ ν”„λ ˆμŠ€μ˜ REST API v2κ°€ λ‚΄ μ‚¬μ΄νŠΈμ—μ„œ 쿼리 정보. μ—”λ“œ 포인트 URL을 직접 λ°©λ¬Έν•˜λ©΄ λͺ¨λ“  데이터λ₯Ό 곡개적으둜 λ³Ό 수 μžˆμŠ΅λ‹ˆλ‹€. λ˜ν•œ λ§Žμ€ μžμŠ΅μ„œμ—μ„œ 라이브 μ‚¬μ΄νŠΈκ°€ μ•„λ‹Œ ν…ŒμŠ€νŠΈ λ˜λŠ” 둜컬 μ„œλ²„ μ‚¬μš©μ— λŒ€ν•΄ μ–ΈκΈ‰ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

λ‚΄ μ§ˆλ¬Έμ€ :

  • 이것은 생산 ν˜„μž₯μ—μ„œ μ‚¬μš©ν•˜κΈ°μœ„ν•œ κ²ƒμž…λ‹ˆκΉŒ?
  • /wp-json/wp/v2/users/μ‚¬μ΄νŠΈμ— 등둝 된 λͺ¨λ“  μ‚¬μš©μžλ₯Ό λ³΄μ—¬μ£ΌλŠ” μ‚¬λžŒκ³Ό 같이 μ—”λ“œ 포인트λ₯Ό λ³Ό μˆ˜μžˆκ²Œν•˜λŠ” λ³΄μ•ˆ μœ„ν—˜μ΄
    μžˆμŠ΅λ‹ˆκΉŒ?
  • 인증 된 μ‚¬μš©μž 만 μ—”λ“œ ν¬μΈνŠΈμ— μ•‘μ„ΈμŠ€ν•˜λ„λ‘ ν—ˆμš© ν•  수 μžˆμŠ΅λ‹ˆκΉŒ?

λ³΄μ•ˆκ³Ό κ΄€λ ¨λœ λͺ¨λ²” 사둀λ₯Ό λ”°λ₯΄κ³  μžˆμœΌλ―€λ‘œ λͺ¨λ“  팁이 도움이 될 κ²ƒμž…λ‹ˆλ‹€. API λ¬Έμ„œλŠ” 인증을 μ–ΈκΈ‰,ν•˜μ§€λ§Œ λ‚œ 직접 μ•‘μ„ΈμŠ€λ˜λŠ” URL을 λ°©μ§€ν•˜λŠ” 방법을 λͺ¨λ₯΄κ² μ–΄μš”. λ‹€λ₯Έ μ‚¬λžŒλ“€μ€ 일반적으둜 λ„ˆλ¬΄ λ§Žμ€ 정보λ₯Ό λ…ΈμΆœμ‹œν‚€μ§€ μ•Šκ³  μ™ΈλΆ€ μ‘μš© ν”„λ‘œκ·Έλž¨μ—μ„œμ΄ 데이터에 μ•‘μ„ΈμŠ€ν•˜λ„λ‘ μ–΄λ–»κ²Œ μ„€μ •ν•©λ‹ˆκΉŒ?



λ‹΅λ³€

이것은 생산 ν˜„μž₯μ—μ„œ μ‚¬μš©ν•˜κΈ°μœ„ν•œ κ²ƒμž…λ‹ˆκΉŒ?

예. λ§Žμ€ μ‚¬μ΄νŠΈμ—μ„œ 이미 μ‚¬μš©ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

μ‚¬μ΄νŠΈμ— 등둝 된 λͺ¨λ“  μ‚¬μš©μžλ₯Ό ν‘œμ‹œν•˜λŠ” / wp-json / wp / v2 / users /와 같은 μ‚¬λžŒμ΄ μ—”λ“œ 포인트λ₯Ό λ³Ό μˆ˜μžˆκ²Œν•˜λŠ” λ³΄μ•ˆ μœ„ν—˜μ΄ μžˆμŠ΅λ‹ˆκΉŒ?

μ•„λ‹ˆμš”. μ„œλ²„μ˜ 응닡은 λ³΄μ•ˆκ³Ό 관련이 μ—†μŠ΅λ‹ˆλ‹€. 빈 ν™”λ©΄ / 읽기 μ „μš© μ•‘μ„ΈμŠ€λ‘œ 무엇을 ν•  수 μžˆμŠ΅λ‹ˆκΉŒ? 아무것도!

κ·ΈλŸ¬λ‚˜ μ‚¬μ΄νŠΈμ—μ„œ μ·¨μ•½ν•œ μ•”ν˜Έλ₯Ό ν—ˆμš©ν•˜λŠ” 경우 λͺ‡ 가지 λ¬Έμ œκ°€ μžˆμŠ΅λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ 그것은 κ·€ν•˜μ˜ μ‚¬μ΄νŠΈ μ •μ±…μ΄λ―€λ‘œ REST APIλŠ” 그것에 λŒ€ν•΄ 아무것도 λͺ¨λ¦…λ‹ˆλ‹€.

인증 된 μ‚¬μš©μž 만 μ—”λ“œ ν¬μΈνŠΈμ— μ•‘μ„ΈμŠ€ν•˜λ„λ‘ ν—ˆμš© ν•  수 μžˆμŠ΅λ‹ˆκΉŒ?

예. κΆŒν•œ 콜백 을 μ‚¬μš©ν•˜μ—¬μ΄λ₯Ό μˆ˜ν–‰ ν•  수 μžˆμŠ΅λ‹ˆλ‹€ .

예λ₯Ό λ“€λ©΄ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

if ( 'edit' === $request['context'] && ! current_user_can( 'list_users' ) ) {
    return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you cannot view this resource with edit context.' ), array( 'status' => rest_authorization_required_code() ) );
}

λ‹€λ₯Έ μ‚¬λžŒλ“€μ€ 일반적으둜 λ„ˆλ¬΄ λ§Žμ€ 정보λ₯Ό λ…ΈμΆœμ‹œν‚€μ§€ μ•Šκ³  μ™ΈλΆ€ μ‘μš© ν”„λ‘œκ·Έλž¨μ—μ„œμ΄ 데이터에 μ•‘μ„ΈμŠ€ν•˜λ„λ‘ μ–΄λ–»κ²Œ μ„€μ •ν•©λ‹ˆκΉŒ?

정보 κ°€ 무엇인지 / μ–Έμ œ λͺ¨λ₯΄κΈ° λ•Œλ¬Έμ—μ΄ μ§ˆλ¬Έμ— λŒ€λ‹΅ν•˜κΈ°κ°€ μ–΄λ ΅μŠ΅λ‹ˆλ‹€ . κ·ΈλŸ¬λ‚˜ μš°λ¦¬λŠ” λͺ¨λ‘ μ°Έμ‘° 와 치트 μ‹œνŠΈλ₯Ό μ‚¬μš© ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.


λ‹΅λ³€

인증 된 μ‚¬μš©μž 만 μ—”λ“œ ν¬μΈνŠΈμ— μ•‘μ„ΈμŠ€ν•˜λ„λ‘ ν—ˆμš© ν•  수 μžˆμŠ΅λ‹ˆκΉŒ?

컨텐츠λ₯Ό 보렀면 인증이 ν•„μš”ν•œ API μ—”λ“œ ν¬μΈνŠΈμ— μ‚¬μš©μž μ •μ˜ κΆŒν•œ μ½œλ°±μ„ μΆ”κ°€ ν•  수 μžˆμŠ΅λ‹ˆλ‹€. κΆŒν•œμ΄μ—†λŠ” μ‚¬μš©μžλŠ” 였λ₯˜ μ‘λ‹΅μ„λ°›μŠ΅λ‹ˆλ‹€"code": "rest_forbidden"

이λ₯Ό μˆ˜ν–‰ν•˜λŠ” κ°€μž₯ κ°„λ‹¨ν•œ 방법은 WP_REST_Posts_Controllerλ₯Ό ν™•μž₯ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€. λ‹€μŒμ€ κ·Έ κ°„λ‹¨ν•œ μ˜ˆμž…λ‹ˆλ‹€.

class My_Private_Posts_Controller extends WP_REST_Posts_Controller {

   /**
   * The namespace.
   *
   * @var string
   */
   protected $namespace;

   /**
   * The post type for the current object.
   *
   * @var string
   */
   protected $post_type;

   /**
   * Rest base for the current object.
   *
   * @var string
   */
   protected $rest_base;

  /**
   * Register the routes for the objects of the controller.
   * Nearly the same as WP_REST_Posts_Controller::register_routes(), but with a
   * custom permission callback.
   */
  public function register_routes() {
    register_rest_route( $this->namespace, '/' . $this->rest_base, array(
        array(
            'methods'             => WP_REST_Server::READABLE,
            'callback'            => array( $this, 'get_items' ),
            'permission_callback' => array( $this, 'get_items_permissions_check' ),
            'args'                => $this->get_collection_params(),
            'show_in_index'       => true,
        ),
        array(
            'methods'             => WP_REST_Server::CREATABLE,
            'callback'            => array( $this, 'create_item' ),
            'permission_callback' => array( $this, 'create_item_permissions_check' ),
            'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
            'show_in_index'       => true,
        ),
        'schema' => array( $this, 'get_public_item_schema' ),
    ) );

    register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
        array(
            'methods'             => WP_REST_Server::READABLE,
            'callback'            => array( $this, 'get_item' ),
            'permission_callback' => array( $this, 'get_item_permissions_check' ),
            'args'                => array(
                'context' => $this->get_context_param( array( 'default' => 'view' ) ),
            ),
            'show_in_index'       => true,
        ),
        array(
            'methods'             => WP_REST_Server::EDITABLE,
            'callback'            => array( $this, 'update_item' ),
            'permission_callback' => array( $this, 'update_item_permissions_check' ),
            'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
            'show_in_index'       => true,
        ),
        array(
            'methods'             => WP_REST_Server::DELETABLE,
            'callback'            => array( $this, 'delete_item' ),
            'permission_callback' => array( $this, 'delete_item_permissions_check' ),
            'args'                => array(
                'force' => array(
                    'default'     => true,
                    'description' => __( 'Whether to bypass trash and force deletion.' ),
                ),
            ),
            'show_in_index'       => false,
        ),
        'schema' => array( $this, 'get_public_item_schema' ),
    ) );
  }

  /**
   * Check if a given request has access to get items
   *
   * @param WP_REST_Request $request Full data about the request.
   * @return WP_Error|bool
   */
  public function get_items_permissions_check( $request ) {
    return current_user_can( 'edit_posts' );
  }

}

콜백이 μ•‘μ„ΈμŠ€ ν—ˆμš© μ—¬λΆ€λ₯Ό κ²°μ •ν•˜λŠ” 데 function get_items_permissions_checkμ‚¬μš© ν•˜λŠ” κΆŒν•œ current_user_can을 μ•Œ 수 μžˆμŠ΅λ‹ˆλ‹€. APIλ₯Ό μ‚¬μš©ν•˜λŠ” 방법에 따라 ν΄λΌμ΄μ–ΈνŠΈ 인증에 λŒ€ν•΄ μžμ„Ένžˆ μ•Œμ•„μ•Ό ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.

그런 λ‹€μŒ λ‹€μŒ 인수λ₯Ό μΆ”κ°€ν•˜μ—¬ REST API μ§€μ›μœΌλ‘œ μ‚¬μš©μž μ •μ˜ κ²Œμ‹œλ¬Ό μœ ν˜•μ„ 등둝 ν•  수 μžˆμŠ΅λ‹ˆλ‹€. register_post_type

  /**
   * Register a book post type, with REST API support
   *
   * Based on example at: http://codex.wordpress.org/Function_Reference/register_post_type
   */
  add_action( 'init', 'my_book_cpt' );
  function my_book_cpt() {
    $labels = array(
        'name'               => _x( 'Books', 'post type general name', 'your-plugin-textdomain' ),
        'singular_name'      => _x( 'Book', 'post type singular name', 'your-plugin-textdomain' ),
        'menu_name'          => _x( 'Books', 'admin menu', 'your-plugin-textdomain' ),
        'name_admin_bar'     => _x( 'Book', 'add new on admin bar', 'your-plugin-textdomain' ),
        'add_new'            => _x( 'Add New', 'book', 'your-plugin-textdomain' ),
        'add_new_item'       => __( 'Add New Book', 'your-plugin-textdomain' ),
        'new_item'           => __( 'New Book', 'your-plugin-textdomain' ),
        'edit_item'          => __( 'Edit Book', 'your-plugin-textdomain' ),
        'view_item'          => __( 'View Book', 'your-plugin-textdomain' ),
        'all_items'          => __( 'All Books', 'your-plugin-textdomain' ),
        'search_items'       => __( 'Search Books', 'your-plugin-textdomain' ),
        'parent_item_colon'  => __( 'Parent Books:', 'your-plugin-textdomain' ),
        'not_found'          => __( 'No books found.', 'your-plugin-textdomain' ),
        'not_found_in_trash' => __( 'No books found in Trash.', 'your-plugin-textdomain' )
    );

    $args = array(
        'labels'             => $labels,
        'description'        => __( 'Description.', 'your-plugin-textdomain' ),
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'book' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'show_in_rest'       => true,
        'rest_base'          => 'books-api',
        'rest_controller_class' => 'My_Private_Posts_Controller',
        'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
    );

    register_post_type( 'book', $args );
}

당신은 λ³Ό 수 μžˆμŠ΅λ‹ˆλ‹€ rest_controller_classμ‚¬μš©ν•˜λŠ” My_Private_Posts_ControllerλŒ€μ‹  κΈ°λ³Έ 컨트둀러λ₯Ό.

μ„€λͺ…μ„œ μ™ΈλΆ€μ—μ„œ REST APIλ₯Ό μ‚¬μš©ν•˜λŠ” 데 λŒ€ν•œ 쒋은 μ˜ˆμ™€ μ„€λͺ…을 μ°ΎκΈ°κ°€ μ–΄λ €μ› μŠ΅λ‹ˆλ‹€ . κΈ°λ³Έ 컨트둀러 ν™•μž₯에 λŒ€ν•œ ν›Œλ₯­ν•œ μ„€λͺ…을 μ°Ύμ•˜ 으며 μ—¬κΈ° 에 μ—”λ“œ 포인트 좔가에 λŒ€ν•œ μ² μ €ν•œ μ•ˆλ‚΄μ„œκ°€ μžˆμŠ΅λ‹ˆλ‹€.


λ‹΅λ³€

λ‘œκ·ΈμΈν•˜μ§€ μ•Šμ€ λͺ¨λ“  μ‚¬μš©μžκ°€ REST APIλ₯Ό μ „ν˜€ μ‚¬μš©ν•˜μ§€ λͺ»ν•˜λ„둝 μ°¨λ‹¨ν•˜λŠ” 데 μ‚¬μš©ν•œ λ‚΄μš©μ€ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

add_filter( 'rest_api_init', 'rest_only_for_authorized_users', 99 );
function rest_only_for_authorized_users($wp_rest_server){
    if ( !is_user_logged_in() ) {
        wp_die('sorry you are not allowed to access this data','cheatin eh?',403);
    }
}

λ‹΅λ³€

add_filter( 'rest_api_init', 'rest_only_for_authorized_users', 99 );
function rest_only_for_authorized_users($wp_rest_server)
{
if( !is_user_logged_in() )

    wp_die('sorry you are not allowed to access this data','Require Authentication',403);
} }
function json_authenticate_handler( $user ) {

global $wp_json_basic_auth_error;

$wp_json_basic_auth_error = null;

// Don't authenticate twice
if ( ! empty( $user ) ) {
    return $user;
}

if ( !isset( $_SERVER['PHP_AUTH_USER'] ) ) {
    return $user;
}

$username = $_SERVER['PHP_AUTH_USER'];
$password = $_SERVER['PHP_AUTH_PW'];


remove_filter( 'determine_current_user', 'json_authenticate_handler', 20 );

$user = wp_authenticate( $username, $password );

add_filter( 'determine_current_user', 'json_authenticate_handler', 20 );

if ( is_wp_error( $user ) ) {
    $wp_json_basic_auth_error = $user;
    return null;
}

$wp_json_basic_auth_error = true;

return $user->ID;}add_filter( 'determine_current_user', 'json_authenticate_handler', 20 );

λ‹΅λ³€