shashi278 / social-auth-kivy

Integrate Google, Facebook, Github & Twitter login in kivy applications
MIT License
150 stars 23 forks source link

Integrating google auth in my own application #15

Closed MasterJubei closed 3 years ago

MasterJubei commented 3 years ago

Hi, thanks for making this

The demo app works on the playstore for me. However I cannot seem to get it to work. I created a google cloud oauth2 credentials for android. I then get my clientid.

In the code, I replaced GOOGLE_CLIENT_ID with my own string. I left the google_client_secret as "secret" since I do not think that gets used?

However when I run it, and then click on my google account and it tries to login. I get:

07-04 21:01:46.104 21011 21011 W SDLThread: type=1400 audit(0.0:131814): avc: granted { execute } for path="/data/user/0```
/com.dslice.imgupload/files/app/_python_bundle/modules/mmap.cpython-38.so" dev="dm-8" ino=202643 scontext=u:r:untrusted_app_27:s0:c163,c257,c512,c768 tcontext=u:object_r:app_data_file:s0:c163,c257,c512,c768 tclass=file app=com.dslice.imgupload
07-04 21:01:46.444 21011 21086 I python  : [WARNING] [Base        ] Unknown <android> provider
07-04 21:01:46.444 21011 21086 I python  : [INFO   ] [Base        ] Start application main loop
07-04 21:01:50.729 21011 21086 I python  : [INFO   ] [KivyAuth    ] Initiated google login
07-04 21:01:52.611 21011 21011 I python  : [INFO   ] [KivyAuth    ] google_activity_listener called.
07-04 21:01:52.619 21011 21011 I python  : [INFO   ] [KivyAuth    ] Error signing in using Google. JVM exception occurred: 10: 

My build looks the same,

    def build(self):

        initialize_google(

            self.after_login,

            self.error_listener,

            GOOGLE_CLIENT_ID,

            GOOGLE_CLIENT_SECRET,

        )

Right now the top of my code looks like (edited the google client ID a bit):

GOOGLE_CLIENT_ID = (

    "394092214639-t40tofvmef5ds50kvi72XXXXXprko0p.apps.googleusercontent.com"

)

GOOGLE_CLIENT_SECRET = "secret"

os.environ["SSL_CERT_FILE"] = certifi.where()

if platform == "android":

    from android.runnable import run_on_ui_thread

    from jnius import autoclass, cast

    Toast = autoclass("android.widget.Toast")

    String = autoclass("java.lang.String")

    CharSequence = autoclass("java.lang.CharSequence")

    Intent = autoclass("android.content.Intent")

    Uri = autoclass("android.net.Uri")

    #NewRelic = autoclass("com.newrelic.agent.android.NewRelic")

    LayoutParams = autoclass("android.view.WindowManager$LayoutParams")

    AndroidColor = autoclass("android.graphics.Color")

    PythonActivity = autoclass("org.kivy.android.PythonActivity")

    context = PythonActivity.mActivity

# Store user_info in user_info.txt for now

if(os.path.isfile("user_info.txt")): 

    open("user_info.txt")

    File_object = open("user_info.txt","r+")    # open in rw

else:

    File_object = open("user_info.txt", "a")    # create, and open in rw

os.environ['SSL_CERT_FILE'] = certifi.where()

#'https://3dslice.net/imgUpload/php_form_test/test.php'

Builder.load_file('imgUpload.kv')

class MyLayout(Widget):

    pass

class Content(BoxLayout):

    pass

class imgUploadApp(MDApp):

    def build(self):

        initialize_google(

            self.after_login,

            self.error_listener,

            GOOGLE_CLIENT_ID,

            GOOGLE_CLIENT_SECRET,

        )

        return MyLayout()
shashi278 commented 3 years ago

Hi @MasterJubei, make sure you go through the pre-requisite for google auth and fill all the details correctly on the google console page. This error generally comes up when the package name or SHA key is mismatched.

MasterJubei commented 3 years ago

Hi @shashi278 Thanks a lot, I believe you are correct.

