Google Drive API with Android
Tags: googledirve, android, google, java
Published: 2019-12-24 10:02:29

---
title: Google Drive API with Android
published: true
description: Google Drive API with Android
tags: #googledirve #android #google #java
---

There are many ways to upload images and files. Also, there are many places from which to uploads files: Dropbox, local storage on the device, Google Drive and other services. In this tutorial, we are going to take a look at how to upload and create a file in Google Drive. To do so we need to Google Drive SDK and Google Authentication Base on Google Drive Rest API we are going to do that.

Okay, it's enough to talk about it 😎 Let's get into it,


# Getting Started

Start a new `Android Studio project` from the startup screen or `New Project` then select `Empty Activity' from the `Choose Project` box.

Enter the name `drive-rest-app` company domain and a project location. Make sure to Choose Language Java.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/rgomvmv3qjodb0vlqq1g.png)

And Click `Finish` .


# Registering for Google Drive

As we discuss we need to Google Drive SDK, So in order to use it, we need to enable that API. Okay don't worry we will move forward step by step

- Go to [Google Console](https://console.developers.google.com/projectselector/apis/dashboard).
- Sign up for a developer account if We don't have or then sign in.
- `Create` a project Or `Select` and click continue from below,

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/9tkmgg6z7s5fup0vxfxs.png)

- After creating a project we have `Dashboard` like below,

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/yd30f54tv84u4zbbqwup.png)


- Now, Select `Library` on the left-hand side to go to the Search screen
- Type “Google Drive” and select Google Drive API.
- Select Enable. Then It looks like below,

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/3jyyhw1qc16zdii50mye.png)

- Back to Dashboard, from Dashboard left-hand side go to Credentials. Create `Credential` by selecting the `OAuth client ID` like below

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/985j2fol9qo84nd04gln.png)

Tt navigates to the `Create OAuth client ID` page. Select our Application Type `Android`.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/uc5b0l55srkulmptlo6x.png)

- Now we need your `SHA-1` `Signing-certificate fingerprint` key. Do so follow below steps
- Copy the keytool text (press the Copy icon) and paste it into a terminal.
- Change the path-to-debug-or-production-keystore to your default debug keystore location:
- On Mac or Linux, `~/.android/debug.keystore` .
- On Windows, `%USERPROFILE%/.android/debug.keystore` .
- After you execute the command, you will be prompted to enter the keystore password. The password for the debug keystore is blank by default, so you can just press Return or Enter.


> Notes: If throwing error to generate singing key then it might be `keystore` not found. 

I used an alternative command for my machine `MAC OS`.

```bash
keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android
```


If everything works correctly, you should see something like this:
- Copy the `SHA1` value from the terminal into the text field and press Create. The Client ID dialog will appear. Press OK.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/yeb93rnpvwwfspb7u6wc.png)

- Finally, enter a name and the `package name` that we used to `create our app`. Although the hint refers to the package name in `AndroidManifest.xml`, it has to match the `applicationId` in `build.gradle` instead — otherwise, the login flow will fail.
- We don't need to complete the form that appears, it's optional so press `Save` and move on.

> Optional, Now, In the Credentials page. Authorization on Android uses the `SHA1` fingerprint and `package name` to `identify your app`, so you `don’t have to download` any `JSON file or copy any API key` or secret to our project.

It about lots man, Don't worry! Now, we are in the fun part 😎.


# Let's Back to the coding part,

> Sorry! `Dev.to` can't render source files properly, the rest of the part will cover below.

- https://github.com/mesadhan/google-drive-app
- https://github.com/mesadhan/google-drive-app/blob/master/README.md