Kivy for mobile and use drive files

Viewed 35

Y want to access googel drive file from my mobile, in apk made in kivy. in PC is workink perfectly,but not in mobile,when is loading...crash. tihs is my code:

from pydrive2.auth import GoogleAuth
from pydrive2.drive import GoogleDrive

gauth = GoogleAuth()
# Try to load saved client credentials
gauth.LoadCredentialsFile("mycreds.txt")
if gauth.credentials is None:
            # Authenticate if they're not there

            # This is what solved the issues:
            gauth.GetFlow()
            gauth.flow.params.update({'access_type': 'offline'})
            gauth.flow.params.update({'approval_Prompt': 'force'})

            gauth.LocalWebserverAuth()

elif gauth.access_token_expired:
            # Authenticate if they're not there
            gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
            # Refresh them if expired

            gauth.Refresh()
            # gauth.LocalWebserverAuth()
else:
            # Initialize the saved creds
            gauth.Authorize()
            # gauth.LocalWebserverAuth()
        # Save the current credentials to a file
gauth.SaveCredentialsFile("mycreds.txt")
drive = GoogleDrive(gauth)
#print(type(self.drive))
# PUT YOUR FILE ID AND ANY-NAME HERE
file_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' #(my id is ocult here)
file_name = "compra.xlsx"  # You can use existing drive file name / totally different name
# Get contents of your drive file into the desired file. Here contents are stored in the file specified by 'file_name'
downloaded = drive.CreateFile({'id': file_id})
downloaded.GetContentFile(file_name)

is posible to do run in android? thanks!!

1 Answers

You may need to request INTERNET permissions. Try something like this at the top of your main python file:

from kivy.utils import platform

if platform == 'android':
    perms = ['android.permission.INTERNET']
    from android.permissions import request_permissions
    request_permissions(perms, None)
Related