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

Add example of the service account auth #148

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions docs/quickstart.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
Quickstart
=============================

Authentication
--------------
Authentication via OAuth2.0
---------------------------

Drive API requires OAuth2.0 for authentication. *PyDrive2* makes your life much easier by handling complex authentication steps for you.

1. Go to `APIs Console`_ and make your own project.
Expand All @@ -29,11 +30,47 @@ Create *quickstart.py* file and copy and paste the following code.
gauth = GoogleAuth()
gauth.LocalWebserverAuth() # Creates local webserver and auto handles authentication.

Run this code with *python quickstart.py* and you will see a web browser asking you for authentication. Click *Accept* and you are done with authentication. For more details, take a look at documentation: `OAuth made easy`_
Run this code with *python quickstart.py* and you will see a web browser asking you for authentication.
Click *Accept* and you are done with authentication. For more details, take a look at documentation: `OAuth made easy`_

.. _`APIs Console`: https://console.developers.google.com/iam-admin/projects
.. _`OAuth made easy`: ./oauth.html

Authentication via service accounts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's technically also part of the OAuth 2.0 - https://developers.google.com/identity/protocols/oauth2/service-account

-----------------------------------

Alternatively to OAuth2.0 client authentication *PyDrive2* can use Google Service Accounts for authentication.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make Google Service Accounts a link to general docs about service accounts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type of authentication is useful when developing a servers side application which doesn't interact with endusers.

1. create a service account in the Google Cloud Console
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we provide a link to Google docs here?

2. download a json files with account credentials and name it 'client_secrets.json'.
3. run following code:

.. code-block:: python

from pydrive2.auth import GoogleAuth
from pydrive2.drive import GoogleDrive
from oauth2client.service_account import ServiceAccountCredentials

scope = ["https://www.googleapis.com/auth/drive"]
gauth = GoogleAuth()
gauth.auth_method = 'service'
gauth.credentials = ServiceAccountCredentials.from_json_keyfile_name('client_secrets.json', scope)
drive = GoogleDrive(gauth)

about = drive.GetAbout()

print('Current user name:{}'.format(about['name']))
print('Root folder ID:{}'.format(about['rootFolderId']))
print('Total quota (bytes):{}'.format(about['quotaBytesTotal']))
print('Used quota (bytes):{}'.format(about['quotaBytesUsed']))


file_list = drive.ListFile().GetList()
for file1 in file_list:
print('title: %s, id: %s' % (file1['title'], file1['id']))


Creating and Updating Files
---------------------------

Expand Down