Discuss Scratch

spaceyour
Scratcher
81 posts

Scratch post api how to make ?

Hello ! Nowadays I'm trying to write a project using scratch api(https://en.scratch-wiki.info/wiki/Scratch_API) . I want to make a project like this: https://scratch.mit.edu/projects/509531164/ (This is very cool How can I do that. I know I need to use Node js. In short, how can I make a POST request to change the project title?

Last edited by spaceyour (June 29, 2021 13:29:41)

awesome-llama
Scratcher
1000+ posts

Scratch post api how to make ?

What language are you writing this in?

You should try searching up how to make a POST request in the language you are using.

Last edited by awesome-llama (June 29, 2021 13:45:14)

spaceyour
Scratcher
81 posts

Scratch post api how to make ?

awesome-llama wrote:

What language are you writing this in?

You should try searching up how to make a POST request in the language you are using.
I am using Node js and I know how to make post request. But where do I need to send post request?
ProgrammerDE
Scratcher
100+ posts

Scratch post api how to make ?

You have to post to https://api.scratch.mit.edu/projects/ID.
The body should define a title, e.g. “This project has 1000 Views”

Here's the request I did:

fetch("https://api.scratch.mit.edu/projects/549288606", {
  "headers": {
    "accept": "application/json",
    "accept-language": "de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7",
    "content-type": "application/json",
    "sec-ch-ua": "\" Not;A Brand\";v=\"99\", \"Google Chrome\";v=\"91\", \"Chromium\";v=\"91\"",
    "sec-ch-ua-mobile": "?0",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-site",
    "x-token": "SECRET TOKEN"
  },
  "referrer": "https://scratch.mit.edu/",
  "referrerPolicy": "strict-origin-when-cross-origin",
  "body": "{\"title\":\"Test\"}",
  "method": "PUT",
  "mode": "cors",
  "credentials": "omit"
});

Instead of SECRET TOKEN you have to write the project token you get if you open the developer tools on the projects (F12 or Ctrl + Shift + i or …). Then go to the network tab and change the projects title (not on the dev tools). You should find a thing in the network tab with your project id. Right-click, copy > Copy as fetch and you have the thing I got above with the id and token.

I'm btw not the best at POST and these things.

Last edited by ProgrammerDE (June 29, 2021 14:20:32)

spaceyour
Scratcher
81 posts

Scratch post api how to make ?

ProgrammerDE wrote:

You have to post to https://api.scratch.mit.edu/projects/ID.
The body should define a title, e.g. “This project has 1000 Views”

Here's the request I did:

fetch("https://api.scratch.mit.edu/projects/549288606", {
  "headers": {
    "accept": "application/json",
    "accept-language": "de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7",
    "content-type": "application/json",
    "sec-ch-ua": "\" Not;A Brand\";v=\"99\", \"Google Chrome\";v=\"91\", \"Chromium\";v=\"91\"",
    "sec-ch-ua-mobile": "?0",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-site",
    "x-token": "SECRET TOKEN"
  },
  "referrer": "https://scratch.mit.edu/",
  "referrerPolicy": "strict-origin-when-cross-origin",
  "body": "{\"title\":\"Test\"}",
  "method": "PUT",
  "mode": "cors",
  "credentials": "omit"
});

Instead of SECRET TOKEN you have to write the project token you get if you open the developer tools on the projects (F12 or Ctrl + Shift + i or …). Then go to the network tab and change the projects title (not on the dev tools). You should find a thing in the network tab with your project id. Right-click, copy > Copy as fetch and you have the thing I got above with the id and token.

I'm btw not the best at POST and these things.
I don't quite understand what you mean by secret. I actually tried to use a repository like this: https://www.npmjs.com/package/scratch-api but I couldn't change the project title using it. Is SECRET a similar .scratchsession ID?
ProgrammerDE
Scratcher
100+ posts

Scratch post api how to make ?

If someone has the TOKEN of the project, he can do what he want with it - Change the title or other stuff.
spaceyour
Scratcher
81 posts

Scratch post api how to make ?

ProgrammerDE wrote:

