I am trying to upload a file from a moblie phone to Google Drive, inside my android app. Basically Chaquopy is a plugin that enables me to run python scripts inside an android application and I use it to take advantage from PyDrive library and upload the file I want. But before that, I am just trying to stablish a connection with Google Drive but it refuses to retrieve the code from authorized redirect URI http://localhost:8080/ (that is how I configured my web application`s credential on google cloud project). I've already tried to transfer a file from pc to google drive with this project (and method) and it worked fine.
I've also tried to break gauth.LocalWebserverAuth()
in steps as suggested in PyDrive documentation but get no code at the end of authentication (can't connect to localhost).
I would like to use this method to upload the file, but other methods may be considered too.
my build.gradle (app level)
defaultConfig {
applicationId "com.example.epshape"
minSdk 24
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
ndk {
abiFilters "armeabi-v7a", "arm64-v8a", "x86", "x86_64"
}
python {
buildPython "C:/Users/Administrator/AppData/Local/Programs/Python/Python38/python.exe"
version "3.8"
pip {
install "pydrive"
}
}
}
my python script:
from pydrive.auth import GoogleAuth
import os
def upload_dataset():
GoogleAuth.DEFAULT_SETTINGS['client_config_file'] = os.environ['HOME']+'/client_secrets.json'
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
python script call from java activity class:
public void onClickUploadDataset(View view) {
Python py = Python.getInstance();
PyObject module = py.getModule("upload_dataset");
module.callAttr("upload_dataset");
}
error:
2023-11-12 23:05:37.636 32523-32523 python.stdout com.example.epshape I Failed to start a local web server. Please check your firewall
2023-11-12 23:05:37.636 32523-32523 python.stdout com.example.epshape I settings and locally running programs that may be blocking or
2023-11-12 23:05:37.636 32523-32523 python.stdout com.example.epshape I using configured ports. Default ports are 8080 and 8090.
2023-11-12 23:05:37.648 32523-32523 AndroidRuntime com.example.epshape D Shutting down VM
2023-11-12 23:05:37.650 32523-32523 AndroidRuntime com.example.epshape E FATAL EXCEPTION: main
Process: com.example.epshape, PID: 32523
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:473)
at android.view.View.performClick(View.java:8160)
at android.widget.TextView.performClick(TextView.java:16222)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1213)
at android.view.View.performClickInternal(View.java:8137)
at android.view.View.access$3700(View.java:888)
at android.view.View$PerformClick.run(View.java:30236)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8506)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:468)
at android.view.View.performClick(View.java:8160)
at android.widget.TextView.performClick(TextView.java:16222)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1213)
at android.view.View.performClickInternal(View.java:8137)
at android.view.View.access$3700(View.java:888)
at android.view.View$PerformClick.run(View.java:30236)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8506)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Caused by: com.chaquo.python.PyException: AuthenticationError:
at <python>.pydrive.auth.LocalWebserverAuth(auth.py:224)
at <python>.pydrive.auth._decorated(auth.py:115)
at <python>.upload_dataset.upload_dataset(upload_dataset.py:8)
at <python>.chaquopy_java.call(chaquopy_java.pyx:354)
at <python>.chaquopy_java.Java_com_chaquo_python_PyObject_callAttrThrowsNative(chaquopy_java.pyx:326)
at com.chaquo.python.PyObject.callAttrThrowsNative(Native Method)
at com.chaquo.python.PyObject.callAttrThrows(PyObject.java:232)
at com.chaquo.python.PyObject.callAttr(PyObject.java:221)
at com.example.epshape.MainActivity.onClickUploadDataset(MainActivity.java:70)
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:468)
at android.view.View.performClick(View.java:8160)
at android.widget.TextView.performClick(TextView.java:16222)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1213)
at android.view.View.performClickInternal(View.java:8137)
at android.view.View.access$3700(View.java:888)
at android.view.View$PerformClick.run(View.java:30236)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8506)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Thanks in advance!
I'm using another solution. I upload the file through a service account plus a shared folder in google drive. This video helped. I still run the python script with Chaquopy.