omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Has somebody already used PyDrive to access Google Drive files?

    Pythonista
    6
    22
    23535
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • JonB
      JonB last edited by JonB

      I had trouble setting up the oath client ID from the ipad, but i was able to set it up on my pc.

      Then, i downloaded the file to my ipad, used open in pythonista / Install File (hey look at that new unannounced feature i the beta!?), then renamed Unknown to client_secret.json in Documents.

      Then the auth example basically worked. i had to monkeypatch webbrowser open:

      from pydrive.auth import GoogleAuth
      
      gauth = GoogleAuth()
      # Create local webserver and auto handles authentication.
      import webbrowser
      if not hasattr(webbrowser,'_open'):
      	webbrowser._open=webbrowser.open
      def wbopen(url, *args,**kwargs):
      	return webbrowser._open(url)
      webbrowser.open=wbopen
      gauth.LocalWebserverAuth()
      

      Note also the webbrowser might pop up to another tab in the back. The first time I did not notice the redirect url needs the ending slash, so had to update the client id settings on the developer page. after this worked, I was able to then create a file and list my drive.

      cvp 1 Reply Last reply Reply Quote 0
      • cvp
        cvp last edited by

        Thanks for spending time...
        I'll try but my Mac (no more PC since 5 years) is entirely locked by the new Mac OS update of this night...😭

        My dream is to perform all my daily tasks (not a lot because I'm retired 😇) on my iPad, thanks to Pythonista of course, and to leave my Mac.
        My Mac is synchronized with Google Drive and I want to develop this synchronization in Pythonista, assuming that all my personal files could be in Pythonista local storage.
        I also try to use iCloud Drive as 2nd cloud (both are not expensive for 100 GB, and unfortunately is Dropbox expensive if you don't need their 1 TB). Workflow app allows some kind of access to their folder in iCloud Drive.

        1 Reply Last reply Reply Quote 0
        • cvp
          cvp @JonB last edited by cvp

          @JonB
          My Mac is still hanging.
          I've tried on my iPad by asking in safari to open with the Desktop version of the Google site and the creation of my user has been successful.
          After that, I

          • downloaded the unknown file
          • renamed into client_secrets.json
          • Created a quickstart.py which has a problem on webbrowser.open
          • Copied your code (who else could solve that?)
          • run it and it opens a tab with an URL. If I tap it, I receive an error from Google, if I copy it and open it with a safari, all is ok.
          • try the code of upload, it is ok but it seems to request I always open the URL to see the file really uploaded in my Google Drive. is that true?

          Anyway, thanks a lot for your help. Without you, I would never think to try on a PC/Mac, and thus on the desktop version of the site, and without you, the original code for webbrowser open would never be ok.

          The script stays running until I open the URL with Safari and then the file is really uploaded.
          That's ennoying, I hoped no human interaction. Still to be analyzed but I (with you) am in the right direction.

          1 Reply Last reply Reply Quote 0
          • cvp
            cvp last edited by cvp

            followed
            And all is automatic....
            Marvelous
            Thus, from now, we could access Google Drive for saving or importing files....

            1 Reply Last reply Reply Quote 1
            • zhengyiluo
              zhengyiluo last edited by

              This is truly amazing!

              Can the source be shared?

              cvp 1 Reply Last reply Reply Quote 0
              • cvp
                cvp @zhengyiluo last edited by

                @zhengyiluo
                No more used since 6 months, just retried now and it still functions 😅
                Of course, you need to install PyDrive, and follow all steps like explained in the forum and read these url's
                Good luck
                After that, a very easy sample:

                # see https://pythonhosted.org/PyDrive/quickstart.html
                # see https://pythonhosted.org/PyDrive/oauth.html#sample-settings-yaml
                # see https://github.com/googledrive/PyDrive
                
                from pydrive.auth import GoogleAuth
                from pydrive.drive import GoogleDrive
                import webbrowser
                
                # Create local webserver and auto handles authentication.
                # Standard webbrowser gave an error, JonB found the solution
                if not hasattr(webbrowser,'_open'):
                	webbrowser._open=webbrowser.open
                def wbopen(url, *args,**kwargs):
                	return webbrowser._open(url)
                webbrowser.open=wbopen
                
                gauth = GoogleAuth()
                gauth.LocalWebserverAuth()
                
                # upload 
                drive = GoogleDrive(gauth)
                
                file1 = drive.CreateFile({'title': 'Hello.txt'})  # Create GoogleDriveFile instance with title 'Hello.txt'.
                file1.SetContentString('Hello World!') # Set content of the file from given string.
                file1.Upload()
                
                file1 = drive.CreateFile({'title': 'quickstart.py'})  # Create GoogleDriveFile instance with title ...
                file1.SetContentFile('quickstart.py') # Set content of the file from given file
                file1.Upload()
                
                zhengyiluo 1 Reply Last reply Reply Quote 0
                • zhengyiluo
                  zhengyiluo @cvp last edited by

                  @cvp Fantastic! Thanks a lot!

                  1 Reply Last reply Reply Quote 0
                  • FarmerPaco
                    FarmerPaco last edited by

                    Hi @JonB and @cvp
                    Just completed this install.

                    Run with python3
                    Returns syntax error on line 202 of discovery.py

                    Traceback reads:

                    Traceback (most recent call last):
                    File "/private/var/mobile/Containers/Shared/AppGroup/BDD90EB4-7B3E-4A50-A939-642531B58BFE/Pythonista3/Documents/pyconnect.py", line 1, in <module>
                    from pydrive.auth import GoogleAuth
                    File "/private/var/mobile/Containers/Shared/AppGroup/BDD90EB4-7B3E-4A50-A939-642531B58BFE/Pythonista3/Documents/site-packages-3/pydrive/auth.py", line 7, in <module>
                    from apiclient.discovery import build
                    File "/private/var/mobile/Containers/Shared/AppGroup/BDD90EB4-7B3E-4A50-A939-642531B58BFE/Pythonista3/Documents/site-packages-3/apiclient/discovery.py", line 202
                    except ValueError, e:
                    ^
                    SyntaxError: invalid syntax

                    Does PyDrive still work with pythonista?

                    (With python2.7 I also get a name error: cannot import name util )

                    Any pointers or help would be much appreciated.
                    Thanks in advance

                    cvp 1 Reply Last reply Reply Quote 0
                    • JonB
                      JonB last edited by

                      looks like you installed it using launch_stash in python 3 mode. run stash in py2 and try again.

                      FarmerPaco 1 Reply Last reply Reply Quote 0
                      • cvp
                        cvp @FarmerPaco last edited by

                        @FarmerPaco I use it every day, in Python 3 (I backup my Pythonista files on Google Drive).

                        1 Reply Last reply Reply Quote 0
                        • shinyformica
                          shinyformica last edited by

                          I am also using it right now in pythonista with python2.7, so I can verify it does work.

                          My setup is a little strange, since I needed to keep the entire install of pydrive and its dependencies isolated to the same directory structure as my pythonista project itself (so, nothing it relies on which isn't part of the standard pythonista install is placed in the site-packages areas). Which means most of the issues I had getting it to work probably don't apply to your situation.

                          I did run into problems with the webbrowser module way of trying to authenticate via oauth2 that pydrive uses by default - I was unable to get the method above by @cvp from a couple years back to work. From what I could gather, the issue was that as of 2017 Apple/Google no longer allow authentication from a web-view: https://developers.googleblog.com/2016/08/modernizing-oauth-interactions-in-native-apps.html

                          cvp 1 Reply Last reply Reply Quote 0
                          • cvp
                            cvp @shinyformica last edited by

                            @shinyformica My pydrive folder is in site-packages.
                            I agree that I didn't retry a new authentication 😅

                            1 Reply Last reply Reply Quote 0
                            • JonB
                              JonB last edited by

                              There was a thread that came up again recently about using a webview with custom user agent. Search for pythonista webview useragent. Alternatively iirc you can download creds from the Google app dashboard

                              1 Reply Last reply Reply Quote 1
                              • shinyformica
                                shinyformica last edited by

                                For reference, @cvp @JonB, the way I got authentication to work ended up being to use the brilliant WKWebView by @mikael, another pythonista master, which uses the more modern webkit library, and allows the user-agent to be set to whatever you want. That, combined with a bit of custom code for the authentication process worked fine.

                                @FarmerPaco, if you find yourself stuck trying to get authentication to work even after you get past the pydrive module issues, I'll try to whittle down the stuff I did to a basic example.

                                1 Reply Last reply Reply Quote 0
                                • FarmerPaco
                                  FarmerPaco @JonB last edited by

                                  @JonB thanks again for coming to the rescue (I remember you helped me with turtle2).

                                  Let me indicate what I did:

                                  1.installed PyDrive with Stash running in 2.7 which installed to Site packages 2. I realized this when I tried to run the following code in python 3.6

                                  from pydrive.auth import GoogleAuth
                                  from pydrive.drive import GoogleDrive
                                  import webbrowser
                                  
                                  # Create local webserver and auto handles authentication.
                                  # Standard webbrowser gave an error, JonB found the solution
                                  if not hasattr(webbrowser,'_open'):
                                      webbrowser._open=webbrowser.open
                                  def wbopen(url, *args,**kwargs):
                                      return webbrowser._open(url)
                                  webbrowser.open=wbopen
                                  
                                  gauth = GoogleAuth()
                                  gauth.LocalWebserverAuth()
                                  
                                  # upload 
                                  drive = GoogleDrive(gauth)
                                  
                                  file1 = drive.CreateFile({'title': 'Hello.txt'})  # Create GoogleDriveFile instance with title 'Hello.txt'.
                                  file1.SetContentString('Hello World!') # Set content of the file from given string.
                                  file1.Upload()
                                  
                                  file1 = drive.CreateFile({'title': 'pyconnect.py'})  # Create GoogleDriveFile instance with title ...
                                  file1.SetContentFile('pyconnect.py') # Set content of the file from given file
                                  file1.Upload()
                                  

                                  and received an import error.

                                  1. Installed PyDrive again in stash running python 3.6. Now I have the PyDrive module in both site packages.

                                  Tried to run code and received an error in line 202 of the discovery.py dependency.
                                  I need to admit that I have not done a pip install of pyasn1 because the printout on the PyDrive install stated that the dependency was already there.

                                  I appreciate your help.
                                  Since I have PyDrive installed in both site packages. My questions are:

                                  1.Do I still need to reinstall pyasn1 like you did originally?

                                  1. @cvp is running PyDrive in python3. Can PyDrive by installed using stash running 3.6? I plan to use PyDrive in python3 exclusively. I use this for education. (Full disclosure: I am a lousy teacher and an even worse student).

                                  2. Why would I get a syntax error in discovery.py (which I am not even sure what it is.) Can I reinstall this modul again without errors from pip install?

                                  @shinyformica thanks for the offer for help. I will probably take you on it was I reach the authentication stage. I have already complete my google console step, downloaded the unknown file, remained and move to my current directory.

                                  Thank you everyone for taking time from your surely busy schedules to help me.
                                  Thank yo in advance for any further guidance.
                                  I want to install PyDrive on pythonista on my students so they can automate checking their work.

                                  Best,
                                  Justin

                                  1 Reply Last reply Reply Quote 0
                                  • FarmerPaco
                                    FarmerPaco last edited by ccc

                                    This is a repeat of a follow-up in a different thread but since it address the matter of access and manipulation of Google Drive files I am going to cross-post it here:

                                    Just to follow up on this matter. After struggling with PyDrive in pythonista for a while with resulting errors, I opted to just go ahead and try the Google Drive API.

                                    There is a super simple tutorial in codelabs:
                                    https://codelabs.developers.google.com/codelabs/gsuite-apis-intro/#0

                                    Everything installs nicely and runs well. It even lets you complete the authentication in a separate browser if need be. I did the authentication on my raspberry pi and scped the secrets.jason and since then it works perfectly.

                                    Just in case anyone wants to access and G suite files, you should know that pip in stash running in python3 nicely installs google-api-python-client and oauth2client in the site-packages-3 (which is all you need for Google Drive API).

                                    cvp 1 Reply Last reply Reply Quote 1
                                    • cvp
                                      cvp @FarmerPaco last edited by

                                      @FarmerPaco Thanks. good to know

                                      1 Reply Last reply Reply Quote 1
                                      • First post
                                        Last post
                                      Powered by NodeBB Forums | Contributors