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
    23605
    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.
    • 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