[英]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,
},
},
],
};
以下是我如何摆脱gatsby-source-wordpress
插件中的401 Unauthorized
警告( 单击此处获取有关 npm 的文档)。
警告:
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",
],
值得一试的是尝试禁用所有 WordPress 插件,看看它是否有效。 如果是这样,请尝试一次重新启用它们,看看哪个可能导致问题。
一直得到同样的错误。 并运行verboseOutput: true
给出它无法访问这些端点的信息:
http://your-site.com/wp-json/wp/v2/users/me http://your-site.com/wp-json/wp/v2/settings
我想这是正常的,因为您必须登录才能访问这些页面。
如果一切都对你有用,那么我认为你可以忽略它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.