Work with Google API in iOS application

BytePace
5 min readOct 31, 2019

--

On one of our project we had to work with Google API, especially Google Sheets API and Google Drive API.

The application has to be signed up by user who has an access to these files to start a work with private files.

The work with public files doesn’t require signing up, it needs to add suitable identificator, for example, API key.

In this article we’ll look at API data, how to work with it and make our lives easier.

Project creating in Google API Console:

Google API Console allows to manage different processes of your project as credentials creating, required API involvement and management of humans who work on this project.

On creating it needs to choose organisation, it will lead projects’ hierarchy that created with its domain:

Provide the project name and its identificator that you can’t change after creation. Pay attention to the fact that you can create a limited number of free projects.

The moment the project is created, we go to the dashboard and add API that we’ll use, in our case they are Drive и Sheets APIs:

Credentials creating for project:

Each request in Google API has to have unique identification that is required for console to attach requests to appropriate projects.

API key can be used in case of when you need to get information about the user or profile but there is no possibility to get an access to private files and make notes in speadsheet.

Protocol OAuth 2.0 allows to request the user acceptance for application data access. So, using this protocol we can get an access to Google signed up profile and generate token that has unique identificator which we’ll use for authentication and access to private files.

We can create in credentials identificator OAuth client as well as API key, but the second case requires set of request window for OAuth access. On identificator OAuth client creating it needs to pay attention to the application type and kit identificator that you couldn’t change after setting. You need to get Identity section in XCode to find kit identificator named Bundle Identifier:

It needs to download plist file of your OAuth identificator that has two keys which we’ll use further.

Reading/writing into spreadsheet’s public file:

To read public spreadsheet you need to make a request: GET https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/values:batchGet where spreadsheetId is id of required spreadsheet. Also you can add additional parameters:

  • APIKey is a key that has to be for reading
  • ranges is value range that we need to get
  • majorDimension is dimension which will have reading, especially rows and column

In our case we use this that spreadsheet:

After adding a value range A1:Z and reading by rows in parametres we get JSON:

We need to sign up with OAuth and add a token as an access_token parameter to make a write. In this case we don’t need an API key.

We make writing into the public spreadsheet by request: POST https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/values:batchUpdate. Also you need to add required parameters in request body:

  • valueInputOption is like your data will be interpreted, especially will they be formatted or stay same as user entered
  • data

Data contains of valueRanges array:

  • range
  • majorDimension
  • values — arrays’ array that will have values which had to be written

Send in body request this JSON:

Write into spreadsheet:

Google Sign-In Integration:

Google Sign-In is a framework that allows to integrate authorization through Google in this service, it’s interesting for us because it simplifies work with OAuth. After authorization you can get an access to Google profile, token that we will send on Google API work, and make a request for working with files of that user.

Google Sign-In set with CocoaPods:

  • In your project directory in terminal set: -pod init
  • In Podfile set pod ‘GoogleSignIn’
  • In terminal enter -pod install

Go to Info → URL Types and create new reference type that will work on callback. In URL Scheme set your key REVERSED_CLIENT_ID:

Most simple way to set main processes of this framework in AppDelegate, but taking into account that its methods will be called often enough, it’s recommend to stand it apart into another service.

Import GoogleSignIn:

Set a key CLIENT_ID and mark delegate that in our case will be service:

Implement application:openURL:options: method into AppDelegate:

Realize GIDSignInDelegate protocol using these methods:

Create authorization’s buttons:

Reading/writing into private spreadsheet file:

You can read spreadsheet directly with its id or get a list of files on Google Drive, get id of required file and read it. Let’s look the second option.

Get files list: GET https://www.googleapis.com/drive/v3/files. In parameters add:

  • access_token is OAuth token that we get with Google Sign-In
  • q is search parameter that helps to combine one or number of parameters for filtration. We need mimeType to display only Spreadsheets.

Create static variable and write token there to use it simpler:

Get a list of required files:

Reading from private spreadsheet is going same as public, you just need to use OAuth token instead of API key.

Example of writing into private spreadsheet with iOS application:

Sources:

https://console.developers.google.com

https://developers.google.com/drive/api/v3

https://developers.google.com/sheets/api

https://github.com/petrovdmitriy/GoogleAPI.ios

Thanks for reading! And join our website for more articles: http://bytepace.com

--

--

BytePace
BytePace

Written by BytePace

Mobile development: iOS, Android, Java, Swift, Objective-C, Design (Graphics, Web, Icon, Logo, UI/UX elements) www.bytepace.com

Responses (1)