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

时间:2018-01-23 16:04:09

标签: javascript wordpress reactjs rest gatsby

我刚试过Wordpress'使用GatsbyJS的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,
      },
    },
  ],


};

3 个答案:

答案 0 :(得分:1)

这就是我摆脱401 Unauthorized插件(click here for documentation on npm)中的gatsby-source-wordpress警告的原因。

警告:

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."

仅在includedRoutes中使用gatsby-config.js设置包含所需的路由:

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

一直有同样的错误。并且运行verboseOutput: true会提供无法访问这些端点的信息:

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

我猜这是正常的,因为您必须登录才能访问这些页面。

如果一切都适合你,那么我认为你可以忽略它。