question

Arron Voce avatar image
Arron Voce asked ·

Pull Call Data, not authorisation.

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);
}

authorizationtokengoogle script
1 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Phong Vu avatar image
Phong Vu answered ·

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.

2 comments Share
1 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Hi, yes I've check and the Client ID and Secret is correct however I'm still getting the above authorisation failed message

0 Likes 0 · ·

I added you to the expedited support Glip group. Please check your email and accept the invite to join the group. Once you are in the group, post your question there (just refer to this thread) then I can help you better.

I had a look at your app analytics and I am surprise that there were not much test call on the sandbox environment and the app also failed in sandbox environment. Not sure how you could graduate the app to the production.

Anyway, please join the group and I can walk you through steps to solve the problem.

0 Likes 0 · ·
Arron Voce avatar image
Arron Voce answered ·

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

1 comment Share
1 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

I see your app is active in production. With the error above, did you try to login the production or the sandbox environment? If it was the production, can you double check if the client Id and client secret are now set with the production ones:

client id: e7L2fXnbRhaXXXXXXXX-XX

client secret: P1NXlcW1S9K2clXenERJDwdrgUdXXXXXXXXX-XX-XXX

0 Likes 0 · ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.