Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: ravenlord on June 13, 2011, 07:37:14 PM



Title: Accessing MT Gox API from Java
Post by: ravenlord on June 13, 2011, 07:37:14 PM
Hi there,

I currently try to access the MT Gox API from a Java-Tool. No matter which code I use, it always results in a "{"error":"Not logged in."}" error. Therefore I hope, that you can help me...

I think, the following code should result in a correct POST-Request.
_url = https://mtgox.com:443/code/getFunds.php
_postcontent = name=blah&pass=blah

Code:
urlConnection = (HttpsURLConnection) (new URL(_url).openConnection());
           
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
           
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-length", String.valueOf(_postcontent.length()));
urlConnection.setRequestProperty("Content-Type", "application/x-www- form-urlencoded");

dataOutputStream = new DataOutputStream(urlConnection.getOutputStream());
LOGGER.debug("writeBytes {}", _postcontent);
dataOutputStream.writeBytes(_postcontent);
dataOutputStream.flush();
dataOutputStream.close();

I would be very happy, if somebody could help me out of this!

Thanks a lot.


Title: Re: Accessing MT Gox API from Java
Post by: vrotaru on June 14, 2011, 04:25:35 AM
"POST" or "GET"? I was only reading their ticker from JAVA, and it worked with "GET"

Code:
String tickerURL = "https://mtgox.com/code/ticker.php";

URL url = new URL(tickerURL);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()));
String line = bufferedReader.readLine();
bufferedReader.close();


Title: Re: Accessing MT Gox API from Java
Post by: ravenlord on June 14, 2011, 07:29:31 PM
MtGox requires POST requests for all API-Actions, which need a login.  Only the three open action can be performed with a get-Request.


Title: Re: Accessing MT Gox API from Java
Post by: holgero on July 03, 2011, 01:18:13 PM
This works for me:
Code:
        final URL url = new URL("https://mtgox.com/code/getFunds.php");
        final HttpsURLConnection urlConn = (HttpsURLConnection) url.openConnection();
        urlConn.setDoInput(true);
        urlConn.setDoOutput(true);
        urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        urlConn.setRequestMethod("POST");
        final DataOutputStream printout = new DataOutputStream(urlConn.getOutputStream());
        final String content = "name=" + URLEncoder.encode(USERNAME, "UTF-8") + "&pass=" + URLEncoder.encode(PASSWORD, "UTF-8");
        printout.writeBytes(content);
        printout.flush();
        printout.close();

Did you forget to URLEncode your username and password? Or is the typo where you set the "Content-Type" to
Code:
"application/x-www- form-urlencoded"
really there?