For anyone else, I believe this should resolve that issue. https://github.com/shashi278/social-auth-kivy/blob/master/docs/prerequisites.md

To generate a keystore file, I recommend just using Ubuntu/linux rather than Windows Java. I am just saying this because I first tried it with Windows and it didn't work BUT I could have easily just made a typo.

In buildozer I have

# (str) Title of your application
title = imgUpload

# (str) Package name
package.name = imgUpload

# (str) Package domain (needed for android/ios packaging)
package.domain = com.dslice

So my full package name is com.dslice.imgupload

To generate a keystore: https://stackoverflow.com/questions/3997748/how-can-i-create-a-keystore

For me I ran: (imgupload is my program name) keytool -genkey -v -keystore newkey.keystore -alias imgupload -keyalg RSA -keysize 2048 -validity 10000

Then in the same directory as the newkey.keystore keytool -keystore newkeystore.key -list -v

Put that in the google cloud form which asks for the SHA1 fingerprint.

Then in your code add:

GOOGLE_CLIENT_ID = (

    "394092214639-xxxxxxxxxxxx.apps.googleusercontent.com"

)
GOOGLE_CLIENT_SECRET = "secret"

And follow the example provided https://github.com/shashi278/social-auth-kivy/blob/master/demo/main.py

I also added the export P4A commands, I am fairly sure you to run those before running buildozer in the same terminal session.

https://github.com/kivy/kivy/wiki/Creating-a-Release-APK

From here before building the latest version of my app, I beleive it has to be a release, not debug. So make sure you run with buildozer -v android release

Then it seems to work

07-05 00:10:58.063 11374 11374 I python  : [INFO   ] [KivyAuth    ] Google Login success. Calling success listener.
07-05 00:10:58.192 11374 11374 W SDLThread: type=1400 audit(0.0:135768): avc: granted { execute } for path="/data/user/0/com.dslice.imgupload/files/app/_python_bundle/modules/unicodedata.cpython-38.so" dev="dm-8" ino=106083 scontext=u:r:untrusted_app_27:s0:c168,c257,c512,c768 tcontext=u:object_r:app_data_file:s0:c168,c257,c512,c768 tclass=file app=com.dslice.imgupload

However I am facing one issue so far,

I am trying to replace an image with the google login one, but I get [WARNING] [GL ] Unpack subimage support is not available

my after_login is

    def after_login(self, name, email, photo_uri):

        self.user_avatar = AsyncImage(source = photo_uri)

        self.user_avatar.reload()

It just has the old picture though. I might be doing something wrong though.

shashi278 commented 3 years ago

Then in your code add:

 GOOGLE_CLIENT_ID = (

     "394092214639-xxxxxxxxxxxx.apps.googleusercontent.com"

 )
 GOOGLE_CLIENT_SECRET = "secret"

You don't need to add client ID and secret if you're only targeting android. Those are only required for desktop.

From here before building the latest version of my app, I beleive it has to be a release, not debug. So make sure you run with buildozer -v android release

You don't need to build it for release if you're only testing it out on your device.

However I am facing one issue so far,

I am trying to replace an image with the google login one

Shouldn't it be possible by simply using a new instance of AsyncImage? I'm not sure why you doing a reload.

MasterJubei commented 3 years ago

Thanks for the tips. I got it after messing with it a bit. Before the code was a bit odd. I already had an AsyncImage loaded by default. But in the code above I was basically linking self.user_avatar to a new AsyncImage instead of replacing the current one's image.

So with that:

    def after_login(self, name, email, photo_uri):

        if platform == "android":

            show_toast("Logged in using {}".format(self.current_provider))

        self.user_avatar.source = photo_uri

        self.user_avatar.reload()

Thanks for the help! You are also correct that I do not have to make a release version. I also went ahead and removed the google_client_id code and it still works as you mentioned on Android.

I really appreciate you for making this library!

MasterJubei commented 3 years ago

Also I should add, for me buildozer did not always seem to use the P4A values I specified.

But what I can do is get the SHA1 fingerprint from the output .apk file

keytool -printcert -jarfile imgupload-0.1-armeabi-v7a-debug.apk

