GatsbyJS 和 Wordpress REST API 401 未经授权的错误

GatsbyJS & Wordpress REST API 401 Unauthorized Error

我刚刚使用 GatsbyJS 试用了 Wordpress 的 REST API,但出现以下错误:

success onPreBootstrap — 0.005 s
⠠ source and transform nodes -> wordpress__POST fetched : 6
⠐ source and transform nodes -> wordpress__PAGE fetched : 2
⠈ source and transform nodes -> wordpress__wp_media fetched : 15
⠠ source and transform nodes -> wordpress__wp_types fetched : 1
⡀ source and transform nodes -> wordpress__wp_statuses fetched : 1
⠁ source and transform nodes -> wordpress__wp_taxonomies fetched : 1
⠠ source and transform nodes -> wordpress__CATEGORY fetched : 8
⢀ source and transform nodes -> wordpress__TAG fetched : 5
⠂ source and transform nodes -> wordpress__wp_users fetched : 2
⠐ source and transform nodesThe server response was "401 Unauthorized"
Inner exception message : "You are not currently logged in."
⢀ source and transform nodes -> wordpress__wp_comments fetched : 0
⡀ source and transform nodesThe server response was "401 Unauthorized"
Inner exception message : "Sorry, you are not allowed to do that."
success source and transform nodes — 16.447 s
success building schema — 0.339 s
success createLayouts — 0.015 s
success createPages — 0.004 s
success createPagesStatefully — 0.010 s
success onPreExtractQueries — 0.001 s
success update schema — 0.160 s
success extract queries from components — 0.088 s
success run graphql queries — 0.030 s
success write out page data — 0.003 s
success write out redirect data — 0.001 s
success onPostBootstrap — 0.001 s

到目前为止,我只通过 npm 安装了 gatsby-source-wordpress,并将我的 gatsby-config 更改为:

module.exports = {
  siteMetadata: {
    title: 'Gatsby Default Starter',
  },
  plugins: [
    'gatsby-plugin-react-helmet',
    /*
     * Gatsby's data processing layer begins with “source”
     * plugins. Here the site sources its data from Wordpress.
     */
    {
      resolve: `gatsby-source-wordpress`,
      options: {
        /*
        * The base URL of the Wordpress site without the trailingslash and the protocol. This is required.
        * Example : 'gatsbyjswpexample.wordpress.com' or 'www.example-site.com'
        */
        baseUrl: `myurl.com`,
        // The protocol. This can be http or https.
        protocol: `http`,
        // Indicates whether the site is hosted on wordpress.com.
        // If false, then the asumption is made that the site is self hosted.
        // If true, then the plugin will source its content on wordpress.com using the JSON REST API V2.
        // If your site is hosted on wordpress.org, then set this to false.
        hostingWPCOM: false,
        // If useACF is true, then the source plugin will try to import the Wordpress ACF Plugin contents.
        // This feature is untested for sites hosted on Wordpress.com
        useACF: false,
      },
    },
  ],


};

值得一试的是尝试禁用所有 WordPress 插件,看看它是否有效。如果是这样,请尝试一次重新启用它们并查看哪个可能导致问题。

遇到同样的错误。 运行 verboseOutput: true 给出了它无法访问这些端点的信息:

http://your-site.com/wp-json/wp/v2/users/me http://your-site.com/wp-json/wp/v2/settings

我想这很正常,因为您必须先登录才能访问这些页面。

如果其他一切都适合你,那么我想你可以忽略它。

以下是我如何摆脱 gatsby-source-wordpress 插件 (click here for documentation on npm) 中的 401 Unauthorized 警告。

警告:

Path: /wp-json/wp/v2/users/me?per_page=100&page=1
The server response was "401 Unauthorized"
Inner exception message: "You are not currently logged in."

使用 gatsby-config.js 中的 includedRoutes 设置仅包括您需要的路线:

includedRoutes: [
    "**/posts",
    "**/pages",
    "**/media",
    "**/categories",
    "**/tags",
    "**/taxonomies",
    // "**/users", // uncomment this and watch the warning above reappear!
    "**/menus",
],