Question

Pull Call Data, not authorisation.

  • 12 August 2019
  • 2 replies
  • 1741 views

We have a Google script that pulls the call data from out account to analyse call volumes and imports this into our analytics platform, however recently I've been getting the following error.


Request failed for https://platform.ringcentral.com returned code 400. Truncated server response: { "error" : "invalid_grant", "errors" : [ { "errorCode" : "OAU-140", "message" : "Invalid resource owner credentials" } ], "error_d... (use muteHttpExceptions option to examine full response). (line 65, file "ringCentralCallsFetch")


No passwords have changed so I have no idea why this is happening


function FetchRingCentralCalls(opt_dateFrom, opt_dateTo, opt_SendEmailToZoho) {
  
  
  // Live environment details
  var livePhone = "123456678";
  //var livePhone = "myemail";
  var livePwd = "password"
  
  var liveClientId = "x";
  var liveSecret = "x";
  
  var dateFrom = "";
  var dateTo = "";

  var rcLiveTokenURL = "https://platform.ringcentral.com/restapi/oauth/token";
  var rcLiveCallsURL = "https://platform.ringcentral.com/restapi/v1.0/account/~/call-log?view=Detailed&perPage=1000";
  
  if (opt_dateFrom !== undefined && opt_dateFrom !== "") {
    dateFrom = Utilities.formatDate(new Date(opt_dateFrom), "GMT", "yyyy-MM-dd'T'HH:mm:ss'Z'");
    rcLiveCallsURL = rcLiveCallsURL + "&dateFrom=" + dateFrom;
  }
  
  if (opt_dateTo !== undefined && opt_dateTo !== "") {
    dateTo = Utilities.formatDate(new Date(opt_dateTo), "GMT", "yyyy-MM-dd'T'HH:mm:ss'Z'");
    rcLiveCallsURL = rcLiveCallsURL + "&dateTo=" + dateTo;
  }
  
  // Zoho reports email
  var toEmail = "emalhete";
  
  
  // Setup parameters for Token retrieval
  var payload = "grant_type=password&username=" + livePhone + "&password=" + livePwd;
  
  var options = {
     "method" : "post",
     "payload" : payload
  };
  
  options.headers = {
    "Authorization" : "Basic " + Utilities.base64Encode(liveClientId + ":" + liveSecret), 
    "Accept" : "application/json",
    "Content-Type" : "application/x-www-form-urlencoded"
  };
  

  Logger.log("");
  Logger.log("*** OPTIONS ***");
  Logger.log("");
  Logger.log(options);
  
  // Fetch new token
  var rcTokenResponse = UrlFetchApp.fetch(rcLiveTokenURL, options);
  Logger.log("");
  Logger.log("*** TOKEN RESPONSE ***");
  Logger.log("");
  Logger.log(rcTokenResponse);
  
  // Extract token from response
  var data = rcTokenResponse.getContentText();
  var token = JSON.parse(data)["access_token"];
  
  Logger.log("");
  Logger.log("*** TOKEN ***");
  Logger.log("");
  Logger.log(token);
  
  
  // Setup paramers to retrieve call log records
  var callsOptions = {
    "method" : "get",
  }
  
  callsOptions.headers = {
    "Authorization" : "Bearer " + token, 
  };

  Logger.log("");
  Logger.log("*** CALLS OPTIONS ***");
  Logger.log("");
  Logger.log(callsOptions);
  
  var rcCallsResponse = UrlFetchApp.fetch(rcLiveCallsURL, callsOptions);
  
  var recordsReturned = JSON.parse(rcCallsResponse)["paging"]["pageEnd"];
  
  Logger.log("");
  Logger.log("*** RECORDS ***");
  Logger.log("");
  Logger.log(recordsReturned);

  
  // Make a file/blob for attaching to Zoho email 
  var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyMMdd_HHmm");
  var fileName = "calls-" + formattedDate;
      
  if (dateFrom !== "") (
    fileName = fileName + "_F_" + dateFrom
  )
  
  if (dateTo !== "") (
    fileName = fileName + "_T_" + dateTo
  )
  
  fileName = fileName + ".txt";
  var jsonBlob = rcCallsResponse.getBlob().setName(fileName);
  
  Logger.log("");
  Logger.log("*** BLOB ***");
  Logger.log("");
  Logger.log(jsonBlob.getDataAsString());
  
  
  // Send to Zoho if there are any calls returned
  if (recordsReturned > 0)
  {
    sendToZohoImportAPI(jsonBlob, 'RC+Calls+Detailed+Log', '&ZOHO_MATCHING_COLUMNS=records.id,records.legs.startTime');
  }
  
  
  /*
  if (recordsReturned && (opt_SendEmailToZoho == undefined || opt_SendEmailToZoho))
  {
    // Send email to Zoho with calls data attachment
    MailApp.sendEmail({
      to: toEmail, 
      subject: "RingCentral calls log", 
      body: "From: " + opt_dateFrom + ", 
To:   " + opt_dateTo + "; 

Calc From: " + dateFrom + ", 
Calc To:   " + dateTo,
      attachments: jsonBlob
    });
  
  
    // Log how many emails we have left in our daily quota
    var emailQuotaRemaining = MailApp.getRemainingDailyQuota();
    Logger.log("Remaining email quota: " + emailQuotaRemaining);
  }
  */
  
  return recordsReturned;
}

  
function sendToZohoImportAPI (jsonBlob, tableName, matchingColumns) {
  
  var zohoCallsURL = 'https://analyticsapi.zoho.eu/api/emailaddresshere@test.com/+All+Data/' + tableName + '?ZOHO_ACTION=IMPORT&ZOHO_OUTPUT_FORMAT=JSON&ZOHO_ERROR_FORMAT=JSON&ZOHO_API_VERSION=1.0&authtoken=&ZOHO_IMPORT_TYPE=UPDATEADD&ZOHO_AUTO_IDENTIFY=TRUE&ZOHO_ON_IMPORT_ERROR=ABORT&ZOHO_CREATE_TABLE=false&ZOHO_IMPORT_FILETYPE=JSON' + matchingColumns;
  
  var formData = {
    'ZOHO_FILE' : jsonBlob
  }
  
  var options = {
    'method' : 'post',
    'payload' : formData
  }
  
  var zohoResponse = UrlFetchApp.fetch(zohoCallsURL, options);
  
  Logger.log("");
  Logger.log("*** Zoho API Response ***");
  Logger.log("");
  Logger.log(zohoResponse);
}


2 replies

Userlevel 1

Hi, while not knowing what was changed (maybe accidentally), I still trust the error message and it is about your login credentials.

 // Live environment details
var livePhone = "123456678";
//var livePhone = "myemail";
var livePwd = "password"  
var liveClientId = "x";
var liveSecret = "x";

Is this your production account login credentials? Make sure the liveClienId and liveSecret are valid for the production. Make sure you can login your account at (https://service.ringcentral.com) with the same username and password.

If everything is correct and you still cannot login, let me know your main company number and the app name.

Thanks


I can login fine to service.ringcentral however I now get this message

Request failed for https://platform.ringcentral.com returned code 400. Truncated server response: { "error" : "invalid_grant", "errors" : [ { "errorCode" : "OAU-140", "message" : "Invalid resource owner credentials" } ], "error_d... (use muteHttpExceptions option to examine full response). (line 64, file "ringCentralCallsFetch")


App is called Zoho Password Flow company number is 020 3900 4154

Reply