Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android Browser Not Launching for OAuth 2.0 #1032

Closed
frankgould opened this issue Apr 7, 2017 · 3 comments
Closed

Android Browser Not Launching for OAuth 2.0 #1032

frankgould opened this issue Apr 7, 2017 · 3 comments

Comments

@frankgould
Copy link

I am trying to get the Android browser to launch running Google OAuth 2.0 python code but nothing happens after displaying the following lines in the console. The code works in Windows 7 and last year when I could compile using OSX, now it's not working on Linux buildozer compile. I cannot compile on OSX due to this issue: #924.

I/python ( 2947): Your browser has been opened to visit:
I/python ( 2947): https://accounts.google.com/o/oauth2/auth?scope=https[...]&access_type=offline
I/python ( 2947): If your browser is on a different machine then exit and re-run this
I/python ( 2947): application with the command-line parameter
I/python ( 2947): --noauth_local_webserver

I can manually launch the browser, Chrome in this instance, paste the [full] url from above, and it authenticates correctly. There are no webbrowser.py error codes or message and it doesn't time out. I searched for anything relevant to this in Android but could not find any suggestions. This is Android 4.4.2.

I asked the google/google-api-python-client forum and got this reply "the underlying library just uses the Python standard library webbrowser package. So if you upgraded python or moved to a new version of android it's possible that broke it." googleapis/google-api-python-client#379.

@inclement
Copy link
Member

Have you added an Android webbrowser handler, e.g. as in the documentation here?

@frankgould
Copy link
Author

frankgould commented Apr 8, 2017

Thank you inclement for your response. No, I did not see that documentation when I searched for an answer. Thank you for pointing me there. Using the example in that doc, I got the following error.

I/python ( 2524): NameError: global name 'mActivity' is not defined
I/python ( 2524): Python for android ended.

So I added the following lines from another section where I use jnius for other Android functions. I also updated my code where I had org.renpy.android.PythonActivity and the runtime said to change that to org.kivy.android.PythonActivity, which I did.

            PythonActivity = autoclass('org.kivy.android.PythonActivity')
            mActivity = PythonActivity.mActivity

That then generated the following results:

I/python ( 4524): [INFO ] [init_vars ] Basic WAN Connection SUCCESSFUL. Date/Time= {2017-04-08 07:59:07}
V/SDL ( 4524): onPause()
V/SDL ( 4524): nativePause()
I/python ( 4524): [INFO ] [---> on_pause triggered] 2017-04-08 07:59:07
I/ActivityManager( 434): START u0 {act=android.intent.action.VIEW dat=https://google.com cmp=com.android.chrome/com.google.android.apps.chrome.Main} from pid 4524
I/python ( 4524): [INFO ] [GAPI_get_credentials] startActivity
I/python ( 4524): [INFO ] [GAPI_get_credentials] webbrowser.registered
I/python ( 4524): [INFO ] [GAPI_get_credentials] token_file=drive_token
I/python ( 4524): [INFO ] [GAPI_get_credentials] flow
I/python ( 4524): [INFO ] [GAPI_get_credentials] application_name=Master-Pics Drive Access
V/SDL ( 4524): onWindowFocusChanged(): false
I/ActivityManager( 434): Start proc com.android.chrome for activity com.android.chrome/com.google.android.apps.chrome.Main: pid=4553 uid=10060 gids={50060, 3003, 1028, 1015}
I/python ( 4524): Python for android ended.

I do not know what is or is not working. Below is my code that matches the results above (logger.info). Chrome does appear on the screen but nothing else happens, like starting the Google authentication (flow).

    if system() != "Windows":
        tmp_credential = mp_frame.credential_dir + "/" + client_secret_file
        if mp_frame.First_Connection:
            Intent = autoclass('android.content.Intent')
            Uri = autoclass('android.net.Uri')
            browserIntent = Intent()
            browserIntent.setAction(Intent.ACTION_VIEW)
            browserIntent.setData(Uri.parse(mp_frame.test_connect_url))
            PythonActivity = autoclass('org.kivy.android.PythonActivity')
            mActivity = PythonActivity.mActivity
            currentActivity = cast('android.app.Activity', mActivity)
            currentActivity.startActivity(browserIntent)
            logger.info('GAPI_get_credentials:  startActivity')
            import webbrowser
            webbrowser.register('android', AndroidBrowser, None, -1)
            logger.info('GAPI_get_credentials:  webbrowser.registered')
            mp_frame.First_Connection = False
    else:
        tmp_credential = mp_frame.credential_dir + "\\" + client_secret_file
    logger.info('GAPI_get_credentials:  token_file=' + str(token_file))

    flow = client.flow_from_clientsecrets(tmp_credential, scopes)
    logger.info('GAPI_get_credentials:  flow')
    flow.user_agent = application_name
    logger.info('GAPI_get_credentials:  application_name=' + str(application_name))
    credentials = tools.run_flow(flow, store)
    logger.info('GAPI_get_credentials:  Storing credentials to: ' + credential_path)

Also note in the doc example, cast is not imported with autoclass. I did not experience a runtime error because I already had declared import from jnius import autoclass, cast.

@frankgould
Copy link
Author

Well, I'm an idiot and did not read the doc correctly. Thank you again inclement for your patience with my stupid mistakes. I followed the directions to add android to the requirements as written and the OAuth2.0 code is now working. For some reason, I figured I needed to add code and misread the doc. I really appreciate everyone's help with this project. If I ever become successful with this product I would like to spread the wealth with you guys who helped!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants