REST API: {"error": "Token does not have required scope"}

I want to access project information via API to get the name of the project. I do that by using this link from the API documentation

https://playcanvas.com/api/projects/:projectID

It works perfectly when using this link on a non-private project.
But when i use this link on a private project i get the response: {“error”: “Token does not have required scope”}. The access token has admin premission and the account linked to it has also owner and admin premission. I dont know why or how to fix this.

Btw: i can use this link also to get all the assets, and folders but not the project information?

Any pointers?

On mobile so can’t check offhand, I know there are some differences with API tokens created from the ORG and the User dashboard.

Can you try again with the API token that is created from the user account please if you aren’t already.

I will check the API once I’m back in the office tomorrow

I don’t see that endpoint in our documentation REST API | Learn PlayCanvas

Can you share which endpoint you are using in the documentation please? If it’s not in the documentation, then it’s not supported with your API token. The reason why it works with public projects is because that information is public to all (no need for authentication).

Getting the list of assets works because it’s a supported endpoint: Assets - List assets | Learn PlayCanvas

I see now, the function i was using was to download the compiled app i think?
https://developer.playcanvas.com/en/user-manual/api/app-download/

But now i see i just wanted to download all project files which could be this function for a complete zip.
https://developer.playcanvas.com/en/user-manual/api/project-archive/

But when using the link
https://playcanvas.com/api/projects/:projectid/export
it gives 404 not found, and if i check jobs it gives a 401.

probably need to do a proper request, and the api is okay, but the naming could be more concrete.

So this end point? Projects - Archive project | Learn PlayCanvas

If so, can you provide the code/command line that you are using to call the REST API to double check please?

If it’s 404, double check that you are using the project id and not the scene id.

We also have a small set of commandline tools to use the REST API with here: GitHub - playcanvas/playcanvas-rest-api-tools: A set of tools to use with the PlayCanvas REST API for common jobs such as downloading a build and archiving a project

example code:


let accessToken = "aASDASDADQ#rwqrqwrq123ewqrVSD";

function downloadProject() {

  const data = JSON.stringify({
    project_id: projectId,
    name: projectData.name
  });
  
  const options = {
    hostname: 'playcanvas.com',
    path: '/api/apps/download',
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${accessToken}`,
      'Content-Type': 'application/json',
    }
  };
  
  const req = https.request(options, res => {
    console.log(`statusCode: ${res.statusCode}`);
  
    let chunks = [];
    res.on('data', chunk => {
      chunks.push(chunk);
    });
  
    res.on('end', () => {
      let body = Buffer.concat(chunks);
      console.log(body.toString());
    });
  });
  
  req.on('error', error => {
    console.error(error);
  });
  
  req.write(data);
  req.end();
}

I ran the script and the only issue I saw is that the error message returned is

statusCode: 400
{"error":"scenes: missing value"}

After I added the scene id, it gave me a job id to poll.

Full script that I ran via node.js

const https = require('node:https');

let accessToken = "XXXXXXXXXX";

function downloadProject() {

  const data = JSON.stringify({
    project_id: 472797,
    name: "Some Project",
    scenes: [513888]
  });

  const options = {
    hostname: 'playcanvas.com',
    path: '/api/apps/download',
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${accessToken}`,
      'Content-Type': 'application/json',
    }
  };

  const req = https.request(options, res => {
    console.log(`statusCode: ${res.statusCode}`);

    let chunks = [];
    res.on('data', chunk => {
      chunks.push(chunk);
    });

    res.on('end', () => {
      let body = Buffer.concat(chunks);
      console.log(body.toString());
    });
  });

  req.on('error', error => {
    console.error(error);
  });

  req.write(data);
  req.end();
}

downloadProject();

Gave me the response (edited to remove private data):

{"id":1,"scope":{"type":"user","id":1},"created_at":"2023-05-10T14:55:36.567Z","modified_at":"2023-05-10T14:55:36.567Z","status":"running","messages":[],"data":{"owner_id":1,"project_id":472797,"branch_id":"XXXXXXXXX","name":"Some Project","scenes":["513888"],"concatenate":false,"minify":false,"sourcemaps":false,"optimize_scene_format":false,"preload_bundle":false}}

It may be worth trying a new REST API token just to be sure that’s not the issue too

I fixed the problem but i came across a new one, in which i get all the assets from project, using ?limit=1000 and no branchid, i get all files including folders.

but when i use this endpoint i only get the scripts, not really the result im expecting? im a bit confused

https://playcanvas.com/api/projects/647497/assets/?branchId=9a9d8fb3-aa44-47e6-a7de-8b335d295ef4

Or wait is it just the files that are different from master?

I have no idea but adding &limit=1000 just fixes everything

It looks like the documentation is inconplete there as the results are paginated (see example response on Assets - List assets | Learn PlayCanvas) but doesn’t tell you how to skip pages :thinking:

@yak32 or @slimbuck , is this something you can look at please?

IIRC, the limit param is the max number of assets to return at once. So even using 1000, means that if you have more than 1000 assets, you still need to load the second page of results to get them

2 Likes

I’ve updated the documentation as a PR that explains the use of skip and limit params

2 Likes