Monday, 10 December 2012

Google Webservice Simple API Access in Objective-C

This is as much an aide-m矇moire for myself as anything else, but in case you're using the Objective-C Google API client, you may at some point need to use the simple API access method. This is where you pass an API key to identify the call as being under a certain project, rater than using a full OAuth2 authentication flow. This doesn't allow access to personal data, but does mean that any calls will be tracked under the quota for your project, rather than a (lower) IP based quota.

It turns out to be very simple. First, go to the API console and the API Access option on the left menu. At the bottom there will be a simple API access section where you can generate new keys. The "API key" value here is the one you need.


Don't worry, I regenerated the key after taking the screenshot! We can then pass the key in as a parameter to the service we create using the APIKey property. In this case I'm using the GTLServicePlus which queries Google+, and using the activities search to retrieve posts that contain the string "objective-c". The same process will work on any GTLService you're using though, as long as it is a public data API and doesn't require OAuth2 authorisation.



It's also possible to pass the API key as an additional parameter to the GTLQuery, but this is definitely the easiest method!