0

I have saved some serialized collection of objects as a file in my google drive. Now I would like to retrieve the collection in my android app. Is there any easy way to this?

  • make your file world readable. get the url for it. and call it using Android's httpurlconnection or OkHttp – Blundell Feb 05 '20 at 19:36
  • I already made it to public. Do I need client_id.json and stored.credential files because I used them to create the file from my web application or since the file is public those credentials are not necessary for android app? I want the file accessible for anybody who is using my android app without any intermediary authentication steps. Actually, I am goint to read the contents of the file in my android app. I am not going to save the file android user's phone. – ENGLISH TEACHER Feb 05 '20 at 19:47
  • if its public you don't need any credentials. – Blundell Feb 05 '20 at 19:51
  • Is it possible if I can create list of public files in my android app without credentials? – ENGLISH TEACHER Feb 05 '20 at 19:53
  • You mean query your drive for files and show them - no. – Blundell Feb 05 '20 at 19:54
  • oh.ok. My web app is creating files. My android app users should be able to view thenm and choose whatever they want. If I use the above solution, everytime I create a content through my web app, I have to add that shared link in my android app and deploy, which is not very desirable. – ENGLISH TEACHER Feb 05 '20 at 20:00

1 Answers1

2

Well google has created an api for these purposes. If it's just for your use (versus allowing access to other google users) you can authorize yourself as a service account (instructions here) which will give your application access to your drive account.

Then you'll need to build an application to accomplish the task. Based on what you described, your workflow will look something like reading your drive files, selecting the document you need and processing the information you need from it. You'll want to dive into their api which they separate out into several components that include drive, sheet, docs and gmail. Here's a couple resources:

Drive API

Searching Files

The application will look something like this (in python):

from googleapiclient.discovery import build
from google.oauth2 import service_account

SCOPES = [
    'https://www.googleapis.com/auth/drive',
    'https://www.googleapis.com/auth/documents',
]

SERVICE_ACCOUNT_FILE = 'credentials.json'
DOCUMENT_ID = '123adsb' # get this from the google doc url 


def get_credentials():
    return service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)


CREDENTIALS = get_credentials()
DRIVE = build('drive', 'v3', credentials=CREDENTIALS)
DOCS = build('docs', 'v1', credentials=CREDENTIALS)

def get_file(): # copy file from drive
    document = DOCS.get(documentId=DOCUMENT_ID).execute()
    print(dir(document)) # process document

if __name__ == '__main__':
    get_file()
Braden Holt
  • 1,544
  • 1
  • 18
  • 32