Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to getEnvironments from space #236

Closed
snurby7 opened this issue Sep 25, 2019 · 6 comments
Closed

Unable to getEnvironments from space #236

snurby7 opened this issue Sep 25, 2019 · 6 comments

Comments

@snurby7
Copy link

snurby7 commented Sep 25, 2019

Using the content-management api and looking at the docs getEnvironments() here there is a missing piece in the URL created.

Per the docs the url should be /spaces/{space_id}/environments

Instead what is sent dropped the {space_id} and instead queries to /spaces/environments

Using the latest package contentful-management@5.11.2

@jroehl
Copy link
Contributor

jroehl commented Sep 26, 2019

Hi @snurby7,
are you trying to getEnvironments with a browser, or nodejs?

This should work (and works for me):

const contentful = require('contentful-management');

const client = contentful.createClient({ accessToken: '<access_token>' });
client.getSpace('<space_id>')
  .then(space => space.getEnvironment('master'))
  .then(environment => environment.getEntries())
  .then(console.log)
  .catch(console.error);

@snurby7
Copy link
Author

snurby7 commented Sep 26, 2019

Hey @jroehl,

Yeah I'm running this on a little node server. Just so we're on the same page what version of contentful-management are you running?

Here's an example of how I'm trying to call it.

export class ContentManager {
  private client: ClientAPI;

  constructor() {
    this.client = createClient({
      // This is the access token for this space. Normally you get the token in the Contentful web app
      accessToken: ContentfulAccessToken
    });
  }

  public async getSpaces(): Promise<Collection<Space>> {
    return this.client.getSpaces();
  }

  public async getEntriesForSpace(spaceId: string): Promise<any> {
    const space = await this.client.getSpace(spaceId);
    const environment = await space.getEnvironment("master");
    const entries = await environment.getEntries();
    console.log(entries);
    return entries;
  }
}

When I call getEntriesForSpace it still errors out for me. I tried switching my node version to 8.x and it still won't work and returns me the below (shortened a little)

"status": 404,
  "statusText": "Not Found",
  "message": "The resource could not be found.",
  "details": {
    "type": "Space",
    "id": "environments"
  },
  "request": {
    "url": "https://api.contentful.com:443/spaces/environments/master",
...

@snurby7
Copy link
Author

snurby7 commented Sep 26, 2019

All good, I made a terrible mistake higher up the chain.

@snurby7 snurby7 closed this as completed Sep 26, 2019
@mkollers
Copy link

Can you share your "terrible mistake" - I think I am doing the same :-(

@snurby7
Copy link
Author

snurby7 commented May 24, 2022

Can you share your "terrible mistake" - I think I am doing the same :-(

If I knew what it was I'd tell you, probably just some bad logic where I was calling on the wrong environment or not waiting for something, sadly I don't have access to that old code anymore :/

@KristofaJosh
Copy link

it's easy to mistakenly use the access token in Settings - API Keys (content delivery) vs Settings - CMA tokens.
Make sure to create a personal access token in contentful and use that instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants