Google's OAuth2 with Kivy?

1,738 views
Skip to first unread message

Bill Janssen

unread,
Jun 21, 2014, 3:13:16 AM6/21/14
to kivy-...@googlegroups.com
Google has an authentication dance they want apps to use, if they want to access a user's Google data.  It works like this:

1.  An app that requires access gets a Google 'client ID' and 'client secret'.

2.  To authenticate a user, the app opens a browser window on a specially formed URL which points to a Google server, and includes the client ID and client secret.  The Google page that comes up allows the user to log in and allow access.

3.  The result of allowing access is an HTTP redirect to a URL such as "http://localhost:50031".  Your app must be listening for connection requests on that port, and read the GET request that comes to it when the browser performs the redirect.  A parameter of the URL is an authorization token.

4.  The app takes the authorization token and performs further dancing with Google using it.  The gory details are at https://developers.google.com/accounts/docs/OAuth2InstalledApp.

The question I have is, how to do #3 using Kivy?

My best guess is to bind to a port and listen on it, then schedule a periodic work event which checks to see if anyone is connecting to it.  If so, the event handler will accept the connection, read a line, and see if it matches the authentication token redirect pattern.

Should that work?

Also, I was just going to use Python's normal "webbrowser.open()" call to open the browser page.  Anyone know if that works on Android and iOS?

TIA,

Bill

Alexander Taylor

unread,
Jun 21, 2014, 6:20:09 AM6/21/14
to kivy-...@googlegroups.com
I didn't look much at the other parts, but the webbrowser module won't work on android and ios. You can instead do it the normal android way with pyjnius, e.g. like knappador has at https://github.com/knappador/kivy-browser/blob/master/src/browser/androidbrowser.py

Bill Janssen

unread,
Jun 21, 2014, 12:51:05 PM6/21/14
to kivy-...@googlegroups.com
There's a lot of pages on the Web suggesting that if pygame or SL4A is installed, webbrowser does indeed work.

Haven't tried it yet.

Bill

Tim H.

unread,
Jun 21, 2014, 2:34:02 PM6/21/14
to kivy-...@googlegroups.com
I have already build a kivy app that uses OAuth2 to connect to google services.
Best regards
Tim

qua non

unread,
Jun 21, 2014, 3:24:21 PM6/21/14
to kivy-...@googlegroups.com
import web browser
webbrowser.open(url) should and does work on android.

If it doesn't then maybe there was a regression? @alexender did you test this?
I have it working in my last app.



--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bill Janssen

unread,
Jun 21, 2014, 8:14:35 PM6/21/14
to kivy-...@googlegroups.com
Thanks for the pointer, Tim.  I was looking for something a bit leaner, though.  I've got it working without httplib2 or any of the Google toolkits.  Bit lower-level.

Bill

Bill Janssen

unread,
Jun 21, 2014, 8:16:31 PM6/21/14
to kivy-...@googlegroups.com
On Saturday, June 21, 2014 12:13:16 AM UTC-7, Bill Janssen wrote:
My best guess is to bind to a port and listen on it, then schedule a periodic work event which checks to see if anyone is connecting to it.  If so, the event handler will accept the connection, read a line, and see if it matches the authentication token redirect pattern.

Should that work?

Seems to work just fine.  I'll post my code after I test the webbrowser stuff on Android.

Bill 

Bill Janssen

unread,
Jun 22, 2014, 7:08:38 PM6/22/14
to kivy-...@googlegroups.com
Just to verify, using webbrowser.open() and listening on a local port works just fine on Android and OS X.

The one thing I don't know how to do is to revert back to my app after the browser page authentication is finished.

Bill


On Saturday, June 21, 2014 12:13:16 AM UTC-7, Bill Janssen wrote:

qua non

unread,
Jun 23, 2014, 6:18:43 AM6/23/14
to kivy-...@googlegroups.com
Create a unique url that only your app is registered to open and make your browser goto that url after your authentication is done.


--

Bill Janssen

unread,
Jun 23, 2014, 3:19:36 PM6/23/14
to kivy-...@googlegroups.com
Thanks.  I'll have my app redirect to that URL in its reply to the redirect from the browser.

Bill

Bill Janssen

