-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Error using ImportJSONBasicAuth to get data from my Bank API into Google Sheet #215
Comments
I tried editing the ImportJSONBasicAuth function to include post as the method but it didn't work. i.e.
|
Hi, you are getting '400's since your bank uses OAuth2, which in your case means the necessity for two requests - one to obtain a Bearer Token and a second one to retrieve data you want using said token, which has an expiration time. This also means you will have to repeat the procedure pretty much every time you want to retrieve data from the API. The ImportJSONBasicAuth is meant for one-step Basic Authorization (simply logging in with username & password), so you'll need to play around a little with UrlFetchApp directly instead, but you can still utilize parseJSONObject_ to insert the retrieved data into your spreadsheet. To be honest Investec's API docs are not too user friendly, but you should be able to retrieve your token using some of the built-in parameters and then follow the similar pattern to retrieve your data based on provided examples. So as an example: const YOUR_API_KEY_STRING = "e2NsaWVudElkfTp7c2VjcmV0fQ==";
/* const payload = 'grant_type=client_credentials&scope=accounts';
// an optional requested access scope as a string- depends on you authorization level */
const options = {method: 'post', headers: {Authorization: `Basic ${YOUR_API_KEY_STRING}`}} /*, payload}*/
const res = UrlFetchApp.fetch('https://openapi.investec.com/identity/v2/oauth2/token', options);
/* note the lack of Content-Type header in this case - UrlFetchApp uses the required one by default.
Same thing with that Accept: application/json header - no need for it. */
console.log(res.getContentText()); /* you should be able to see your access_token in the response here */ Now you're set to parse it to JSON, get that key and use it for the second API call, only now the authorization in the header uses that Bearer token per examples in the docs. Just mind the expiration date ;) |
Wow, thank you so much @christales - you provided me with the context I needed to understand what is going on. I thought I could use ImportJSONBasicAuth to get the bearer token and then take it from there. I will play around with your suggested code. How can I thank you more!? |
Using @christales answer and rewriting the
|
I added code to `ImportJSONViaPost` to handle username and password parameters for Basic Auth requests and I added a function `ImportJSONBearerAuth` to handle bearer auth requests. I considered creating a new function `ImportJSONBasicAuthViaPost` which would pass the basic auth params to `ImportJSONViaPost` but I couldn't get it right so gave up and just put the code inside `ImportJSONViaPost`. Thanks to @christales (https://github.com/christales) for help here: bradjasper#215 Also to help from [@idfurw](https://stackoverflow.com/users/16125139/idfurw) on Stackoverflow [here](https://stackoverflow.com/questions/68818380/error-using-importjsonbasicauth-to-get-data-from-my-bank-api-into-google-sheet/68845929#68845929)
I am trying to get my bank balances in Google Sheets and decided to use this script https://github.com/bradjasper/ImportJSON to pull data from my Investec bank account via the API here: https://developer.investec.com/programmable-banking/#authorization
I am calling the function
ImportJSONBasicAuth("https://openapi.investec.com/identity/v2/oauth2/token","{username}","{secret}","","")
with my username and secret, in my Google Sheet... But I get an error as followsAny ideas what I might be doing wrong?
EDIT
Digging a little deeper into the error...
This is the function I'm calling
Which calls this function in turn - which has the line producing the error i.e. line 220 commented below
The text was updated successfully, but these errors were encountered: