Scripting a Google+ API search
Social media is a great way to gather information on a target company or person. Here, we will be showing you how to script a Google+ API search to find contact information for a company within the Google+ social sites.
Getting ready
Some Google APIs require authorization to access them, but if you have a Google account, getting the API key is easy. Just go to https://console.developers.google.com and create a new project. Click on API & auth | Credentials. Click on Create new key and Server key. Optionally enter your IP or just click on Create. Your API key will be displayed and ready to copy and paste into the following recipe.
How to do it…
Here's a simple script to query the Google+ API:
import urllib2 GOOGLE_API_KEY = "{Insert your Google API key}" target = "packtpub.com" api_response = urllib2.urlopen("https://www.googleapis.com/plus/v1/people? query="+target+"&key="+GOOGLE_API_KEY).read() api_response = api_response.split("\n") for line in api_response: if "displayName" in line: print line
How it works…
The preceding code makes a request to the Google+ search API (authenticated with your API key) and searches for accounts matching the target; packtpub.com
. Similarly to the preceding Shodan script, we set up our static strings including the API key and target:
GOOGLE_API_KEY = "{Insert your Google API key}" target = "packtpub.com"
The next step does two things: first, it sends the HTTP GET
request to the API server, then it reads in the response and stores the output into an api_response
variable:
api_response = urllib2.urlopen("https://www.googleapis.com/plus/v1/people? query="+target+"&key="+GOOGLE_API_KEY).read()
This request returns a JSON formatted response; an example snippet of the results is shown here:
In our script, we convert the response into a list so it's easier to parse:
api_response = api_response.split("\n")
The final part of the code loops through the list and prints only the lines that contain displayName
, as shown here:
See also…
In the next recipe, Downloading profile pictures using the Google+ API, we will look at improving the formatting of these results.
There's more…
By starting with a simple script to query the Google+ API, we can extend it to be more efficient and make use of more of the data returned. Another key aspect of the Google+ platform is that users may also have a matching account on another of Google's services, which means you can cross-reference accounts. Most Google products have an API available to developers, so a good place to start is https://developers.google.com/products/. Grab an API key and plug the output from the previous script into it.