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.
    • cvp
      cvp last edited by

      Has somebody already used PyDrive to access Google Drive files?

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

        I don't think that is needed if you simply want to download them into Pythonista? What is your need?

        https://forum.omz-software.com/topic/3499/simple-file-download/12

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

          Thanks for your answer but I really want to update my Google Drive storage, like I do in my dropbox: add/delete/update a file/folder, thus I need to have an authorized access.

          I never understand how iOS apps like Documents 5 or FileBrowser are able to access the Google Drive storage with these functionalities.

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

            i got pydrive installed as follows:

            in stash:

            pip install pydrive
            

            then

            [~/Documents]$ mkdir pyasn1
            [~/Documents]$ pip download pyasn1 -d pyasn1
            Querying PyPI ... 
            Downloading package ...
            Opening: https://pypi.python.org/packages/f7/83/377e3dd2e95f9020dbd0dfd3c47aaa7deebe3c68d3857a4e51917146ae8b/pyasn1-0.1.9.tar.gz
            Save as: /private/var/mobile/Containers/Data/Application/B1367204-DDA4-4751-8EA6-15CD701EE554/tmp/pyasn1-0.1.9.tar.gz (75947 bytes)
                 75947  [100.00%]
            [~/Documents]$ cd pyasn1
            [pyasn1]$ tar -xzf pyasn1-0.1.9.tar.gz 
            Archive extracted.
            [pyasn1]$ ll
            pyasn1-0.1.9 (544.0B) 2015-09-28 05:07:47
            pyasn1-0.1.9.tar.gz (74.2K) 2016-12-14 23:16:09
            [pyasn1]$ cd pyasn1-0.1.9
            [pyasn1-0.1.9]$ cp pyasn1/ ~/Documents/site-packages/
            

            have not read through the api yet or tried any examples.

            ideally someone would create an fsi for the mc command in stash, which would let you treat it as a "mounted" drive.

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

              Thanks for your answer.
              I had already installed via stash/pip but I wanted to know if somebody already used it because I am not able to define a Google authorized access via the Google console API like explained here

              1 Reply Last reply Reply Quote 0
              • 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
                                            • First post
                                              Last post
                                            Powered by NodeBB Forums | Contributors