Вопрос

I am trying to incorporate the BTC-e.com API in to a google docs spreadsheet.

The API documentation is here: https://btc-e.com/api/documentation

The method name is sent via POST parameter method.

As the URLFetchApp requires me to set the type of request as POST by a parameter method and I then have another parameter called method to be set as getInfo.

How can I go about setting the fetch method as POST and have the API parameter method as getInfo.

Below is the function this relates too. Also I am sure there a more issues in my work I am yet to find.

function inventory() {
  var nonce=Number(SpreadsheetApp.getActiveSheet().getRange('K2').getValue());
  var token=SpreadsheetApp.getActiveSheet().getRange('K1').getValue();
  var tokenEndpoint = "https://btc-e.com/tapi";
  var sign= 'TEMP'

  var head = {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Key': token,
    'Sign': sign

    }

  var params = {

    method : "POST",
    method : "getInfo",
    headers:  head,
    contentType: 'application/x-www-form-urlencoded',
    method : "getInfo",
    nonce: nonce

    }


  var request = UrlFetchApp.getRequest(tokenEndpoint, params); 
  var response = UrlFetchApp.fetch(tokenEndpoint, params); 
  var response2=String(response);
  SpreadsheetApp.getActiveSheet().getRange('K2').setValue(nonce+1);
  SpreadsheetApp.getActiveSheet().getRange('I16').setValue(response2);
  SpreadsheetApp.getActiveSheet().getRange('I17').setValue(nonce);

}

This just yields the error

Attribute provided with invalid value: method

Thanks,

Steve

PS: First time posting, I tried to get the format correct.

Это было полезно?

Решение

I made the following Google JavaScript function to do POST access to BTC-e. You can find this function in action in the example spreadsheet I made to demonstrate the BTC-e API functions.

function btceHttpPost(keyPair, method, params, nonce) {

  if (keyPair === undefined) {
    return "{'error':'missing key pair'}"
  }

  if (params === undefined) {
    params = '';
  }

  // Cleanup keypair, remove all \s (any whitespace)
  var keyPair = keyPair.replace(/[\s]/g, '');

  // Keypair example: "AFE730YV-S9A4FXBJ-NQ12HXS9-CA3S3MPM-CKQLU0PG,96a00f086824ddfddd9085a5c32b8a7b225657ae2fe9c4483b4c109fab6bf1a7"
  keyPair = keyPair.split(',');
  var pubKey = keyPair[0];
  var privKey = keyPair[1];

  // As specified on the BTC-e api (https://btc-e.com/api/documentation) the 
  // nonce POST parameter must be an incrementing integer (>0). The easiest 
  // implementation is the use of a timestamp (TS), so there is no need 
  // for persistant storage. Preferable, the resolution of the TS should be 
  // small enough the handle the desired call-frequency (a sleep of the TS 
  // resolution can fix this but I don't like such a waste). Another 
  // consideration is the sizeof the nonce supported by BTC-e. Experiments 
  // revealed this is a 32 bit unsigned number. The native JavaScript TS, 
  // stored in a float, can be 53 bits and has a resolution of 1 ms. 

  if (nonce === undefined)  
    // This time stamp counts amount of 200ms ticks starting from Jan 1st, 2014 UTC
    // On 22 Mar 2041 01:17:39 UTC, it will overflow the 32 bits and will fail 
    // the nonce key for BTC-e
    var nonce = Math.floor((Date.now() - Date.UTC(2014,0)) / 200);

  // Construct payload message
  var msg = 'nonce=' + nonce + '&method=' + method + params;

  var msgSign = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, msg, privKey);

  // Convert encoded message from byte[] to hex string 
  for (var msgSignHex = [], i = 0; i < msgSign.length; i++) {
    // Doing it nibble by nibble makes sure we keep leading zero's
    msgSignHex.push(((msgSign[i] >>> 4) & 0xF).toString(16));
    msgSignHex.push((msgSign[i] & 0xF).toString(16));
  }
  msgSignHex = msgSignHex.join('');

  var httpHeaders = {'Key': pubKey, 'Sign': msgSignHex};
  var fetchOptions = {'method': 'post', 'headers': httpHeaders, 'payload': msg};

  var reponse = UrlFetchApp.fetch('https://btc-e.com/tapi', fetchOptions);
  return reponse.getContentText();
};

Другие советы

The problem looks to be with your params object . You have method set thrice in the same object, which is a source of confusion. Next, take a look at the documentation for UrlFetchApp.fetch() ( https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetch(String,Object) ) . The method can take a value of post, get, delete, put.

The getInfo should probably be appended to your URL to make it

var tokenEndpoint = "https://btc-e.com/tapi/getInfo"

Per the docs, you also have to put in more parameters to the request, nonce, api key etc. Use this as a starting point, revisit the documentation and get back to SO if you still have trouble

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top