News & Announcements User Community Developer Community

Welcome to the RingCentral Community

Please note the community is currently under maintenance and is read-only.

Search
Make sure to review our Terms of Use and Community Guidelines.
  Please note the community is currently under maintenance and is read-only.
Home » Developers
Pull Call Data, not authorisation.
Tags: authentication
Aug 12, 2019 at 2:35am   •   2 replies  •  0 likes
Arron Voce

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 + ", \nTo:   " + opt_dateTo + "; \n\nCalc From: " + dateFrom + ", \nCalc 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 Answers
answered on Aug 13, 2019 at 7:00am  

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


 0
answered on Aug 12, 2019 at 8:33am  

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.


 0



A new Community is coming to RingCentral!

Posts are currently read-only as we transition into our new platform.

We thank you for your patience
during this downtime.

Try Workflow Builder

Did you know you can easily automate tasks like responding to SMS, team messages, and more? Plus it's included with RingCentral Video and RingEX plans!

Try RingCentral Workflow Builder

PRODUCTS
RingEX
Message
Video
Phone
OPEN ECOSYSTEM
Developer Platform
APIs
Integrated Apps
App Gallery
Developer support
Games and rewards

RESOURCES
Resource center
Blog
Product Releases
Accessibility
QUICK LINKS
App Download
RingCentral App login
Admin Portal Login
Contact Sales
© 1999-2024 RingCentral, Inc. All rights reserved. Legal Privacy Notice Site Map Contact Us