Bitcoin Forum

Other => Beginners & Help => Topic started by: itake on June 28, 2011, 07:25:19 PM



Title: mtGox Trading API down? [solved]
Post by: itake on June 28, 2011, 07:25:19 PM
I keep getting this error: {"error":"Must be logged in"} or when I am logged into the site {"error":"Bad token"}

whenever I try this:

https://mtgox.com/code/getFunds.php?name=itake&pass=wouldn'ty0uliketokn0w

anyone else having the same problem?


Title: Re: mtGox Trading API down?
Post by: Moxed on June 28, 2011, 07:47:58 PM
yeah i do have the same issue since they are back online.

hope they bring them back soon !


Title: Re: mtGox Trading API down?
Post by: angelo95 on June 28, 2011, 07:52:21 PM
Same issue here. I had a very quick chat about this with MagicalTux on the IRC, he told me this should be fixed this weekend. But you know how they are with deadlines at Mtgox...


Title: Re: mtGox Trading API down?
Post by: CharlyBln on June 28, 2011, 07:58:00 PM
Have the same Problems.
By the way...Anyone knows an existing Java implementation für the Mt.Gox-API ? I have Problems to set a POST https-message.
If anyone has a short JAVA-script for me, i would be so thankful!


Title: Re: mtGox Trading API down?
Post by: angelo95 on June 28, 2011, 08:24:49 PM
Sorry I can't help with Java. But FYI I use Curl and it works flawlessly. There is probably an implementation for Java?


Title: Re: mtGox Trading API down?
Post by: Existence on June 28, 2011, 08:32:58 PM
down for me as well. when will mt.gox run smoothly? seems like they're always encountering problems.


Title: Re: mtGox Trading API down?
Post by: itake on June 28, 2011, 08:57:15 PM
I am thinking about just skipping over the API and just logging into the site like a "normal user."

Slightly unrelated, but would anyone be interested in me releasing my mtGox C# library?


Title: Re: mtGox Trading API down?
Post by: angelo95 on June 28, 2011, 09:26:31 PM
I am sure some will be interested in your C# library. For now I am just starting with C++...  :D Thanks anyway!


Title: Re: mtGox Trading API down?
Post by: angelo95 on June 30, 2011, 09:19:18 AM
FYI just received this message from Mtgox:

Quote
Hello,

It will be active by beginning of next week.

Sorry for the inconvenience and thank you for using our site and you have any other questions feel free to contact us by sending us an

mail.

Thanks,

MtGox.com Team


Title: Re: mtGox Trading API down?
Post by: SlipperySlope on June 30, 2011, 03:31:21 PM
I just tried my test scripts for the Mt Gox Trading API and they also gave me the reported error.  However in my development work yesterday I discovered a sequence of commands that works.  Once logged in, only the last command needs to be input.  These are using the curl linux command line utility.  And I also have a Java version that I'm now updating to recover from the reported error using this protocol.  Note that the user and password must be HTML encoded.

Code:
curl -v -k --referer https://mtgox.com https://mtgox.com/users/login

curl -v -k --referer https://mtgox.com/users/login --data "name=XXX&pass=YYY" https://mtgox.com/users/login

curl -v -k --referer https://mtgox.com/users/login https://mtgox.com/

curl -v -k --referer http://mtgox.com/ --data "name=XXX&pass=YYY" -H "Accept: application/json" https://mtgox.com/code/getFunds.php


Title: Re: mtGox Trading API down?
Post by: SlipperySlope on June 30, 2011, 03:47:59 PM
Have the same Problems.
By the way...Anyone knows an existing Java implementation für the Mt.Gox-API ? I have Problems to set a POST https-message.
If anyone has a short JAVA-script for me, i would be so thankful!

Hi.  Did you see my Java HTTPS snippet posted to your request on another thread?  If not, just respond here and I'll post my latest Java POST to get the Mt Gox funds balance - assuming that one is properly logged in.


Title: Re: mtGox Trading API down?
Post by: CharlyBln on June 30, 2011, 07:50:12 PM
Hey,

yeah, thanks for the reply in the other thread. Well, i'm getting this ticker stuff working but now i'm trying to implement the "trading" functionality, but it's hard to develop a communication with an API, which does not work...Please inform me, when you are able to communicate in JAVA with the API, for example if you can place a buy or sell.
Thanks for the help!