unread,
Jun 23, 2014, 3:25:05 PM6/23/14
to kivy-...@googlegroups.com
Is this a reasonable summary of how to do it?



On Monday, June 23, 2014 3:18:43 AM UTC-7, qua-non wrote:

qua non

unread,
Jun 24, 2014, 5:34:36 AM6/24/14
to kivy-...@googlegroups.com
Yes, this seems like a good summary of how to approach this problem and it's solution.

Bill Janssen

unread,
Jun 24, 2014, 11:16:18 AM6/24/14
to kivy-...@googlegroups.com
Thanks.

Bill

avi

unread,
Jan 29, 2016, 9:07:40 AM1/29/16
to Kivy users support
Bill, I am trying to do similar thing. How did you approach this? Would you mind posting the code?

Giacomo Debidda

unread,
Feb 27, 2016, 5:25:03 AM2/27/16
to Kivy users support
Hi everyone!

I'm trying to send an email when a button is pressed.
I followed the Google Gmail API quickstart guide, and this answer on Stack Overflow, and it works fine when I run the app on my computer (Xubuntu 14.04). However, when I try to run it on my smartphone, it crashes immediately.

All the functions needed to create the HTTP client, get the OAuth2 credentials (either with gmail.storage or flow), build the message, and send the email are defined in a python script called email_oauth2.py

Here are the imports of email_oauth2.py
import base64
import httplib2
import argparse
from email.mime.text import MIMEText
from apiclient import discovery
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client import tools

In main.py I simply do:
import email_oauth2
email_oauth2.set_and_send(my_message='my message here')

I'm using buildozer, and it doesn't complain when packaging the APK. I included these requirements in buildozer.spec (I could't include base64).
requirements = kivy,datetime,numpy,google-api-python-client,httplib2,argparse,oauth2client,email

The APK also needs a client_secret.json (and I guess the mail.storage file?) but they are in the app root folder, and they are included in buildozer.spec as well.

I really don't understand what else is missing...

Bill Janssen

unread,
Feb 27, 2016, 6:46:15 PM2/27/16
to kivy-...@googlegroups.com

--
You received this message because you are subscribed to a topic in the Google Groups "Kivy users support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/kivy-users/8XEfiP4YOkg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to kivy-users+...@googlegroups.com.

Giacomo Debidda

unread,
Mar 16, 2016, 2:57:14 PM3/16/16
to Kivy users support
I don't get it...
Logcat tells me that it can't import iri2uri, which is required by httplib2. I tried to include iri2uri in the buildozer.spec file, but I wasn't able to create the APK.

In another thread someone suggests to use Android flow, instead of web flow.


Il giorno sabato 21 giugno 2014 09:13:16 UTC+2, Bill Janssen ha scritto:

Booze Aholic

unread,
Apr 20, 2016, 7:25:36 PM4/20/16
to Kivy users support
Just a quick follow-up on this last point, and not to get too off-topic.

I suspect the problem (at least as late as summer of 2015 when I was messing around with this) was not with using the android flow or webflow, but seemed to be that the version of buildozer I was using at that time wasn't correctly linking something (I suspect openssl) correctly.

In fact I was able to get this to work using python-for-android without changing anything in the python code base. So you can see if this will work for you.

Using the old python for android toolchain:

./distribute.sh -m "openssl numpy kivy"

cd dist/default

./build.py --dir /my-kivy-project \
    --package org.test \
    --name "herpderp" --version 0.0.1 \
    --orientation sensor \
    --permission INTERNET debug installd

Incidently, I eventually also got this to work for kivy-ios. The fact that the python code base works (at least for a native app) for authentication on devices just as it does on PCs after building my app with python-for-android and kivy-ios pretty indicates to me that buildozer is where the problem lies. It seems important to fix and I had been meaning to go back and try to document what exactly was the issue with buildozer but haven't found the time yet.



2016年3月16日水曜日 14時57分14秒 UTC-4 Giacomo Debidda:

MD Azri

unread,
Jun 16, 2017, 4:12:01 AM6/16/17
to Kivy users support
Yeah I second to the post above, but you do not need the old toolchain. Just compile with p4a instead of buildozer and it will work fine.
Reply all
Reply to author
Forward
0 new messages