Authenticating with Google Drive

 In CloudRail

In this post we will guide you through the process of setting up your application for the use of Google Drive. Due to a policy change in the way developers are supposed to handle the authentication with Google services, the setup is slightly different than the one you know from other services. Google does not allow login requests from in-app-WebViews anymore, which is the standard way of logging in users with CloudRail, but only from on-device web browsers. The following paragraphs will illustrate the changes required to successfully configure a Google Drive application as well as the correct use of the advanced authentication.

Getting Google Drive Credentials

Use this link to reach the Google Developer console (you may have to login first): https://console.developers.google.com/iam-admin/projects. Click on “Select a project” and then create a new project by clicking on the “+” Button in the top right corner of the dialog that pops up.

Enter a name for the project and click “Create”.

You will get to the API overview page. Enter “drive” into the search field and select the Google Drive API.

Then, click on “Enable” in the top-left corner of the screen.

On the next screen, click on “Create credentials”. Choose the platform you want to use (for example Android) and select “User data”. Then, click on “What credentials do I need?”.

In the next step, enter the information required from you, like for example the iOS bundle id or Android package name and click on “Create Client ID”. The additional information is needed so that the service knows which app should be opened from the browser. Fill in the information for the Google Drive consent screen, and click “Continue”. Afterwards, you will receive your Client ID.

For the Google Drive service, there is no client secret. when using the SDK you need to pass an empty String as the secret!

What is CloudRail Advanced Authentication?

By default, CloudRail uses in-app-WebViews to perform user logins. When using Google services, it is however necessary to login users using an on-device browser because Google does not allow user logins from in-app-WebViews. This is what CloudRail’s Advanced Authentication feature is for: it redirects the user to the service’s website in a browser on their device. After logging in, the website will redirect the user back to the app, passing along an authorization code. The CloudRail SDK takes care of all steps it possibly can, yet there are some minor steps that need to be handled within your application.

Using CloudRail Advanced Authentication with Google Drive

First, set up the CloudRail SDK as usual and create the service. For the Client Secret parameter, pass along an empty string. Set the Redirect URI to something like “[YourPackageName]:/oauth2redirect” (Android) or “[YourBundleId]:/oauth2redirect” (iOS) respectively. Then, activate the Advanced Authentication feature as follows.

Android

iOS (Objective C)

iOS (Swift)

When not using Advanced Authentication, the SDK will just grab the authentication response from the webview that opened the login page. But because we open that page in an external browser, we have to tell the OS that our app should handle URLs that correspond to the redirect URI. This is done by adding the following pieces of code to the AndroidManifest.xml file if you’re using Android, or to the info.plist file if you’re using iOS:

Android

Add the intent filter to the Activity that triggered the authentication. Notice that we add android:launchMode=”singleTask” to the activity which will prevent Android from restarting your Activity when performing the redirect.

iOS

Add the following right underneath the top level.

What we have achieved so far is that we open the external browser for the authentication. And we listen for the redirect that is performed when the user grants access. Now we have to handle the information coming back. For both, Android and iOS this means, implementing a certain method. This method needs to pass the information to our SDK. Here is the code for the different platforms:

Android

iOS (Objective C)

iOS (Swift)

Examples

There is a minimalistic iOS Objective C example that uses the Google Drive API here:
https://github.com/CloudRail/cloudrail-si-objc-sdk/tree/master/Examples/AdvancedAuthentication/
You can also check out our Unified Cloud Storage examples:
Android: https://github.com/CloudRail/cloudrail-si-android-sdk/tree/master/ExampleProjects/UnifiedCloudStorage
iOS (Objective C): https://github.com/CloudRail/cloudrail-si-objc-sdk/tree/master/Examples/UnifiedCloudStorage
iOS (Swift): https://github.com/CloudRail/cloudrail-si-ios-sdk/tree/master/Examples/Cloudstorage

Recent Posts