[JAVA] Issues formatting auth

I have a correctly formatted python version of my code that I’m currently trying to port over to Java and utilize for a robotics organization as a component to our team’s livestreaming setup. The python version can be found here. The bot handled some commands sent by users with the prefix of and exclamation point (!). However, the transition from Python to Java is a bit bumpy. The first roadblock I’ve run into is :tmi.twitch.tv NOTICE * :Improperly formatted auth. I have considered mixing and matching the ordering of messages sent to tmi.twitch.tv to check if it’s an ordering issue, but the methodology is essentially the same between Python and Java.

Here’s some code that I’m willing to pass along:

public String start() throws IOException {
   String toReturn = "";
   if(TWITCHCOMM.isConnected()) {
      PrintWriter pw = new PrintWriter(TWITCHCOMM.getOutputStream(),true);
      pw.println("PASS " + Main.OAUTH);
      pw.flush();
      pw.println("NICK " + Main.NICK);
      pw.flush();
      pw.println("JOIN #" + CHANNEL);
      pw.flush();
      BufferedReader br = new BufferedReader(new InputStreamReader(TWITCHCOMM.getInputStream()));
      String update = "";
      while((update = br.readLine()) != null && !update.toLowerCase().contains(":end of /names list")) {
         toReturn += update.indexOf("\r\n") > update.length()-10 ? update.substring(0, update.indexOf("\r\n")) : update;
         toReturn += "\n";
      }
      pw.close();
   } else {
      toReturn = "Connection Failed.";
   }
   return toReturn;
}

TWITCHCOMM is a Socket connection to irc.twitch.tv on port 6667, with keep alive set to true. The Main class is my runner class, which controls most of the data read in from a .properties file which can be edited by the user and saved to static variables that I can access from other classes without the requirement of these variables to be synchronized as they are primarily read-only anyways.

Is the OAuth token prefixed with oauth:?

Yes, the OAuth is prefixed with oauth: followed by some OAuth key, as expected.

is this writing a \r\n to the end of the line correctly?

I had forgotten to add this, but even after adding this to each command being sent out the response retrieved was still :tmi.twitch.tv NOTICE * :Improperly formatted auth. Thanks for catching that possible issue!

What’s the character encoding used? You should use UTF-8. “Improperly formatted auth” would indicate the format is not

PASS oauth:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n

I’m not totally sure if it’s UTF-8 or not. To be fair, I’m using the Java library for PrintWriter so I’d have to take a look…

I got the system working by using;

pw.println(new String("some text".getBytes(), "UTF-8"));

for the entire authentication process. So the new code looks something like this:

public String start() throws IOException {
	String toReturn = "";
	if(TWITCHCOMM.isConnected()) {
		PrintWriter pw = new PrintWriter(new OutputStreamWriter(TWITCHCOMM.getOutputStream(), StandardCharsets.UTF_8),true);
		pw.println(new String(("PASS " + (Main.isRedCase ? Main.OAUTH[0] : Main.OAUTH[1]) + Commands.END).getBytes(), "UTF-8"));
		pw.println(new String(("USER firstwa_" + (Main.isRedCase ? "red" : "blue") + Commands.END).getBytes(), "UTF-8"));
		pw.println(new String(("NICK firstwa_" + (Main.isRedCase ? "red" : "blue") + Commands.END).getBytes(), "UTF-8"));
		pw.println(new String(("JOIN #" + CHANNEL + Commands.END).getBytes(), "UTF-8"));
		pw.flush();
		System.out.println("Connector Data Sent");
		BufferedReader br = new BufferedReader(new InputStreamReader(TWITCHCOMM.getInputStream()));
		String update = "";
		while((update = br.readLine()) != null && !update.toLowerCase().contains(":end of /names list")) {
			toReturn += update.indexOf("\r\n") > update.length()-10 ? update.substring(0, update.indexOf("\r\n")) : update;
			toReturn += "\n";
		}
		pw.close();
	} else {
		toReturn = "Connection Failed.";
	}
	return toReturn;
}

Thanks all!

Hi,

I am running in the same issue here, except I am in a C++ project. To verify that I was not crazy, I event tried to create another standalon cmd line app that would just use winsocks to connect to the IRC chat and it worked, only using standard ansii strings. In our project we use some internal string class that are using ansii strings, but we always ge the “tmi.twitch.tv NOTICE * :Improperly formatted auth”. When I look at our strings in debug, I do not see the DOM anywhere (or maybe am I not looking the right way). This is kinda driving me nut…

Thx for the help.

Definitely be sure you have both \r\n in the strings being sent as well as the fact that they need to be sent using the proper character encoding, “UTF-8”. I solved my issue that way…

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.