question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error getspace with non master enviroment accessToken

See original GitHub issue

Expected Behavior

My API key has access to QA environment only. But before initial request I would like to call getSpace. And received 404

Actual Behavior, Possible Solution

Set space and environment

Environment

QA

  • Language Version: 11*
  • Operating System: Ubuntu
  • Package Version: 2.1.47
  • Which API are you using?: Delivery / Preview

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
nimajnebcommented, Oct 22, 2019

@Khaledgarbaya Thank you for looking into this. I understand and I can also confirm that everything works when granting the API key access to the master environment.

Here’s some background: In our project, we’re consuming the Contentful API with the gatsby-source-contentful plugin. When fetching the content, the plugin uses getSpace(), probably to get some metadata from it. However, if the configured API key does not have access to the master space, an exception is thrown, which essentially breaks the build.

As it might not always be desirable to grant certain API keys access to the master environment, even during development, I think I should maybe report this in the gatsby project and see if there’s some solution, even if it’s only a note in the docs.

1reaction
nimajnebcommented, Oct 21, 2019

@Khaledgarbaya

Get space will try to get data about your master environment and since you didn’t have access to it the API returns the error

Hi! I was just debugging the same problem before finding this issue.

If I understand the documentation at https://www.contentful.com/developers/docs/concepts/multiple-environments/ correctly, and a sandbox environment acts as kind of an override for specific space information, should getSpace then really get the data of the master environment’s space?

Here’s my code that failed:

const client = contentful.createClient({
  space: 'my-space-id',
  accessToken: 'my-access-token', // my-access-token does have access to test-environment, but not to master
  environment: 'test-environment',
});
try {
  const space = await client.getSpace();
} catch (e) {
  // ...
}

I’d expect space to contain the space information for the environment specified in createClient, but instead the client.getSpace() call actually throws an exception with a 404 error, just like mentioned by the OP.

This feels like a bug to me. Or do I misunderstand something here?

Read more comments on GitHub >

github_iconTop Results From Across the Web

contentful.js - Documentation
contentful.js. /** * Contentful Delivery API SDK. Allows you to create instances of a client * with access to the Contentful Content Delivery...
Read more >
Content Management API | Contentful
A space has a minimum of one environment, which is called master. The master environment cannot be changed, deleted or renamed.
Read more >
Accessing your Contentful space failed with gatsby-source ...
After giving the key access to master environment it started working. ... The script completed successfully, no errors - only warnings about ...
Read more >
3xn3xw8e2 - JavaScript - OneCompiler
environment : 'master', // defaults to 'master' if not set. accessToken: 'yTlb7gLay9iH8YXj9pVqV-RApok-0RQLqeVYVpmwZBI'. }) client.getSpace().
Read more >
contentful-management - npm
Normally you get the token in the Contentful web app accessToken: ... getEnvironment('master').then((environment) => { // Now that we have an environment, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found