PyDrive icon indicating copy to clipboard operation
PyDrive copied to clipboard

Deploying into Remote Machine

Open vincentwhales opened this issue 7 years ago • 7 comments

This is my use case:

I have a web portal where my clients can upload instruction files to my gdrive folder. The only OAuth token needed is mine - my web server will upload everything via my oauth token.

I am trying to understand how I can build my own custom oauth flow given that I am deploying my applications on remote machines (Heroku).

Currently, I have settings.yaml as such:

client_config_backend: settings
client_config:
  client_id: hello
  client_secret: world

save_credentials: True
save_credentials_backend: file
save_credentials_file: credentials.json

get_refresh_token: True

oauth_scope:
  - https://www.googleapis.com/auth/drive.file
  - https://www.googleapis.com/auth/drive.install 

And I am running my oauth flow using the example:

self.gauth = GoogleAuth()
self.drive = GoogleDrive(self.gauth)

When I run this on my local machine, I am seeing a webpage firing up on my browser and asking me to authorize. Once I accept, my application works as expected.

However, when I deploy this on Heroku, I am unable to visit the page fired.

I tried using the following code:

auth_url = gauth.GetAuthUrl() # Create authentication url user needs to visit
code = AskUserToVisitLinkAndGiveCode(auth_url) # Your customized authentication flow
gauth.Auth(code) # Authorize and build service from the code

The problem with this flow is that the gauth token can only be used once. And am I expected to authorize every time my webserver restarts?

How can I replicate the flow on my webserver to the flow on my remote machines? Since file is the only option for save_credentials_backend, am I expected to manually upload the credentials.json to my webserver each time I deploy?

vincentwhales avatar Oct 18 '17 18:10 vincentwhales

Hey, I think you are looking for CommandLineAuth(), see the docs.

I.e. instead of using gauth.GetAuthUrl() you can use:

gauth.CommandLineAuth()

analogous to how you would use WebServerAuth(). It will prompt you to open a website on your local machine. The link will lead to a page which provides you with an auth code, which you then need to paste into the shell from where you started your remote web server. This will then be saved onto the remote machine and if your settings.yaml file is correct (which it seems to be), you will only need to go through the process once.

Hope this helps!

RNabel avatar Oct 19 '17 11:10 RNabel

Spent days trying to figure this out but this post was helpful. Thanks! In case anyone else has this issue in the future: when creating your client ID via Google APIs make sure the type is set to native application and not web application. That in conjunction with using gauth.CommandLineAuth() and the above settings.yaml (with appropriate client id/secret) should resolve the problem.

hate5six avatar Mar 15 '18 22:03 hate5six

Spent days trying to figure this out but this post was helpful. Thanks! In case anyone else has this issue in the future: when creating your client ID via Google APIs make sure the type is set to native application and not web application. That in conjunction with using gauth.CommandLineAuth() and the above settings.yaml (with appropriate client id/secret) should resolve the problem.

Have you finished the problem?

NiceSugar avatar Mar 26 '19 10:03 NiceSugar

Is "AskUserToVisitLinkAndGiveCode" an actual function? If not, what is an authentication flow that would work with Flask? If I redirect it to the auth_url, when I return to my site (with editing the redirect_uri) it forces me to log in to my site again and everything is lost.

I'm running on a public ip, not localhost, and I cannot find any examples online that help me with an authentication flow that works (+ settings.yaml combinations).

gayatri-a-b avatar Jun 24 '19 15:06 gayatri-a-b

Dear Can anyone teach me how to use CommandLineAuth(). I am using Debian 9 on google cloud platform and I need to use CommandLineAuth(), since my linux will not fire out a browser which will walk me through the authentication process. But I really do not know how to use it. If I use CommandLineAuth(), it will ask me to input verification code. What is that? Thank you!

from pydrive.auth import GoogleAuth
gauth = GoogleAuth()
gauth.CommandLineAuth()
from pydrive.drive import GoogleDrive

pulin49 avatar Mar 09 '20 08:03 pulin49

@pulin49 CommandLineAuth requires the user to copy the URL that is printed to the console into a browser (maybe the one you're using to view this issue). So:

  1. Open a shell on the remote machine (SSH or VNC or similar)
  2. Start your application
  3. Copy the URL that it prints to the shell output
  4. Past the URL into a browser on your development machine
  5. The URL will show you a page that contains the verification code,
  6. Copy the verification code and paste it into the console that asked you for the verification code (and hit "Enter")
  7. Your application is now authenticated :)

RNabel avatar Mar 09 '20 11:03 RNabel

Dear Robin

It works! Thank you so much! I am deeply appreciated for your help. Sincerely Pu

On Mon, 9 Mar 2020 at 19:45, Robin Nabel [email protected] wrote:

@pulin49 https://github.com/pulin49 CommandLineAuth requires the user to copy the URL that is printed to the console into a browser (maybe the one you're using to view this issue). So:

  1. Open a shell on the remote machine (SSH or VNC or similar)
  2. Start your application
  3. Copy the URL that it prints to the shell output
  4. Past the URL into a browser on your development machine
  5. The URL will show you a page that contains the verification code,
  6. Copy the verification code and paste it into the console that asked you for the verification code
  7. Your application is now authenticated :)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/gsuitedevs/PyDrive/issues/115?email_source=notifications&email_token=AMHPZJT3IM6JHMDB7FVZVODRGTJGNA5CNFSM4D7YR6X2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEOGYYCA#issuecomment-596478984, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMHPZJVSVC7LVCA7DN5NRRTRGTJGNANCNFSM4D7YR6XQ .

pulin49 avatar Mar 10 '20 08:03 pulin49