9
gatsby: 2.20.8
gatsbh-source-contentful: 2.2.5

I've verified my spaceId and accessToken are correct, and I've verified that these keys have access to my Contentful development environment. I've tried:

  • Enabling GATSBY_CONTENTFUL_OFFLINE
  • Deleting the .cache directory
  • Deleting node_modules
  • Setting environment to "master" and removing it completely
  • Wrapping the env vars in template literals (suggested on an old github issue)

I keep getting this error. Any ideas?

ERROR 

Accessing your Contentful space failed.
Try setting GATSBY_CONTENTFUL_OFFLINE=true to see if we can serve from cache.

Used options:
spaceId: "*********z1u"
accessToken: "*****************************************vMI'"
environment: "development"
host (default value): "cdn.contentful.com"
downloadLocal (default value): false
localeFilter (default value): [Function]
forceFullSync (default value): false
pageLimit (default value): 100
useNameForId (default value): true

And here's my code from gatsby-config.js

{
  resolve: `gatsby-source-contentful`,
  options: {
    spaceId: process.env.CONTENTFUL_SPACE_ID,
    accessToken: process.env.CONTENTFUL_ACCESS_TOKEN,
    environment: process.env.CONTENTFUL_ENVIRONMENT,
  },
},
Barryman9000
  • 1,987
  • 3
  • 21
  • 29
  • Just wondering why do you have environment in there in the config file? Also do you currently have any content? I know I came across some errors when I was trying to set up before having any content models in the UI. Also are you able to see the graphiQL output? – Aquasar Mar 27 '20 at 21:11
  • 1
    I do have published content. I created the development environment in Contentful just to see if that was the problem (I just haven't bothered creating an env var) but if I completely remove the `environment` property from the `gatsby-source-contentful` options it still fails. I can't access graphiQL because this error is killing the build – Barryman9000 Mar 27 '20 at 21:42
  • I change the `gatsby-config.js` code and added a ref to an env var for environment to help avoid confusion – Barryman9000 Mar 27 '20 at 22:05
  • I have the same problem. I installed the starter called [rg-portfolio](https://www.gatsbyjs.org/starters/rohitguptab/rg-portfolio/) and ran the script to initialize my Contentful space with models and content. So my spaceID and app token are correct. When I launched `gatsby develop` it gave me your error. I [posted an issue in the rg-portfolio repo](https://github.com/rohitguptab/rg-portfolio/issues/1). At this point I have a suspicion this problem results from any kind of mismatch between the model(s) expected by a graphQL query and what is actually in the Contentful space. – Brent Arias Apr 28 '20 at 05:26
  • With rg-portfolio, I made the mistake of using the access token provided in the tutorial about Contentful. Instead, I needed to use the access token on my Contentful space at... `Setting > API Keys > Example Key 1`. – Matt May 28 '20 at 08:37

5 Answers5

4

Not sure if you got it working in the meantime but I had this same issue and found this on Github https://github.com/gatsbyjs/gatsby/issues/19392 that is what you and I are having.

Apparently, in newer versions of gatsby-source-plugin it requires the key to have access to the master environment as well (which is ridiculous imo).

After giving the key access to master environment it started working.

Hugo Marques
  • 65
  • 10
  • Thanks but I've verified the space I'm trying to access has permissions for the `master` and `development` environments but I'm still getting that same error – Barryman9000 May 28 '20 at 21:32
  • @Barryman9000 when you say that you mean that you're using an api key that has access to both master and development branch? For me it only worked when I checked both branches in here https://i.imgur.com/jzROvsj.png – Hugo Marques Jun 01 '20 at 14:28
  • Correct, I've tried just checking the box for just `master`, just `development` and then both. it doesn't seem to matter. – Barryman9000 Jun 01 '20 at 22:13
2

Had a similar issue, after importing one space to another. I deleted the demo API key (which i had tried to use) and created a new one. Worked after that

Alex
  • 804
  • 2
  • 14
  • 26
0

As I stated in my comment to your question, I was suspicious this had something to do with a discrepancy between the Contentful models that my Gatsby build expected, and what models were actually in the Contentful space. Acting on that I decided to delete my Contentful space entirely and re-create it. I then re-ran the automation script, which came with the rg-portfolio repo, that is responsible for creating all my initial content models. This had been part of the instructions for rg-portfolio.

contentful space --space-id xxxxxxxxx0b9 import --content-file contentful-data.json

The script completed successfully, no errors - only warnings about rate limiting.

When I then finally retried gatsby develop it worked!!!

Post-game analysis.

Part of the reason I suspected this Contentful "model discrepancy" issue was because when I first ran that contentful-data.json script:

  1. It was running against a Contentful space that already had some unrelated content models in it.
  2. It gave actual errors that made me wonder if it really had worked properly.

So there was a lot of room for me to believe there was something just "wrong" with the model(s) in that Contentful space.

Community
  • 1
  • 1
Brent Arias
  • 26,187
  • 32
  • 120
  • 209
  • Thanks for the reply but in my case I think it's something else. I tried creating a new space and using one of the sample/starter spaces that Contentful offers to create for you. Even with their sample space, I'm still getting the same error (after updating to the new keys). The other weird thing is I can create content models from the command line but running `gatsby-develop` still fails – Barryman9000 May 12 '20 at 20:08
  • I even forked this repo, updated the keys, and still get that error when running `gatsby develop` https://github.com/Khaledgarbaya/egghead-jamstack-course – Barryman9000 May 12 '20 at 20:36
0

I encountered the same issue when I tried to access my development environment. I was using gatsby 2.22.17 and gatsby-source-contentful 2.3.14. Apparently, Contentful has environment alias. It can be found in settings > environments. I resolved mine by using the environment alias.

Basically, it is like making the master a pointer to an environment. You might be asked to rename your master environment when setting it up. Please think carefully about the name you want to choose for your master environment because you might not be able to rename it again. I don't think the process is reversible.

So any time you want to work on your development environment. You have to go to the environment setting and change the target environment for the alias. And then you change it back to your master environment when you are done. Try not to forget to change the target environment for the alias back to the master environment. Else your production site might fetch its content from the development environment on the next build.

I hope this helps. This solution does not make sense for me but it was the only one I could find. Please I would love to hear about any other better solution.

Peter Umoren
  • 301
  • 8
  • 5
  • 2
    Thanks for the suggestion but unfortunately this didn't work for me. I have one API key, I'm using the `SpaceID`, the `Content Delivery API - access token` and the `environment` set to `master` or `development` or my new alias `prod-1`. I verified that the API key has access to the alias and even tried toggling those to different configurations but nothing worked. Thanks again – Barryman9000 Jun 07 '20 at 00:31
0

In my case just deleted the 2 spaces in Contentful and then created a new one, changed the values of the env variables and it worked, seems is some problem with the content model and content..

just reporting

Rolando Niubó
  • 167
  • 2
  • 7