Search code examples
apihyperlinkscriptingplaid

Google Scripts App and Plaid Link Authentication


I am attempting to create some code to download my bank transactions to google sheets. I am trying to do that by using google scripts and making API calls to Plaid. I currently have a free sand box account set up but after trying several things over the last few days have not been able to successfully make the api call. I have also downloaded Postman and have made successful calls from there.

Here is the main body of my code which I expect to return a link token. (Plaid Docs) This is what I believe to be the first step in making this all happen. (Plaid Docs)

// 1 - Create a Link Token

  var data = {
   "client_id":"redacted",
   "secret":"redacted",
   "client_name":"Google Sheets Money",
   "country_codes":[
      "CA"
   ],
   "language":"en",
   "user":{
      "client_user_id":"unique_user_id"
   },
   "products":[
      "auth",
      "transactions"
   ]
 };
  
 var json = JSON.stringify(data);
  
 var options = {
     "method": "POST",
     "contentType": "application/json",
      "body": json,
      "muteHttpExceptions": true,
  };
  

  var response = UrlFetchApp.fetch("https://sandbox.plaid.com/link/token/create", options);

  Logger.log(response);

Here is what my logger says:

{
  "display_message": null,
  "documentation_url": "https://plaid.com/docs/?ref=error#invalid-request-errors",
  "error_code": "INVALID_BODY",
  "error_message": "body could not be parsed as JSON",
  "error_type": "INVALID_REQUEST",
  "request_id": "0A86SP6KUD02oES",
  "suggested_action": null
}

Some googling has had me move my contentType from the headers section. I've run my JSON through an online format checker, and I've played with sending the json with and with out the stringify option.

Can anyone see what I might be missing? I believe it's possible as I have seen at least one add on solution that appears to integrate Plaid with Google Sheets. BudgetSheets I am interested in building a solution myself.

Thanks


Solution

  • Are you sure you're sending the JSON at all? The documentation for UrlFetchApp.fetch doesn't mention a body options parameter, it uses a payload parameter instead. Also, I'm not sure whether it is case sensitive or not, but just in case, it's worth nothing that its documentation uses lowercase http methods, not uppercase ones.