If someone has the TOKEN of the project, he can do what he want with it - Change the title or other stuff.
So where can I get the token of my own project? Finally, is there a document of the json you wrote in the body part? For example, what can I do if I want to change the project description like this? It's like I asked a little too many questions I would be glad if you answer.
ProgrammerDE
Scratcher
100+ posts

Scratch post api how to make ?

1. How to get the token
You need to open the dev tools by pressing F12 or Ctrl + Shift + i for example.
Go inside there in the network tab.
Then go to your browser with the project opened and change the title to something.
Go to the network tab again and look there: It should contain two entries now: 548537669 and the same again e.g.
Right-click on one of them, click Copy, Copy as fetch.
Go to the console tab of the dev tools.
Paste the code you copied in there.
You should have something similar to my code above. But this time with the token.
To test it out, change the name of the project (in here Test)
"body": "{\"title\":\"Test\"}",
to anything and hit enter. If you reload the page,
the title should have changed.

2. How to change the description
Easy. In body add rather instructions or description. For example:
"body": "{\"title\":\"titletest\", \"instructions\":\"test\", \"description\":\"test2\"}",

Any more questions? ;)
ProgrammerDE
Scratcher
100+ posts

Scratch post api how to make ?

Now let's try to make a little script for that…
spaceyour
Scratcher
81 posts

Scratch post api how to make ?

ProgrammerDE wrote:

1. How to get the token
You need to open the dev tools by pressing F12 or Ctrl + Shift + i for example.
Go inside there in the network tab.
Then go to your browser with the project opened and change the title to something.
Go to the network tab again and look there: It should contain two entries now: 548537669 and the same again e.g.
Right-click on one of them, click Copy, Copy as fetch.
Go to the console tab of the dev tools.
Paste the code you copied in there.
You should have something similar to my code above. But this time with the token.
To test it out, change the name of the project (in here Test)
"body": "{\"title\":\"Test\"}",
to anything and hit enter. If you reload the page,
the title should have changed.

2. How to change the description
Easy. In body add rather instructions or description. For example:
"body": "{\"title\":\"titletest\", \"instructions\":\"test\", \"description\":\"test2\"}",

