I’m trying to authenticate with the Twitch API using the password grant flow but keep running into issues. Here’s my Java implementation:
package streaming.auth.service;
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.userPass));
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 output = "";
while ((output = reader.readLine()) != null) {
System.out.println(output);
}
}
}
The response I get back is:
{“status”:403,“message”:“Password grant not permitted for client ID xyz123abc456”,“error”:“Forbidden”}
I followed the documentation but still can’t figure out what’s going wrong. Has anyone encountered this before? What could be causing this authentication failure?