Gives me the fingerprint so I can put that in google cloud

shashi278 commented 3 years ago

Glad it worked!

Also I should add, for me buildozer did not always seem to use the P4A values I specified.

It should. What fields you think are not working? Also it's recommended to get your SHA from the keystore you've created because I believe apk fingerprint can be changed sometimes, for example, when uploading your app to playstore and opting for 'let Google sign my apk'(which is recommended in play console) option.

MasterJubei commented 3 years ago

The SHA1 fingerprint was being used from my keystore, and that is put on google cloud. But for some reason my output for the apk stopped using them. I am not sure why this is. I was using the same terminal session back when it originally was working. Buildozer also is respecting the P4A commands in some way at least because if I put a nonexistant keystore with export P4A_RELEASE_KEYSTORE=~/keystores/<your-new-key>.keystore it will complain.

So for now I have both the keystore fingerprint and the apk file's fingerprint on google cloud. They should be the same, but I am not sure what's going on there.

shashi278 commented 3 years ago

So they're not same? Again I'm not sure why you'd be exporting release keystore if you're only testing out your apk in debug mode.

Also something to note, if you're creating a release version of you app and uploading it to the play store and opting for 'let Google sign my apk' then you'd need to add/update the Google provided SHA into your Google cloud console for your app otherwise it won't work because the app on play store won't have your local SHA anymore. This is a very common mistake that, I guess, most people make when working with social authentication. You'd need to update your SHA everywhere if you're using Facebook Auth, firebase(Github and Twitter) Auth for your app to work after releasing it to the playstore. It think this info should be in the Readme as well

MasterJubei commented 3 years ago

Oh, that's probably it... I assume I should be using something like P4A_DEBUG_KEYSTORE instead right? (Since I am running the app in debug mode for now)

shashi278 commented 3 years ago

Keystore is used to sign your application and you need to sign your application only when you're building the release version so that when you update your application, the play console knows that it's from you. When you build release version of your application using buildozer, it expects those environment variables to be set(exported). No such requirements for building debug app.

AM-ash-OR-AM-I commented 2 years ago

Then in your code add:

GOOGLE_CLIENT_ID = (

    "394092214639-xxxxxxxxxxxx.apps.googleusercontent.com"

)
GOOGLE_CLIENT_SECRET = "secret"

You don't need to add client ID and secret if you're only targeting android. Those are only required for desktop.

From here before building the latest version of my app, I beleive it has to be a release, not debug. So make sure you run with buildozer -v android release

You don't need to build it for release if you're only testing it out on your device.

However I am facing one issue so far, I am trying to replace an image with the google login one

Shouldn't it be possible by simply using a new instance of AsyncImage? I'm not sure why you doing a reload.

I've same issue as @MasterJubei had. But as you said CLIENT_ID and CLIENT_SECRET are not needed for android only. That should mean that we don't need SHA-1 hash or any app in cloud console? But, even after I tried to get hash I get same error message as his. Also as you said release is not needed for app if it just needs to be tested then why my app is still not working can you tell?

shashi278 commented 2 years ago

But as you said CLIENT_ID and CLIENT_SECRET are not needed for android only. That should mean that we don't need SHA-1 hash or any app in cloud console?

It does not mean that. You'd need to add correct hash and package name in the cloud console.

AM-ash-OR-AM-I commented 2 years ago

But as you said CLIENT_ID and CLIENT_SECRET are not needed for android only. That should mean that we don't need SHA-1 hash or any app in cloud console?

It does not mean that. You'd need to add correct hash and package name in the cloud console.

I did that still I'm getting same error. When setting package name in the cloud console in the package name section we'll set it equal to package name that we set in buildozer.spec like package.name = ProjectRed ? image

AM-ash-OR-AM-I commented 2 years ago

You don't need to add client ID and secret if you're only targeting android. Those are only required for desktop.

Can you make it clear as you are saying that it's not needed to add client id and secret here? [EDIT] Now, I got it.

AM-ash-OR-AM-I commented 2 years ago

So, I finally understood what do, If anybody still has this issue while making debug app then refer #19