Discuss Scratch

BookOwl
Scratcher
1000+ posts

How do I post on the forums from a script?

I know that I need
scratchsessionsid
and
scratchcsrftoken.
How do I get or calculate those? And how would I use that to post?

who needs signatures
ChocolatePi
Scratcher
1000+ posts

How do I post on the forums from a script?

This is a post to catch the request for submitting a post to the topic. Will edit when I find out how.

You'll need to submit a POST request to the Scratch server containing things, including:

  • Scratch session ID and cookie
  • Content of the message
  • URL, etc. etc.

Last edited by ChocolatePi (April 17, 2015 19:05:52)

Zaidhaan
Scratcher
100+ posts

How do I post on the forums from a script?

Hmm.. I'll check it!

Zaidhaan
Scratcher
100+ posts

How do I post on the forums from a script?

Hmm.. I'll check it! Maybe it has something to do with HTTP requests

Last edited by Zaidhaan (April 18, 2015 14:15:14)


Superdoggy
Scratcher
1000+ posts

How do I post on the forums from a script?

BookOwl wrote:

I know that I need
scratchsessionsid
and
scratchcsrftoken.
How do I get or calculate those? And how would I use that to post?
SessionID and CSRFtoken are both pertaining to your account login, specifically. I don't think you can calculate them. If the script is just for yourself, then you can capture any post/get request and copy/paste. If the script is for other users than you, you'll have to get the token somehow. IDK how (never tried) but I think @MegaApuTurkUltra would probably know…








































MegaApuTurkUltra
Scratcher
1000+ posts

How do I post on the forums from a script?

Here's the code I use for login on my thumbnail hacker (Java + Apache Httpclient)

public class SampleText {
	static RequestConfig globalConfig;
	static CookieStore cookieStore;
	static CloseableHttpClient httpClient;
	static CloseableHttpResponse resp;
	static String csrfToken;
	public static void init() {
		globalConfig = RequestConfig.custom()
				.setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY)
				.setSocketTimeout(0).setConnectionRequestTimeout(0)
				.setConnectTimeout(0).build();
		cookieStore = new BasicCookieStore();
		BasicClientCookie lang = new BasicClientCookie("scratchlanguage", "en");
		lang.setDomain(".scratch.mit.edu");
		lang.setPath("/");
		cookieStore.addCookie(lang);
		httpClient = HttpClients
				.custom()
				.setDefaultRequestConfig(globalConfig)
				.setUserAgent(
						"Mozilla/5.0 (Windows NT 6.1; WOW64)"
								+ " AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/"
								+ "537.36").setDefaultCookieStore(cookieStore)
				.build();
	}
	public static void login(String username, char[] password) throws Exception {
		// try to look legit
		HttpUriRequest index = RequestBuilder.get()
				.setUri("https://scratch.mit.edu/")
				.addHeader("Accept", "text/html")
				.addHeader("Referer", "https://scratch.mit.edu").build();
		resp = httpClient.execute(index);
		resp.close();
		// get csrf
		HttpUriRequest csrf = RequestBuilder.get()
				.setUri("https://scratch.mit.edu/csrf_token/")
				.addHeader("Accept", "*/*")
				.addHeader("Referer", "https://scratch.mit.edu")
				.addHeader("X-Requested-With", "XMLHttpRequest").build();
		resp = httpClient.execute(csrf);
		resp.close();
		for (Cookie c : cookieStore.getCookies()) {
			if (c.getName().equals("scratchcsrftoken")) {
				csrfToken = c.getValue();
			}
		}
		// log in
		JSONObject loginObj = new JSONObject();
		loginObj.put("username", username);
		loginObj.put("password", new String(password));
		loginObj.put("captcha_challenge", "");
		loginObj.put("captcha_response", "");
		loginObj.put("embed_captcha", false);
		loginObj.put("timezone", "America/New_York");
		loginObj.put("csrfmiddlewaretoken", csrfToken);
		HttpUriRequest login = RequestBuilder
				.post()
				.setUri("https://scratch.mit.edu/login/")
				.addHeader("Accept",
						"application/json, text/javascript, */*; q=0.01")
				.addHeader("Referer", "https://scratch.mit.edu")
				.addHeader("Origin", "https://scratch.mit.edu")
				.addHeader("Content-Type", "application/json")
				.addHeader("X-Requested-With", "XMLHttpRequest")
				.addHeader("X-CSRFToken", csrfToken)
				.setEntity(new StringEntity(loginObj.toString())).build();
		resp = httpClient.execute(login);
		StringBuffer loginResp = new StringBuffer();
		InputStream in = resp.getEntity().getContent();
		int i;
		while ((i = in.read()) != -1)
			loginResp.append((char) i);
		in.close();
		resp.close();
		// check for login errors
		JSONArray response = new JSONArray(loginResp.toString());
		JSONObject obj = response.getJSONObject(0);
		String loginMessage = "Unknown";
		if (obj.has("msg"))
			loginMessage = obj.getString("msg");
		if (obj.getInt("success") != 1) {
			throw new Exception(
					"Login might have failed\nScratch returned the message:\n\""
							+ loginMessage + "\"");
		}
	}
}

As for posting, all you need to do is capture the post request when you post, and replicate it.

$(".box-head")[0].textContent = "committing AT crimes since $whenever"

Powered by DjangoBB