fimp



Offline



Activity: 304

Merit: 250









Sr. MemberActivity: 304Merit: 250

Re: [3BTC bounty] Authenticate with MtGox API from Google Apps script December 22, 2012, 08:13:08 PM #5 Quote from: error on December 22, 2012, 06:56:48 PM



It uses the Google Apps Script



It takes two parameters, path and parameters. path is the API call (e.g. "0/sellBTC.php" or "1/private/BTCUSD/order/add"). parameters is an associative array containing the key/value pairs of the parameters to be passed to the API call.



Regrettably, this is only minimally tested since I have no need to create any orders right now, and MtGox doesn't seem to provide a sandbox. Nevertheless it should be about 99% correct, with only one or two bugs, which I presume you will report so I can fix them. Once that's done, you can send the BTC to the address in my signature.



Example (to sell 1 BTC at $13.50):



Code: parameters = {

"type": "ask",

"amount_int": 100000000,

"price_int": 1350000

};

result = jsonParse(mtgoxApiCall("1/BTCUSD/order/add", parameters));

View or download this script:



Code: function http_build_query (formdata, numeric_prefix, arg_separator) {

// http://kevin.vanzonneveld.net

// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)

// + improved by: Legaev Andrey

// + improved by: Michael White (http://getsprink.com)

// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)

// + improved by: Brett Zamir (http://brett-zamir.me)

// + revised by: stag019

// + input by: Dreamer

// + bugfixed by: Brett Zamir (http://brett-zamir.me)

// + bugfixed by: MIO_KODUKI (http://mio-koduki.blogspot.com/)

// % note 1: If the value is null, key and value is skipped in http_build_query of PHP. But, phpjs is not.

// - depends on: urlencode

// * example 1: http_build_query({foo: 'bar', php: 'hypertext processor', baz: 'boom', cow: 'milk'}, '', '&');

// * returns 1: 'foo=bar&php=hypertext+processor&baz=boom&cow=milk'

// * example 2: http_build_query({'php': 'hypertext processor', 0: 'foo', 1: 'bar', 2: 'baz', 3: 'boom', 'cow': 'milk'}, 'myvar_');

// * returns 2: 'php=hypertext+processor&myvar_0=foo&myvar_1=bar&myvar_2=baz&myvar_3=boom&cow=milk'

var value, key, tmp = [],

that = this;



var _http_build_query_helper = function (key, val, arg_separator) {

var k, tmp = [];

if (val === true) {

val = "1";

} else if (val === false) {

val = "0";

}

if (val != null) {

if(typeof(val) === "object") {

for (k in val) {

if (val[k] != null) {

tmp.push(_http_build_query_helper(key + "[" + k + "]", val[k], arg_separator));

}

}

return tmp.join(arg_separator);

} else if (typeof(val) !== "function") {

return encodeURIComponent(key) + "=" + encodeURIComponent(val);

} else {

throw new Error('There was an error processing for http_build_query().');

}

} else {

return '';

}

};



if (!arg_separator) {

arg_separator = "&";

}

for (key in formdata) {

value = formdata[key];

if (numeric_prefix && !isNaN(key)) {

key = String(numeric_prefix) + key;

}

var query=_http_build_query_helper(key, value, arg_separator);

if(query != '') {

tmp.push(query);

}

}



return tmp.join(arg_separator);

}



// End of code from phpjs



// ******* THE FUN STARTS HERE



function mtgoxApiCall(path, parameters) {

var apiKey = "YOUR_KEY";

var apiSecret = "YOUR_SECRET";



parameters = (typeof parameters === "undefined") ? [] : parameters;

parameters["nonce"] = Date.now();



var payload = http_build_query(parameters);



signature = Utilities.base64Encode(Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, payload, apiSecret));



var headers = {

"User-Agent": "YOUR_USER_AGENT",

"Rest-Key": apiKey,

"Rest-Sign": signature

}



var options = {

"method": "post",

"headers": headers,

"payload": payload

};



return UrlFetchApp.fetch("https://mtgox.com/api/" + path, options);

} This solution relies on a very small part of the phpjs code, which is dual-licensed under GPL and MIT. Pick whichever you want. As a result, you may also consider this dual-licensed under GPL and MIT.It uses the Google Apps Script UrlFetchApp class , and should handle both v0 and v1 API calls. Customize it by adding in your API key, secret and preferred user-agent string in the marked places.It takes two parameters, path and parameters. path is the API call (e.g. "0/sellBTC.php" or "1/private/BTCUSD/order/add"). parameters is an associative array containing the key/value pairs of the parameters to be passed to the API call.Regrettably, this is only minimally tested since I have no need to create any orders right now, and MtGox doesn't seem to provide a sandbox. Nevertheless it should be about 99% correct, with only one or two bugs, which I presume you will report so I can fix them.Once that's done, you can send the BTC to the address in my signature.Example (to sell 1 BTC at $13.50):View or download this script: https://script.google.com/d/10ZDAQq87gbAItZhrYV_B0ldH_l5kLU3BlwsG6xch_0PWMOiadO_gYUz0/edit



Running the mtgoxBuy() function I get the following error: "Request failed for



What am I doing wrong? Thanks. So I made this from your code: https://script.google.com/d/1MRRWhU4LDjS1o65J_o_M3Q3yxgGidKFCxjnXk06TiftJ_mYekbDSYgQ5/edit (I have removed API key and secret again)Running the mtgoxBuy() function I get the following error: "Request failed for https://mtgox.com/api/1/BTCUSD/order/add returned code 403. Server response: {"result":"error","error":"Identification required to access private API","token":"login_required"} (line 90)"What am I doing wrong?