[BEGINNER] Authentication for Java App

Hi guys, im starting to programing with this API and i want to make an Android apk, but first i try the code i want to use in Java mode. I was able to get some information with the API but the problem was when i try to get de Subscribers of an User. After searching i discover that i have to make an Authentication page, so i do the following:
1- Download this https://github.com/mb3364/Java-Twitch-Api-Wrapper and using it as a Lib.
2- Test this simple code to make sure im doing a good work:

Twitch twitch = new Twitch();
	twitch.setClientId("Myid");
	URI callbackUri = null;
	try {
		callbackUri = new URI("http://127.0.0.1:23522/token/");
	} catch (URISyntaxException e) {
		e.printStackTrace();
	}
	
	String authUrl = twitch.auth().getAuthenticationUrl(twitch.getClientId(), callbackUri, Scopes.USER_READ);
	System.out.println(authUrl);
	try {
		String url = authUrl + "&force_verify=true";
		java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
	} catch (java.io.IOException e) {
		System.out.println(e.getMessage());
	}
	
	boolean authSuccess = twitch.auth().awaitAccessToken();
	
	if (authSuccess) {
		  String accessToken = twitch.auth().getAccessToken();
		  System.out.println("Access Token: " + accessToken);
	} else {
		  System.out.println(twitch.auth().getAuthenticationError());
	}

But the problem is that i never reach that final If because is always waiting.
Any help will be welcome.

Thanks for the help.

At what point is it getting stuck? Your Java app should open a browser window pointing to Twitch. You should permit the app access, which redirects you back to the app’s callbackUri, which tells the app the it’s found the token and the authorization should be successful.

If you never get a browser window, then your code is breaking when it tries to open a browser. If it never gets past authorizing the app with Twitch (ie you’re redirected back and nothing happens), java.awt.Desktop.getDesktop().browse may be a blocking call that you need to initiate in a separate thread.

The function twitch.auth().awaitAccessToken(); never ends, it keeps waiting, but never returns true or false even when i can see the token access in the browser. Can it be because of my Redirect URI? I have “http://127.0.0.1:23522/token/” but i dont know if is a good practice or not.

Ok, i´ve solved it, i will post here the solution for any1 who got the same misstake if you are using Api-Twitch-Wrapper( https://github.com/mb3364/Java-Twitch-Api-Wrapper ):

1- Change your Redirect Uri in you Twitch account to: http://127.0.0.1:23522/authorize.html
2- Go to your project and change the callbackUri variable to the same string: http://127.0.0.1:23522/authorize.html

Thats all, it was a simple error, but it consumed some time until i realised it.

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