Title: Re: mtGox Trading API down?
Post by: angelo95 on June 30, 2011, 09:03:59 PM
I just tried my test scripts for the Mt Gox Trading API and they also gave me the reported error.  However in my development work yesterday I discovered a sequence of commands that works.  Once logged in, only the last command needs to be input.  These are using the curl linux command line utility.  And I also have a Java version that I'm now updating to recover from the reported error using this protocol.  Note that the user and password must be HTML encoded.

Code:
curl -v -k --referer https://mtgox.com https://mtgox.com/users/login

curl -v -k --referer https://mtgox.com/users/login --data "name=XXX&pass=YYY" https://mtgox.com/users/login

curl -v -k --referer https://mtgox.com/users/login https://mtgox.com/

curl -v -k --referer http://mtgox.com/ --data "name=XXX&pass=YYY" -H "Accept: application/json" https://mtgox.com/code/getFunds.php

Thanks for sharing this. I am using curl as well, fantastic little utility. FYI here below are the lines I use:

Code:
curl -s -k https://mtgox.com/code/data/ticker.php
curl -s -k -d "name=$user&pass=$pass" https://mtgox.com/code/getFunds.php
curl -s -k -d "name=$user&pass=$pass&oid=$oid&type=$type" https://mtgox.com/code/cancelOrder.php
curl -s -k -d "name=$user&pass=$pass&amount=$amount_to_buy&price=$buy" https://mtgox.com/code/buyBTC.php

So it's a bit different... but yours work at the moment, mine was working before. If it's not fixed by this weekend I will modify my code.


Title: Re: mtGox Trading API down?
Post by: angelo95 on June 30, 2011, 10:17:02 PM
OK, small update. I just realized my script was not working anymore because they added something new in the ticker ("average"). If it can save you some time... ;))


Title: Re: mtGox Trading API down?
Post by: itake on July 07, 2011, 05:31:34 PM
OK, small update. I just realized my script was not working anymore because they added something new in the ticker ("average"). If it can save you some time... ;))

does that mean the trade API is working?


Title: Re: mtGox Trading API down?
Post by: QuickTrader on July 07, 2011, 05:45:27 PM
I can confirm the "basic" api of mt gox and tradehill works

price / market / orders / buy and sell


Title: Re: mtGox Trading API down?
Post by: Coinbuck @ BTCLot on July 08, 2011, 01:15:39 AM
I get:

{"error":"Bad token"}

Regards,


Title: Re: mtGox Trading API down?
Post by: old_engineer on July 08, 2011, 03:20:43 AM
Mtgox's feed seem to go up and down all the time.  An easy way to see if the problem is you or them is to look at other sites like bitcoinity.com to see if their feed is stuck, too.


Title: Re: mtGox Trading API down?
Post by: angelo95 on July 08, 2011, 08:12:11 AM
OK, small update. I just realized my script was not working anymore because they added something new in the ticker ("average"). If it can save you some time... ;))

does that mean the trade API is working?

API was working perfectly 2 days ago. I stopped using it yesterday to update my computer but till there it was OK. However sometimes it's slow to get some feeds like balance or orders (the ones you have to log in).


Title: Re: mtGox Trading API down?
Post by: itake on July 08, 2011, 09:42:03 PM
I get:

{"error":"Bad token"}

Regards,

I just said screw it and wrote my program to use the normal user interface.


Title: Re: mtGox Trading API down?
Post by: NYConsultant on July 09, 2011, 05:11:13 AM
You guys do realize that you need to issue a POST to that url with your credentials as POST'ed variables?  Hitting it from the browser with your credentials as query string parameters will always yield that error message.

If you need some C# code to illustrate this, let me know; i've written a c# api that will talk to the MTGox api for you


Title: Re: mtGox Trading API down?
Post by: txcoin on July 09, 2011, 03:46:34 PM
@NYConsultant

I'd like to see you C# code as I am still stuck at the error must log in stage...

Would you post it here?

Otherwise, feel free to email me the cs file. I am using gmail with the same username.

Cheers
txcoin


Title: Re: mtGox Trading API down?
Post by: txcoin on July 09, 2011, 04:43:28 PM
@NYConsultant

Still interested in you version of the API. But at least I got the basics working:


