Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing Google custom search API in iOS

I went through several links in order to find the proper steps to implement google customsearchapi in an ios application and spent about 6-7 hours in that process.

Links:

  • https://developers.google.com/custom-search/json-api/v1/introduction
  • http://developers.google.com/apis-explorer/#p/customsearch/v1/search.cse.list?q=a&_h=1&
  • https://productforums.google.com/forum/#!topic/customsearch/hT2fnfErVwo
  • Google Custom Search: 403 error in iOS
  • And Father of all

All these provide bits and peaces of formation. Is there any place to have a summarize, precise info that can help to implement the custom search in an iOS application?

like image 531
rptwsthi Avatar asked Nov 20 '14 11:11

rptwsthi


People also ask

How do I use Google Custom Search API?

Custom Search JSON API requires the use of an API key. An API key is a way to identify your client to Google. After you have an API key, your application can append the query parameter key=yourAPIKey to all request URLs. The API key is safe for embedding in URLs, it doesn't need any encoding.

Is there a Google API for search?

The Search Console API provides programmatic access to the most popular reports and actions in your Search Console account. Query your search analytics, list your verified sites, manage your sitemaps for your site, and more. Official Google Search updates and SEO best practices.

How do I get a custom search API key?

You can get an API key by visiting https://code.google.com/apis/console and clicking "API Access". You will then need to switch on the custom search API on the "Services" tab.

Is SERP API free?

Start using the API — It's free! Our powerful cloud infrastructure is built to withstand high volume API requests without the need of a queue. Tailor your automated search queries based on a series of options, including location, language, device, and more.


2 Answers

Brief Step of the process:

  1. Create a Google account (ignore if you have one)
  2. You may found some piece of information related to pricing at the bottom of this page helpful(you can ignore this too)
  3. Create a project and generate the API key
  4. Go to google console and create a project
  5. After the project is created, click on it to go to its detail.
  6. On the left bar under the Auth&API segment, click on APIs.
  7. Now you'll find CustomSearchAPI link in the Browse APIs section (as it is not activated by default), turn it on by clicking on the button at right.
  8. Now click on Credentials, just below the APIs option
  9. On this page under "Public API Access" click on Create New Key Button, for now, choose the browser key(as at first we wanna test it on the browser), create it, and leave it, as it is for now.
  10. Create Custom Search Engine
  11. Now on the new tab, open Custom Search Engine page. On this page click on Create a custom search engine, button
  12. That will lead you to create a new search engine page, here give your domain name in the "Sites to search" field. (If you don't have one no worries, give anything, that has www. at the start and .com in the end)
  13. Fill in the name, if it hasn't pick one already, then click on create.
  14. So you got a jumping robot to congratulate you? ;) Yeah, that's it. On this page step up to "Modify your search engine", by clicking on, "Control Panel" button
  15. There you are, now turn on the Image Search, (if you want to)
  16. Also in the "Sites to search" section, select, "Search the entire web but emphasize on the included item", instead of, the default one, which is "Search only included site"
  17. That is it, at the bottom of this page click on update. And then come back to the middle of the page and under the "Detail" title, click on Search engine ID, copy the Id, paste it somewhere.
  18. Search, using get request:
  19. To make a get request use this request URL
  20. In it replace, {API_KEY} which you have created under "Create a project and generate API key" section
  21. And replace {SEARCH_ENGINE_KEY} with the Search engine Id you just copy-pasted Call it with some different value, at query string, then 'a', https://www.googleapis.com/customsearch/v1?**q=a**&key={API_KEY}&cx={SEARCH_ENGINE_KEY} change a with anything you wanna search you must have got the beautiful JSON of search result
  22. Other Stuff
  23. If you wanna see the request status, go back to your project page, that how many requests placed, how many of those failed, etc. Click on the overview and you will get the graph for that, love you google
  24. If you have trouble with JSON, here are some links at your service,
    1. What is JSON 1, 2?
    2. Use JSON in ios.
    3. Use JSON in android.
like image 153
rptwsthi Avatar answered Sep 20 '22 08:09

rptwsthi


The following provide implementation in Swift 4, of "GET" request from google custom search engine,

let apiKey = "Your api key here"
let bundleId = "com.Your uniqueBundleId here"
let searchEngineId = "Your searchEngine here"
let serverAddress = String(format: "https://www.googleapis.com/customsearch/v1?q=%@&cx=%@&key=%@","Your query here" ,searchEngineId, apiKey)


let url = serverAddress.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
let finalUrl = URL(string: url!)
let request = NSMutableURLRequest(url: finalUrl!, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10)
request.httpMethod = "GET"
request.setValue(bundleId, forHTTPHeaderField: "X-Ios-Bundle-Identifier")

let session = URLSession.shared

let datatask = session.dataTask(with: request as URLRequest) { (data, response, error) in
    do{
        if let jsonResult = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary {
            print("asyncResult\(jsonResult)")
        }
    }
    catch let error as NSError {
        print(error.localizedDescription)
    }
}
datatask.resume()
like image 20
Frostmourne Avatar answered Sep 19 '22 08:09

Frostmourne