代码之家  ›  专栏  ›  技术社区  ›  Muskett

Gatsby/Wordpress-.env凭据不起作用

  •  1
  • Muskett  · 技术社区  · 6 年前

    我在和盖茨比的资料来源wordpress合作 plugin

    如果我硬编码我的API key/secret到Gatsby.config中,一切正常,但我想将它们添加为.env变量,这样我就可以.gitignore进行部署,这就是问题所在。

    在目录的根目录中,我有一个.env文件,它如下所示

    CLIENT_SECRET=10987654321
    CLIENT_ID=123456
    USER=secret@secret.com
    PASS=mypassword1
    

    然后我尝试在gatsby.config中访问这些,如下所示

    require('dotenv').config({
    path: `.env.${process.env.NODE_ENV}`
      });
    
        module.exports = {
          siteMetadata: {
            title: 'Gatsby Default Starter',
           },
          plugins: [
            {
              resolve: 'gatsby-source-wordpress',
               options: {
                baseUrl: 'myurl.com',
                protocol: 'http',
                hostingWPCOM: true,
                useACF: false,
                auth: {
                  wpcom_app_clientSecret: `${process.env.CLIENT_SECRET}`,
                  wpcom_app_clientId: `${process.env.CLIENT_ID}`,
                  wpcom_user: `${process.env.USER}`,
                  wpcom_pass: `${process.env.PASS}`,
               },
              },
             },
            {
             resolve: `gatsby-plugin-emotion`,
            },
            'gatsby-plugin-react-helmet',
            {
              resolve: `gatsby-plugin-manifest`,
              options: {
                name: 'gatsby-starter-default',
                short_name: 'starter',
                start_url: '/',
                background_color: '#663399',
                theme_color: '#663399',
                display: 'minimal-ui',
                icon: 'src/images/gatsby-icon.png', // This path is 
        relative to the root of the site.
            },
           },
            'gatsby-plugin-offline',
          ],
        }
    

    当我运行gatsby develop或gatsby build时返回以下错误

    • 源和转换节点服务器响应为“400错误请求”

    • 源和转换节点服务器响应为“403禁止” 内部异常消息:“用户无法访问此私人博客。” 没有路径可供提取。结束。

    所以,问题是.env变量似乎没有正确地通过,但我看不出它们不通过的原因?我在设置这个过程中遗漏了什么吗?

    1 回复  |  直到 6 年前
        1
  •  0
  •   LekoArts    6 年前

    盖茨比不知道你指的是哪个插件(参见 How to use )你的整体语法是错误的。这个 plugins 是一个数组。

    module.exports = {
      plugins: [
        {
          resolve: "gatsby-source-wordpress",
          options: {
            auth: {
              wpcom_app_clientSecret: process.env.CLIENT_SECRET,
              wpcom_app_clientId: process.env.CLIENT_ID,
              wpcom_user: process.env.USER,
              wpcom_pass: process.env.PASS,
            }
          }
        }
      ]
    }
    

    假设您还定义了自述文件中提到的其他必要字段,那么这应该可以工作。