Any more questions? ;)
Thank you very much mate (: I think I need to look at the body part here and write it https://api.scratch.mit.edu/users/spaceyour/projects/446610554. Thank you again.
ProgrammerDE
Scratcher
100+ posts

Scratch post api how to make ?

FINALLY! It's maybe very long (26 Lines, because I never worked with NodeJS, PUT and these things), but it's working:

const fetch = require('node-fetch');
fetch("https://api.scratch.mit.edu/projects/ID")
  .then(res => res.json())
  .then(json => set(json.stats.views));
function set(views) {
  fetch("https://api.scratch.mit.edu/projects/ID", {
    "headers": {
      "accept": "application/json",
      "accept-language": "de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7",
      "content-type": "application/json",
      "sec-ch-ua": "\" Not;A Brand\";v=\"99\", \"Google Chrome\";v=\"91\", \"Chromium\";v=\"91\"",
      "sec-ch-ua-mobile": "?0",
      "sec-fetch-dest": "empty",
      "sec-fetch-mode": "cors",
      "sec-fetch-site": "same-site",
      "x-token": "SECRET TOKEN"
    },
    "referrer": "https://scratch.mit.edu/",
    "referrerPolicy": "strict-origin-when-cross-origin",
    "body": "{\"title\":\"" + views + " Views\"}",
    "method": "PUT",
    "mode": "cors",
    "credentials": "omit"
  });
  // FOR DEBUGGING: console.log(views)
}

You need the node-fetch libary.

You only have to change the ID and SECRET TOKEN. You can also use loves or favorites and you can print them in the title, instructions or description.
(And maybe in the comments, but that would probably be spam)

Hope that will solve the problem!

Last edited by ProgrammerDE (June 29, 2021 16:45:30)

spaceyour
Scratcher
81 posts

Scratch post api how to make ?

ProgrammerDE wrote:

FINALLY! It's maybe very long (26 Lines, because I never worked with NodeJS, PUT and these things), but it's working:

const fetch = require('node-fetch');
fetch("https://api.scratch.mit.edu/projects/ID")
  .then(res => res.json())
  .then(json => set(json.stats.views));
function set(views) {
  fetch("https://api.scratch.mit.edu/projects/ID", {
    "headers": {
      "accept": "application/json",
      "accept-language": "de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7",
      "content-type": "application/json",
      "sec-ch-ua": "\" Not;A Brand\";v=\"99\", \"Google Chrome\";v=\"91\", \"Chromium\";v=\"91\"",
      "sec-ch-ua-mobile": "?0",
      "sec-fetch-dest": "empty",
      "sec-fetch-mode": "cors",
      "sec-fetch-site": "same-site",
      "x-token": "SECRET TOKEN"
    },
    "referrer": "https://scratch.mit.edu/",
    "referrerPolicy": "strict-origin-when-cross-origin",
    "body": "{\"title\":\"" + views + " Views\"}",
    "method": "PUT",
    "mode": "cors",
    "credentials": "omit"
  });
  // FOR DEBUGGING: console.log(views)
}

You need the node-fetch libary.

You only have to change the ID and SECRET TOKEN. You can also use loves or favorites and you can print them in the title, instructions or description.
(And maybe in the comments, but that would probably be spam)

Hope that will solve the problem!
Now I'm trying to do it with javascript on the frontend. But I got an error for some reason. Is it possible to replace it with a remote site like W3schools. This is my code
fetch("https://api.scratch.mit.edu/projects/435133524", {
"headers": {
"accept": "application/json",
"accept-language": "tr",
"content-type": "application/json",
"sec-ch-ua": "\" Not;A Brand\";v=\"99\", \"Microsoft Edge\";v=\"91\", \"Chromium\";v=\"91\"",
"sec-ch-ua-mobile": "?0",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-site",
"x-token": "Token"
},
"referrer": "https://scratch.mit.edu/",
"referrerPolicy": "strict-origin-when-cross-origin",
"body": "{\"title\":\"Tam sayı uygulaması Denemesi\"}",
"method": "PUT",
"mode": "cors",
"credentials": "omit"
});
spaceyour
Scratcher
81 posts

Scratch post api how to make ?

const fetch = require('node-fetch');
fetch("https://api.scratch.mit.edu/projects/ID")
  .then(res => res.json())
  .then(json => set(json.stats.views));
function set(views) {
  fetch("https://api.scratch.mit.edu/projects/ID", {
    "headers": {
      "accept": "application/json",
      "accept-language": "de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7",
      "content-type": "application/json",
      "sec-ch-ua": "\" Not;A Brand\";v=\"99\", \"Google Chrome\";v=\"91\", \"Chromium\";v=\"91\"",
      "sec-ch-ua-mobile": "?0",
      "sec-fetch-dest": "empty",
      "sec-fetch-mode": "cors",
      "sec-fetch-site": "same-site",
      "x-token": "SECRET TOKEN"
    },
    "referrer": "https://scratch.mit.edu/",
    "referrerPolicy": "strict-origin-when-cross-origin",
    "body": "{\"title\":\"" + views + " Views\"}",
    "method": "PUT",
    "mode": "cors",
    "credentials": "omit"
  });
  // FOR DEBUGGING: console.log(views)
}

You need the node-fetch libary.

You only have to change the ID and SECRET TOKEN. You can also use loves or favorites and you can print them in the title, instructions or description.
(And maybe in the comments, but that would probably be spam)

Hope that will solve the problem!

yes I finally did Here is my project https://scratch.mit.edu/projects/549372772/
For now, the project is not uptime and is detecting late, but okay. Thanks man

Last edited by spaceyour (June 30, 2021 12:25:04)

ProgrammerDE
Scratcher
100+ posts

Scratch post api how to make ?

No problem, I have to thank you, because without you I wouldN't have started programming with NodeJS and fetch requests!
cookieOS1-0
Scratcher
1 post

Scratch post api how to make ?

Scratch cat is Cute
cosmosaura
Scratch Team
1000+ posts

Scratch post api how to make ?

Topic closed due to necroposting.

Powered by DjangoBB