7

What I would like to be able to do is for user to click button on my website (actually a webapp that runs locally on user machine so each instance is single user) which initiates a Google image search with various params set and display the results in a section of my html page.

This is because the idea is they can then select an image and drag and drop it onto a dropzone on my webpage. This parts works but currently the search is opening in a new tab so its a bit of a pain dragging from that tab to my tab.

Embedding as an iframe does not work, Google does not allow that.

So

a> is there a Google sanctioned API I can use to perform a Google search for images and display within my page.

b> Could I send send a url request from my server (i,e like curl/wget and then screenscrape the results and present on webpage

Paul Taylor
  • 13,411
  • 42
  • 184
  • 351

2 Answers2

7

Google's Custom Search Engine (CSE) API is limited to 100 free requests per day.

Creating cx and modifying it to search for images

  1. Create custom search engine at https://cse.google.com/cse/create/new based on your search criteria.
  2. Choose sites to search (leave this blank if you want to search the entire web, otherwise you can enter a site to search in one particular site)
  3. Enter a name and a language for your search engine.
  4. Click "create." You can now find cx in your browser URL.
  5. Under "Modify your search engine," click the "Control Panel" button. In the "edit" section you will find an "Image Search" label with an ON/OFF button, change it to ON. Click "update" to save your changes.

Conducting a search with the API

The API endpoint url is https://www.googleapis.com/customsearch/v1

The following JSON parameters are used for this API:

  • q: specifies search text
  • num: specifies number of results. Requires an integer value between 1 and 10 (inclusive)
  • start: the "offset" for the results, which result the search should start at. Requires an integer value between 1 and 101.
  • imgSize: the size of the image. I used "medium"
  • searchType: must be set to "image"
  • filetype: specifies the file type for the image. I used `"jpg", but you can leave this out if file extension doesn't matter to you.
  • key: an API key, obtained from https://console.developers.google.com/
  • cx: the custom search engine ID from the previous section

Simply make a GET request by passing above parameters as JSON to the API endpoint (also listed above).

Note: If you set a list of referrers in the search engine settings, visiting the URL via your browser will likely not work. You will need to make an AJAX call (or the equivalent from another language) from a server specified in this list. It will work for only the referrers which were specified in the configuration settings.

Reference: https://developers.google.com/custom-search/json-api/v1/reference/cse/list

Answer from: https://stackoverflow.com/a/34062436/4366303

Pradeep Kumar
  • 4,065
  • 2
  • 33
  • 40
  • So it wouldn't be of any use if I had to limit to 100 calls a day over all my customers, to limit to 100 each customer maybe okay but that would mean they would have to have a google account and create a search themselves, would that work ? – Paul Taylor Apr 04 '18 at 19:22
  • 100 *free* requests. You can make as many requests as you want if you are willing to pay for them. https://developers.google.com/custom-search/json-api/v1/overview#Pricing – ThatOneDude Apr 04 '18 at 23:13
  • As of now additional requests cost $5 per 1000 queries, up to 10k queries per day. Above link explains it. – Pradeep Kumar Apr 05 '18 at 05:53
  • Yes, but I have no control over how many requests my users would make, and for this low cost software would be far too expensive. – Paul Taylor Apr 05 '18 at 07:47
  • Just track the count in backend and have a warning with remaining count when it exceeds 90. Give a documentation/label with 100 API call limit per day. – Pradeep Kumar Apr 05 '18 at 08:18
  • But its 100 per application, i have 5000 users so they cannot even make a call, or is it 100 per location so if my webapp is deployed on different machines each has 100 ? – Paul Taylor Apr 05 '18 at 08:56
  • if 5000 users need google result in the same application, then it should not be a low cost one. Better have role based access so that only few can access the page and you need to go for billing API. Or have proper instructions for them to create API key and enter in your application. Then use it. – Pradeep Kumar Apr 05 '18 at 12:14
  • 1
    They each run the app locally so there is not a single website they go to, most wont actually use the search but some will. Okay Ive marked your question correct since it is a good explanation of how to use Custom Search – Paul Taylor Apr 06 '18 at 08:36
  • Make the API key configurable, and have the clients that want search manually logon to the developers portal, create a new project, and generate their own API key. Then each client will have 100 free requests per day, and should they exceed it, theyd be responsible for their own billing. – ThatOneDude May 01 '18 at 23:47
1

Google has retired the image search API, it is no longer available (see this SO thread with alternatives).

If you open up the URL you provided it will say this:

{"responseData": null, "responseDetails": "This API is no longer available.", "responseStatus": 403}
Marcos Pérez Gude
  • 21,869
  • 4
  • 38
  • 69
DeepakB
  • 121
  • 1
  • 12