For all the C# devs around here: This is the basic code to use the API as of today.

        public static string GetBalance(string userName, string password)
        {
            // parameters: name1=value1&name2=value2
            var parameters = "name=" + HttpUtility.UrlEncode(userName)
                 + "&pass=" + HttpUtility.UrlEncode(password);

            var req = (HttpWebRequest) WebRequest.Create("https://mtgox.com/code/getFunds.php");

            req.ContentType = "application/x-www-form-urlencoded";
            req.Method = "POST";
            req.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
            req.Accept = "application/json";

            byte[] bytes = Encoding.ASCII.GetBytes(parameters);
            req.ContentLength = bytes.Length;

            Stream os = req.GetRequestStream();
            os.Write(bytes, 0, bytes.Length);
            os.Close();

            WebResponse resp = req.GetResponse();

            try
            {
                var sr = new StreamReader(resp.GetResponseStream());
                return sr.ReadToEnd().Trim();
            }
            catch (Exception ex)
            {
                Logger.Fatal(ex.Message);
                Logger.FatalException(ex.InnerException.ToString(), ex);   
                return null;
            }
        } // end HttpPost


What got me stuck in the very beginning was that I lacked the UserAgent. This lead to TimeOuts without any (for me obvious) reason. After that I was having some issues with the combination of Url/HtmlEncode and the ContentType...

Any feedback appreciated.


Cheers
txcoin

PS:
As this may become out of date pretty much any moment, also look out for comments by "SlipperySlope" on how to do it in Java. The only change you have to make is that whenever we writes that he is using HtmlEncoding, in C# the UrlEncoding is needed.

PPS: Now I know why I need UrlEncode instead of HtmlEncode: it is the ContentType header :D

PPPS: Yes, this is some kind of x-post to the thread http://forum.bitcoin.org/index.php?topic=25644


Title: Re: mtGox Trading API down?
Post by: itake on July 09, 2011, 06:23:55 PM
I get:

{"error":"Bad token"}

Regards,

oooo I think I figured out what the problem is.  You have to submit a token variable.  It is given in the html when you log in.

Code:
<script type="text/javascript" language="javascript">
var token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

</script>


so to get the order data, you do this:

1) login
2) in the trade HTML, it will give you a token value (see above).
3) HTTP POST https://mtgox.com/code/getOrders.php
   Request Headers:
      Host[mtgox.com]
      User-Agent[Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0]
      Accept[application/json, text/javascript, */*; q=0.01]
      Accept-Language[en-gb,en;q=0.5]
      Accept-Encoding[gzip, deflate]
      Accept-Charset[ISO-8859-1,utf-8;q=0.7,*;q=0.7]
      Connection[keep-alive]
      Content-Type[application/x-www-form-urlencoded; charset=UTF-8]
      X-Requested-With[XMLHttpRequest]
      Referer[https://mtgox.com/]
      Content-Length[38]
      Cookie[MAKE SURE YOU POST WITH LOGIN COOKIE]
      Pragma[no-cache]
      Cache-Control[no-cache]
   Post Data:
      token[PUT TOKEN HERE]
   Response Headers:
      Date[Sat, 09 Jul 2011 18:18:13 GMT]
      Server[Apache]
      X-Powered-By[PHP/5.3.6]
      Expires[Thu, 19 Nov 1981 08:52:00 GMT]
      Cache-Control[no-store, no-cache, must-revalidate, post-check=0, pre-check=0]
      Pragma[no-cache]
      Strict-Transport-Security[max-age=1209600]
      Content-Encoding[gzip]
      Vary[Accept-Encoding]
      Content-Length[220]
      Keep-Alive[timeout=15, max=95]
      Connection[Keep-Alive]
      Content-Type[application/json]



Title: Re: mtGox Trading API down? [solved]
Post by: txcoin on July 10, 2011, 01:57:47 PM
No need to get token or cookie:


        public static string GetOpenOrders(string userName, string password)
        {
            // parameters: name1=value1&name2=value2
            var parameters = "name=" + HttpUtility.UrlEncode(userName)
                 + "&pass=" + HttpUtility.UrlEncode(password);

            var req = (HttpWebRequest)WebRequest.Create("https://mtgox.com/code/getOrders.php");

            req.ContentType = "application/x-www-form-urlencoded";
            req.Method = "POST";
            req.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
            //req.Accept = "application/json";

            byte[] bytes = Encoding.ASCII.GetBytes(parameters);
            req.ContentLength = bytes.Length;

            Stream os = req.GetRequestStream();
            os.Write(bytes, 0, bytes.Length);
            os.Close();

            WebResponse resp = req.GetResponse();

            try
            {
                var sr = new StreamReader(resp.GetResponseStream());
                return sr.ReadToEnd().Trim();
            }
            catch (Exception ex)
            {
                Logger.Fatal(ex.Message);
                Logger.FatalException(ex.InnerException.ToString(), ex);   
                return null;
            }
        } // end HttpPost


(Convert to your most favorite programming language ;)

Cheers
txcoin