Krivo
July 10, 2020, 9:21pm
1
Anyone tried to importJSON from an api where username and password is required?
@Vicio_Sciascia @ThinhDinh
The importJSON from this source:
/*====================================================================================================================================*
ImportJSON by Brad Jasper and Trevor Lohrbeer
====================================================================================================================================
Version: 1.5.0
Project Page: https://github.com/bradjasper/ImportJSON
Copyright: (c) 2017-2019 by Brad Jasper
(c) 2012-2017 by Trevor Lohrbeer
License: GNU General Public License, version 3 (GPL-3.0)
http://www.opensource.org/licenses/gpl-3.0.html
------------------------------------------------------------------------------------------------------------------------------------
A library for importing JSON feeds into Google spreadsheets. Functions include:
ImportJSON For use by end users to import a JSON feed from a URL
ImportJSONFromSheet For use by end users to import JSON from one of the Sheets
ImportJSONViaPost For use by end users to import a JSON feed from a URL using POST parameters
ImportJSONAdvanced For use by script developers to easily extend the functionality of this library
ImportJSONBasicAuth For use by end users to import a JSON feed from a URL with HTTP Basic Auth (added by Karsten Lettow)
For future enhancements see https://github.com/bradjasper/ImportJSON/issues?q=is%3Aissue+is%3Aopen+label%3Aenhancement
This file has been truncated. show original
As example - I’m trying to import data from Cloudinary
https://<API_KEY>:<API_SECRET>@api.cloudinary.com/v1_1/ <cloud_name>/resources/search
If you succeed - how do you do it?
Can you try if the method by thadk here works?
1 Like
Hi @Krivo , I don’t know if I understood correctly what you are trying to do man, but to me it’s seems easier than that: according to Cloudinary documentation you have to pass your API Key and API Secret (used for the authentication) as part of the API’s URL.
So what you have to do is: go to your Cloudinary dashboard and copy your API Key, API Secret and Cloud name:
And paste them as part of API URL. Here’s an example in Postman:
Basically your importJSON formula would be something like that:
=ImportJSON(“https://{{api_key}}:{{api_secret}}@api.cloudinary.com/v1_1/{{cloud_name} }/resources/image”;“”;“”)
I think that the piece of script in the link posted by @ThinhDinh is useful when you have to deal with APIs with basic auth authorization method , but I think it’s not our case with Cloudinary
1 Like
Forgotten to say: glad to read that you’re practicing with APIs!
Kudos and keep going
1 Like
Krivo
July 11, 2020, 7:40pm
5
@Vicio_Sciascia @ThinhDinh
It is not possible to use a simple importJSON function as it will not allow login information which can be seen by the exception
Exception: Login information disallowed:
But the link that Thinh supplied does work.
Add this to your importJSON script
function ImportJSONBasicAuthentication(url, query, parseOptions, username, password) {
var fetchOptions = {
“headers” : {
“Authorization” : 'Basic ’ + Utilities.base64Encode(username + ‘:’ + password)
},
muteHttpExceptions: true
};
return ImportJSONAdvanced(url, fetchOptions, query, parseOptions, includeXPath_, defaultTransform_);
}
and supply the username and password from Cloudinary things then work perfectly
username = API Key
password = API Secret
=ImportJSONBasicAuthentication(“https://api.cloudinary.com/v1_1/\ /resources/search?max_results=100&expression=folder=<foldername>%20AND%20filename=2*”,“noHeaders”,<API Key>,<API Secret>)
2 Likes
Good to hear, Krivo. I will surely use this at some point in the future. Thank you
You’re right @Krivo , perhaps it is some some sort of a bug? I don’t know
Anyway, good to know and thank you for sharing