Discuss Scratch

TheGamer-
Scratcher
100+ posts

New(er) API documentation

I've been trying to find some documentation for the new(er) api (api.scratch…), but what I could find didn't have many of the features of the old one shown (ie posting comments, following etc). Is there a topic for this somewhere or have they just not been migrated (yet?). Sorry if I've just managed to miss it somehow

232323.XFTQ.2011 | XqzkBEpogUjTtMDjTEyI
_nix
Scratcher
1000+ posts

New(er) API documentation

Those just haven't been migrated yet.

The official Scratch API documentation is here; I sort-of not-really maintain an unofficial wiki here.

══ trans autistic lesbian enbydoggirls // 16 17 18 19 20, she/they
sparrows one word to the paragraph // <3 // ~(quasar) nebula
NitroCipher
Scratcher
500+ posts

New(er) API documentation

@_nix Is there any way to read/write data to/from the backpack?

I hope my post helped you in some way! Post count: 500+

Current project: [s3Blocks: scratchblocks rewritten for Scratch 3.0] ::#4b4a60 //https://scratch.mit.edu/discuss/topic/290031/ Basically done!
This is my signature identifier “aWFtbml0cm9jaXBoZXI=”
blob8108
Scratcher
1000+ posts

New(er) API documentation

Do the new APIs have CORS? The scratchblocks generator is sad

tosh · slowly becoming a grown-up adult and very confused about it
herohamp
Scratcher
1000+ posts

New(er) API documentation

blob8108 wrote:

Do the new APIs have CORS? The scratchblocks generator is sad
Just self host crossorigin.me, or use http://crossorigin.mooredrive.pw/
jokebookservice1
Scratcher
1000+ posts

New(er) API documentation

NitroCipher wrote:

@_nix Is there any way to read/write data to/from the backpack?
Not with the new API, I don't think… but you can use the old API.

GET https://scratch.mit.edu/internalapi/USERNAME/get/
with session cookie

and

POST https://scratch.mit.edu/internalapi/USERNAME/set/
with X-CSRFToken and session cookie and JSON payload as body
blob8108
Scratcher
1000+ posts

New(er) API documentation

herohamp wrote:

Just self host crossorigin.me
Thanks, but that didn’t answer my question. I am aware of crossorigin.me

tosh · slowly becoming a grown-up adult and very confused about it
NitroCipher
Scratcher
500+ posts

New(er) API documentation

jokebookservice1 wrote:

NitroCipher wrote:

@_nix Is there any way to read/write data to/from the backpack?
Not with the new API, I don't think… but you can use the old API.

GET https://scratch.mit.edu/internalapi/USERNAME/get/
with session cookie

and

POST https://scratch.mit.edu/internalapi/USERNAME/set/
with X-CSRFToken and session cookie and JSON payload as body

I cant seem to get this to work. How could i accomplish this with javascript?

Last edited by NitroCipher (Dec. 11, 2017 20:48:02)


I hope my post helped you in some way! Post count: 500+

Current project: [s3Blocks: scratchblocks rewritten for Scratch 3.0] ::#4b4a60 //https://scratch.mit.edu/discuss/topic/290031/ Basically done!
This is my signature identifier “aWFtbml0cm9jaXBoZXI=”
jokebookservice1
Scratcher
1000+ posts

New(er) API documentation

Which one are you using, get or set? What code are you currently trying? Where are you running the code?
NitroCipher
Scratcher
500+ posts

New(er) API documentation

jokebookservice1 wrote:

Which one are you using, get or set? What code are you currently trying? Where are you running the code?
alert("test");
$.get("https://scratch.mit.edu/internalapi/NitroCipher/get/", function(data, status){
    alert("Data: " + data + "\nStatus: " + status);
});
alert("test2");

I have a button event that triggers this in a userscript. Both alerts surrounding the get function properly.

I hope my post helped you in some way! Post count: 500+

Current project: [s3Blocks: scratchblocks rewritten for Scratch 3.0] ::#4b4a60 //https://scratch.mit.edu/discuss/topic/290031/ Basically done!
This is my signature identifier “aWFtbml0cm9jaXBoZXI=”
jokebookservice1
Scratcher
1000+ posts

New(er) API documentation

Excuse me, that was a mistake on my part

The get request is actually
GET https://scratch.mit.edu/internalapi/backpack/NitroCipher/get/
NitroCipher
Scratcher
500+ posts

New(er) API documentation

jokebookservice1 wrote:

Excuse me, that was a mistake on my part

The get request is actually
GET https://scratch.mit.edu/internalapi/backpack/NitroCipher/get/

How would I add this:
{"type": "script", "name": "Exported from Resurgence", "scripts": [[["procDef", "color %c", ["color"], [""], false]]]}
to the backpack?

I hope my post helped you in some way! Post count: 500+

Current project: [s3Blocks: scratchblocks rewritten for Scratch 3.0] ::#4b4a60 //https://scratch.mit.edu/discuss/topic/290031/ Basically done!
This is my signature identifier “aWFtbml0cm9jaXBoZXI=”
jokebookservice1
Scratcher
1000+ posts

New(er) API documentation

Okay, so you'll want to extract the CSRF middleware token. You can find it in document.cookie, and do some RegExes or something to extract it.

POST https://scratch.mit.edu/internalapi/backpack/NitroCipher/set/

The body you will want is your JSON.

You will need to add this header

X-CSRFToken: TOKENHERE

And that should work.
NitroCipher
Scratcher
500+ posts

New(er) API documentation

jokebookservice1 wrote:

Okay, so you'll want to extract the CSRF middleware token. You can find it in document.cookie, and do some RegExes or something to extract it.

POST https://scratch.mit.edu/internalapi/backpack/NitroCipher/set/

The body you will want is your JSON.

You will need to add this header

X-CSRFToken: TOKENHERE

And that should work.


How would all of this look in JS? (I am a noob dev)

I hope my post helped you in some way! Post count: 500+

Current project: [s3Blocks: scratchblocks rewritten for Scratch 3.0] ::#4b4a60 //https://scratch.mit.edu/discuss/topic/290031/ Basically done!
This is my signature identifier “aWFtbml0cm9jaXBoZXI=”
NitroCipher
Scratcher
500+ posts

New(er) API documentation

NitroCipher wrote:

How would all of this look in JS? (I am a noob dev)

Halp

I hope my post helped you in some way! Post count: 500+

Current project: [s3Blocks: scratchblocks rewritten for Scratch 3.0] ::#4b4a60 //https://scratch.mit.edu/discuss/topic/290031/ Basically done!
This is my signature identifier “aWFtbml0cm9jaXBoZXI=”
jokebookservice1
Scratcher
1000+ posts

New(er) API documentation

Another mistake from me.. you're supposed to use the CDN server for setting. I'm unsure, but I think the following should work.
$.ajax({
url: "https://cdn.scratch.mit.edu/internalapi/backpack/NitroCipher/set/",
headers: {
"X-CSRFToken": document.cookie.match(/scratchcsrftoken=([^;]+)/)[1]
},
data: JSON.stringify({"type": "script", "name": "Exported from Resurgence", "scripts": [[["procDef", "color %c", ["color"], [""], false]]]})
})

I'm basing all this from https://github.com/trumank/scratch-api/blob/master/scratchapi.js
NitroCipher
Scratcher
500+ posts

New(er) API documentation

jokebookservice1 wrote:

Another mistake from me.. you're supposed to use the CDN server for setting. I'm unsure, but I think the following should work.
$.ajax({
url: "https://cdn.scratch.mit.edu/internalapi/backpack/NitroCipher/set/",
headers: {
"X-CSRFToken": document.cookie.match(/scratchcsrftoken=([^;]+)/)[1]
},
data: JSON.stringify({"type": "script", "name": "Exported from Resurgence", "scripts": [[["procDef", "color %c", ["color"], [""], false]]]})
})

I'm basing all this from https://github.com/trumank/scratch-api/blob/master/scratchapi.js

Will this add it to the backpack? Or replace the current backpack data?

I hope my post helped you in some way! Post count: 500+

Current project: [s3Blocks: scratchblocks rewritten for Scratch 3.0] ::#4b4a60 //https://scratch.mit.edu/discuss/topic/290031/ Basically done!
This is my signature identifier “aWFtbml0cm9jaXBoZXI=”
MathWizz
Scratcher
100+ posts

New(er) API documentation

jokebookservice1 wrote:

Another mistake from me.. you're supposed to use the CDN server for setting. I'm unsure, but I think the following should work.
$.ajax({
url: "https://cdn.scratch.mit.edu/internalapi/backpack/NitroCipher/set/",
headers: {
"X-CSRFToken": document.cookie.match(/scratchcsrftoken=([^;]+)/)[1]
},
data: JSON.stringify({"type": "script", "name": "Exported from Resurgence", "scripts": [[["procDef", "color %c", ["color"], [""], false]]]})
})

I'm basing all this from https://github.com/trumank/scratch-api/blob/master/scratchapi.js
Uh, it shouldn't be sending a request to the CDN server. I have no idea how that happened. It works though so I'm not gonna change it right now.

NitroCipher wrote:

Will this add it to the backpack? Or replace the current backpack data?
It will replace the entire backpack.

running Chromium 42.0.2311.90 with Flash Player 15.0.0.189 on Arch Linux 3.19.5-1-ck
MathWizzJsScratch && sb.js & Amber (coming soon! maybe)
NitroCipher
Scratcher
500+ posts

New(er) API documentation

MathWizz wrote:

jokebookservice1 wrote:

Another mistake from me.. you're supposed to use the CDN server for setting. I'm unsure, but I think the following should work.
$.ajax({
url: "https://cdn.scratch.mit.edu/internalapi/backpack/NitroCipher/set/",
headers: {
"X-CSRFToken": document.cookie.match(/scratchcsrftoken=([^;]+)/)[1]
},
data: JSON.stringify({"type": "script", "name": "Exported from Resurgence", "scripts": [[["procDef", "color %c", ["color"], [""], false]]]})
})

I'm basing all this from https://github.com/trumank/scratch-api/blob/master/scratchapi.js
Uh, it shouldn't be sending a request to the CDN server. I have no idea how that happened. It works though so I'm not gonna change it right now.

NitroCipher wrote:

Will this add it to the backpack? Or replace the current backpack data?
It will replace the entire backpack.

I can't seem to get it to post

I hope my post helped you in some way! Post count: 500+

Current project: [s3Blocks: scratchblocks rewritten for Scratch 3.0] ::#4b4a60 //https://scratch.mit.edu/discuss/topic/290031/ Basically done!
This is my signature identifier “aWFtbml0cm9jaXBoZXI=”
jokebookservice1
Scratcher
1000+ posts

New(er) API documentation

Whoops! You need to stringify an array containing the backpack, I think; and not the individual item. Perhaps that's the reason why it's not working?

However, I'm interested what you mean by “won't post”. Do you mean it returns a status code other than 200; or literally a POST request isn't being made. If the latter, is another verb being used, or is no request being sent at all?

Powered by DjangoBB