TwitchAPI spiel/status ändern - HttpsURLConnection

H4ckHunt3r

Erfahrenes Mitglied
Hallo zusammen,
ich versuche im moment ein Tool mit Java zu entwickeln,
welches über die TwitchAPI die Channel Informationen ändern soll.
In der TwitchAPI Dokumentation gibt es dazu eine CURL anweisung als Beispiel:
Bash:
curl -H 'Accept: application/vnd.twitchtv.v3+json' -H 'Authorization: OAuth <access_token>' \
-d "channel[status]=Playing+cool+new+game!&channel[game]=Diablo&channel[delay]=0" \
-X PUT https://api.twitch.tv/kraken/channels/test_channel
diesen habe ich versucht in Java via HttpsURLConnection umzusetzen.
Java:
package me.confu5ed.twitch.gametracker;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLEncoder;
import javax.net.ssl.HttpsURLConnection;


// Lib: http://commons.apache.org/proper/commons-io/
import org.apache.commons.io.*;
// Lib: http://mvnrepository.com/artifact/org.json/json/20141113
import org.json.*;


/**
 * Utility Class for Updating Twitch Channel via TwitchAPI.
 * @version 1.0
 * @author Marcel Naeve <me@CoNfu5eD.me>
 */
public final class TwitchChannelUpdate {
   
   /**
    * APIKey (OAuth2.0 Token) for TwitchAPI: channel_edit scale needed.
    */
   public static String APIKey = "";
   
   /**
    * Request URL - TwitchAPI should be constant until the API get updated.
    */
   private static String stringUrl = "https://api.twitch.tv/kraken/channels/";
   
   /**
    * Twitch Channel Name.
    */
   public static String channel = "";
   
   /**
    * Update Channel Information by TwitchAPI.
    * @param key what will be updated? (game/status/delay)
    * @param data newValue
    * @return boolean update success=>true/failed=>false
    */
   public static boolean update(String key, String data) {
     
  URL url;
  String response = "";
     try {     
       // Prepare
       url = new URL(stringUrl + channel);
    String basicAuth = "OAuth " + APIKey;
    String sendData = "channel[" + URLEncoder.encode(key, "UTF-8") + "]=" + URLEncoder.encode(data, "UTF-8");
     
    // Create Request
    HttpsURLConnection apiConnection  = (HttpsURLConnection) url.openConnection();
       apiConnection.setRequestMethod("PUT");
    apiConnection.setDoOutput(true);
     
     
     apiConnection.setRequestProperty("Accept", "application/vnd.twitchtv.v3+json");
     apiConnection.setRequestProperty("X-Requested-With", "TwitchGameTracker");
     apiConnection.setRequestProperty("Authorization", basicAuth);
   
     // Connect to API and send Data
     apiConnection.connect();
     apiConnection.getOutputStream().write(sendData.getBytes("UTF-8"));
     
     
     // Read Response Data
       InputStream inputStream = null;
       System.out.println("ResponseCode: " + apiConnection.getResponseCode());
       if(apiConnection.getResponseCode() == 200)
       {
         inputStream = apiConnection.getInputStream();
       } else {
         inputStream = apiConnection.getErrorStream();
       }
       if(inputStream != null)   response = IOUtils.toString(inputStream);
       
    // Disconnect from API
    apiConnection.disconnect();
     
     } catch (IOException e) {
       // TODO API Request Failed: Permissions??
       e.printStackTrace();
       return false;
     }

     if(response.startsWith("{")) {
       JSONObject json = new JSONObject(response);   
       // TODO check data has been changed and give optical feedback to gui
    System.out.println(json.toString());
     }
     //------------------
     
  return true;
   }
   
}

Leider jedoch bekomme ich den Fehler Response Code 400 zurück und weis nicht was ich falsch mache,
bei google habe ich leider auch keine Lösung gefunden.
Ich hoffe hier kann mir jemand helfen.

Die Rückgabe der TwitchAPI:
Code:
{"error":"Bad Request","message":"Missing required parameter channel","status":400}
 
Habe nun doch selbst eine Lösung gefunden.

Ich habe einfach den URLEncoder raus geschmissen und in den daten die leerzeichen durch plus ersetzt.
Java:
String sendData = "channel[" + key + "]=" + data.replace(' ', '+');

Und dann habe ich noch den content-type auf application/x-www-form-urlencoded geändert.
Java:
apiConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

Tjoa und das wars nun funktioniert es.
 

Neue Beiträge

Zurück