Recent

Author Topic: Using Google APIs to launch search from an application  (Read 1665 times)

simsee

  • Full Member
  • ***
  • Posts: 184
Using Google APIs to launch search from an application
« on: September 13, 2021, 11:32:12 pm »
Using the Google Apis package, is it possible to launch search queries and collect responses from an application? If so, as I think, can anyone give me some suggestions? I have already seen the examples provided in the package and I do not seem to have found anything similar. Thanks in advance.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Using Google APIs to launch search from an application
« Reply #1 on: September 19, 2021, 07:53:28 am »
Not as of now as I just tried. The Custom Search API is a rather different beast than other APIs, probably due to its age. I tried converting the calendar demo but the API says it wants the API key parameter, which per Google's own article, should also be possible using OAuth 2.0 but clearly this is not the case.

So, I think you can ignore using that package for now, and do it a little bit more manually:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2.  
  3. uses
  4.   opensslsockets,fphttpclient,fpjson,jsonparser;
  5.  
  6. var
  7.   SearchResult,ResultItem: TJSONObject;
  8.   ResultEnum: TJSONEnum;
  9. begin
  10.   SearchResult := GetJSON(TFPHTTPClient.SimpleGet('https://www.googleapis.com/customsearch/v1?key=<use yours>&cx=<also use yours>&q=<your query here>')) as TJSONObject;
  11.   for ResultEnum in SearchResult.Find('items') do begin
  12.     ResultItem := ResultEnum.Value as TJSONObject;
  13.     WriteLn(ResultItem['title'].AsString);
  14.     WriteLn(ResultItem['link'].AsString);
  15.     WriteLn(ResultItem['snippet'].AsString);
  16.     WriteLn;
  17.   end;
  18.   SearchResult.Free;
  19. end.
  20.  
You can create the search engine ID (cx parameter) from here while the API key can be created here.

And if somebody is interested in making the demo works, attached is the converted calendar demo project to use TGoogleCustomsearchAPI.

simsee

  • Full Member
  • ***
  • Posts: 184
Re: Using Google APIs to launch search from an application
« Reply #2 on: September 20, 2021, 01:40:05 pm »
Thank you. I'll try as soon as I can.

 

TinyPortal © 2005-2018