为自定义端点禁用 WooCommerce API 身份验证

Disable WooCommerce API authentication for Custom endpoint

我创建了自定义 WooCommerce API 端点(在自定义 WP 插件中),用于在 WooCommerce 中创建新订单。我通常使用 HTTPS 和带有消费者密钥和消费者秘密的基本身份验证。

此客户 API 旨在供另一个无法在请求 header 中输入消费者密钥和机密的平台访问。所以我只想为这个插件禁用 WooCommerce 身份验证。我将通过比较密钥使用原始请求中的字段进行身份验证。

有人知道怎么做吗?

我找到了解决方案:

// To disable authentication, hook into this filter at a later priority and return a valid WP_User

    add_filter( 'woocommerce_api_check_authentication', array( $this, 'authenticate' ), 0 );

如果要禁用 v3 的身份验证,请禁用 plugins/woocommerce/incoudes/api/legacy/v3/class-wc-api-authentication 中的第 152、153 行。php

//$this->check_oauth_signature( $keys, $params );
//$this->check_oauth_timestamp_and_nonce( $keys, $params['oauth_timestamp'], $params['oauth_nonce'] );

在文件 woocommerce/includes/class-wc-rest-authentication.php

的行下方注释
    if ( !hash_equals( $signature, $consumer_signature ) ) { @codingStandardsIgnoreLine
    return new WP_Error( 'woocommerce_rest_authentication_error', __( 'Invalid signature - provided signature does not matchh.', 'woocommerce' ), array( 'status' => 401 ) );
}