Connecting from google sheets

Created at 05 Aug 2024, 02:56
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
AU

aussiemckenna

Joined 03.08.2018

Connecting from google sheets
05 Aug 2024, 02:56


Please forgive whats likely a stupid question BUT i am attempting to complete a fairly simple (I thought!) API request to grab the latest EUR/USD price using a google sheets macro.  I have completed the API access request so have my Client ID and Secret so then have attempted (using AI generated code) to complete the request using the below code - but i just get error messages stating method not allowed.  Please can anyone provide some guidance - or ideally code that would work in this scenario?  Thanks in advance - total newbie

 

function getEURUSDPrice() {

 var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

 var clientId = 'CLIENT ID ENTERED HERE';

 var clientSecret = 'CLIENT SECRET ENTERED HERE';

 var tokenUrl = 'https://id.ctrader.com/connect/token';

 var apiUrl = 'https://api.spotware.com/connect/tradingaccounts/{7 DIGIT ACCOUNT NUMBER ENTERED HERE}/positions';

 

 // Get access token

 var tokenOptions = {

   'method': 'post',

   'payload': {

     'grant_type': 'client_credentials',

     'client_id': clientId,

     'client_secret': clientSecret

   },

   'muteHttpExceptions': true

 };

 

 var tokenResponse = UrlFetchApp.fetch(tokenUrl, tokenOptions);

 if (tokenResponse.getResponseCode() !== 200) {

   Logger.log('Error fetching token: ' + tokenResponse.getContentText());

   return;

 }

 

 var tokenData = JSON.parse(tokenResponse.getContentText());

 var accessToken = tokenData.access_token;

 

 // Fetch EUR/USD price

 var apiOptions = {

   'method': 'get',

   'headers': {

     'Authorization': 'Bearer ' + accessToken

   },

   'muteHttpExceptions': true

 };

 

 var apiResponse = UrlFetchApp.fetch(apiUrl, apiOptions);

 if (apiResponse.getResponseCode() !== 200) {

   Logger.log('Error fetching price: ' + apiResponse.getContentText());

   return;

 }

 

 var apiData = JSON.parse(apiResponse.getContentText());

 

 // Assuming the API returns an object with the price in a field called 'price'

 var eurUsdPrice = apiData.price;

 

 // Set the price in the first cell of the first row

 sheet.getRange('A1').setValue(eurUsdPrice);

}


@aussiemckenna