【问题标题】:WP-API Retrieving Drafts "Cookie nonce is invalid"WP-API 检索草稿“Cookie nonce 无效”
【发布时间】:2018-02-23 14:37:34
【问题描述】:

我正在开发一个前端应用程序,该应用程序与位于单独域中的 wordpress API 相关联。

我想检索一个帖子草稿,因此当用户在 wordpress 管理面板中单击“预览帖子”时,它会打开我的应用程序并显示正确的内容。

我正在将一些 Javascript 加载到 WP Admin 中,因此我可以使用 wp_nonce 令牌修改管理面板中的所有“预览帖子”链接以进行身份​​验证。这是使用下面的 sn-p 完成的,它将预览帖子链接调整为例如:http://example.com/blog?p=127&preview=true&auth=16045802ee

function admin_inline_js(){

    // Grab URL 
    echo '
    <script>
    document.addEventListener("DOMContentLoaded", function() {
        // Grab all preview anchors
        var anchors = document.querySelectorAll("a[href*=\'preview=true\']"), i;
        // Loop through and amend to remove the trailing slash, as WP doesnt provide any easy method to achieve this.
        for(i = 0; i < anchors.length; i++) {
            anchors[i].href = anchors[i].href.replace("blog/", "blog") + \'&auth=' . wp_create_nonce('wp_rest') .'\';
        }
    });
    </script>
    ';
}
add_action( 'admin_print_scripts', 'admin_inline_js' );

http://example.com/blog?p=127&amp;preview=true&amp;auth=16045802ee,然后使用auth 参数将请求发回wordpress,以检索ID 为127 的草稿,随机数标记为16045802ee。但是这不起作用,我收到了以下回复:

object(stdClass)#459 (3) { ["code"]=> string(25) "rest_cookie_invalid_nonce" ["message"]=> string(23) "Cookie nonce is invalid" ["data"]=> object(stdClass)#532 (1) { ["status"]=> int(403) } }

谁能发现我在这里做错了什么? :/

谢谢。

【问题讨论】:

    标签: php wordpress api nonce


    【解决方案1】:

    您面临两个问题:

    1. Wordpress “nonce”是一种仅用于防止 CSRF 令牌的工具。在为管理面板编写插件时非常有用,但对 API 进行身份验证时没有用。

    2. 默认规定的 .htaccess 文件阻止通过 API 传递身份验证详细信息。

    您需要通过 API 传递身份验证详细信息才能接收包含受保护数据的响应。为简单起见,我建议首先使用 HTTP Basic Auth 来使其正常工作,并可选择在未来使用更安全的身份验证方法(例如 OAuth)来增强您的代码。

    我建议专门为 API 使用创建一个新用户,而不是在服务器上的文本文件中传递管理员详细信息。

    创建用户后,您可以使用 PHP 中的简单 cURL 请求草稿帖子,如下所示:

    $apiUsername = "apiuser";
    $apiPassword = "sup3r-s3cr3t-p455w0rd";
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://example.com/wp-json/wp/v2/posts/127");
    curl_setopt($ch, CURLOPT_USERPWD, "$apiUsername:$apiPassword");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $json = curl_exec($ch);
    $postObject = json_decode($json);
    

    这将设置包含用户名和密码的请求标头,只要在此标头到达 Wordpress 之前没有任何干扰,您将在$postObject 中收到您的帖子详细信息,您可以在此处停止阅读。你已经完成了。


    但是,为了获得更多乐趣,Wordpress 规定的默认 .htaccess 文件会丢弃 HTTP 标头,因此如果您正在使用它,则需要自己修复它。基本上,Wordpress 脚本需要看到 $_SERVER 变量中的 PHP_AUTH_USERPHP_AUTH_PW 键才能工作。

    快速解决方法是在 .htaccess 中包含以下规则作为第一个 RewriteRule:

    RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}]
    

    这会将标头拆分为以“REMOTE_USER”为前缀的变量,并且可以使用以下 PHP 再次填充预期的字段(也许可以进入 index.php)。

    $userPass = $_SERVER["REDIRECT_REMOTE_USER"];
    if(!empty($userPass)
    && strstr($userPass, "Basic")) {
            $userPass = substr($userPass, strpos($userPass, " ") + 1);
            $userPass = base64_decode($userPass);
            $userPass = explode(":", $userPass);
    
            $_SERVER['PHP_AUTH_USER'] = $userPass[0];
            $_SERVER['PHP_AUTH_PW'] = $userPass[1];
    }
    

    【讨论】:

      【解决方案2】:

      我调试了代码,认为cookie损坏了。

      在下面的代码sn-p中,我们可以看到。

      // Check the nonce.
      $result = wp_verify_nonce( $nonce, 'wp_rest' );
      
      if ( ! $result ) {
          return new WP_Error( 'rest_cookie_invalid_nonce', __( 'Cookie nonce is invalid' ), array( 'status' => 403 ) );
      }
      

      对我来说,当我清除我的域的浏览器 cookie 后,这个问题就解决了。

      【讨论】:

      • 如果你恢复一个站点,这是一个老歌但 WP 的好东西。谢谢提醒!
      【解决方案3】:

      我发现了这个错误[{"code":"json_cookie_invalid_nonce","message":"Cookie nonce is invalid"}] 在我安装 WP POS 后,当 POS 页面打开时间 ERROR "forbidden"

      展开后报错 我收到这条消息[{"code":"json_cookie_invalid_nonce","message":"Cookie nonce is invalid"}]

      过了一会儿,我找到了解决方案。我一一停用新安装的插件,停用后我发现“Yoast SEO”插件有问题,它是 POS 插件工作正常,然后我再次激活 Yoast SEO 插件没问题,它工作正常

      【讨论】:

        【解决方案4】:

        您需要为 POS 角色添加权限“promote_users”。我使用“用户角色编辑器”插件来执行此操作,但您也可以将以下内容添加到您的 functions.php:

        $pos_role = get_role( 'pos' ); // Or whatever role you want to add it to
        $pos_role->add_cap( 'promote_users' );
        

        【讨论】:

          猜你喜欢
          • 2018-03-15
          • 2014-08-08
          • 1970-01-01
          • 2018-04-19
          • 1970-01-01
          • 2018-02-26
          • 2014-08-17
          • 1970-01-01
          • 2020-11-09
          相关资源
          最近更新 更多