I’m trying to connect to the Twitch API using Java but keep getting a 403 error saying password grant is not allowed for my client ID. Here’s the code I’m using:
package streaming.auth.handler;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
public class TwitchAuthenticator {
public static void main(String[] args) throws URISyntaxException, ClientProtocolException, IOException {
HttpPost authRequest = new HttpPost("https://api.twitch.tv/kraken/oauth2/token");
org.apache.http.client.HttpClient httpClient = new DefaultHttpClient();
ArrayList<NameValuePair> authParams = new ArrayList<NameValuePair>();
authParams.add(new BasicNameValuePair("grant_type", "password"));
authParams.add(new BasicNameValuePair("client_id", Config.appId));
authParams.add(new BasicNameValuePair("client_secret", Config.appSecret));
authParams.add(new BasicNameValuePair("username", Config.userLogin));
authParams.add(new BasicNameValuePair("password", Config.userPassword));
authParams.add(new BasicNameValuePair("scope", "user_read"));
authRequest.setEntity(new UrlEncodedFormEntity(authParams));
HttpResponse response = httpClient.execute(authRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String responseData = "";
while ((responseData = reader.readLine()) != null) {
System.out.println(responseData);
}
}
}
The error message I get is: {"status":403,"message":"Password grant not permitted for client ID xyz123abc","error":"Forbidden"}
I followed the documentation but it still doesn’t work. What could be causing this issue?