I’m trying to authenticate with the Twitch API using the password grant flow, but I keep getting a 403 error saying “Password grant not permitted”. Here’s my Java code:
package com.streaming.auth;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Arrays;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
public class TwitchAuthenticator {
public static void main(String[] args) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost authRequest = new HttpPost("https://api.twitch.tv/kraken/oauth2/token");
List<NameValuePair> authParams = Arrays.asList(
new BasicNameValuePair("grant_type", "password"),
new BasicNameValuePair("client_id", AppConfig.CLIENT_ID),
new BasicNameValuePair("client_secret", AppConfig.CLIENT_SECRET),
new BasicNameValuePair("username", AppConfig.USER_NAME),
new BasicNameValuePair("password", AppConfig.USER_PASSWORD),
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);
}
httpClient.close();
}
}
The error message I’m receiving is:
{“status”:403,“message”:“Password grant not permitted for client ID xyz123”,“error”:“Forbidden”}
I followed the documentation but can’t figure out why this authentication method isn’t working. Any ideas what could be causing this issue?