3 ms·
FYI the Python quickstart.py is broken on Ubuntu 18.04 (tried both python2 and python3). I've notified the eng in the video, and sent her here. By definition,
by asah 8y ago
FYI the Python quickstart.py is broken on Ubuntu 18.04 (tried both python2 and python3). I've notified the eng in the video, and sent her here.
By definition, credentials.json doesn't exist yet and this throws an error. Transcript below.
creds = None
if os.path.exists('token.pickle'):
...
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
...
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server()
https://developers.google.com/docs/api/quickstart/python https://developers.google.com/docs/api/quickstart/python
$ sudo apt install python3-pip
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
grub-pc-bin libpython-all-dev libpython-dev libpython2.7 libpython2.7-dev python-all python-all-dev python-asn1crypto python-cffi-backend python-crcmod python-crypto python-cryptography
python-dbus python-dev python-enum34 python-gi python-idna python-ipaddress python-keyring python-keyrings.alt python-secretstorage python-six python-wheel python-xdg python2.7-dev
Use 'sudo apt autoremove' to remove them.
The following NEW packages will be installed:
python3-pip
0 upgraded, 1 newly installed, 0 to remove and 11 not upgraded.
Need to get 114 kB of archives.
After this operation, 599 kB of additional disk space will be used.
Get:1 http://us-east4-c.gce.clouds.archive.ubuntu.com/ubuntu http://us-east4-c.gce.clouds.archive.ubuntu.com/ubuntu bionic-updates/universe amd64 python3-pip all 9.0.1-2.3~ubuntu1 [114 kB]
Fetched 114 kB in 1s (175 kB/s)
Selecting previously unselected package python3-pip.
(Reading database ... 128010 files and directories currently installed.)
Preparing to unpack .../python3-pip_9.0.1-2.3~ubuntu1_all.deb ...
Unpacking python3-pip (9.0.1-2.3~ubuntu1) ...
Setting up python3-pip (9.0.1-2.3~ubuntu1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
$ pip3 install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
Collecting google-api-python-client
Downloading https://files.pythonhosted.org/packages/55/e9/e8fb2e3a031cb69b9524b80a92b126665d9a17421700a219555e3233ab6a/google_api_python_client-1.7.8-py3-none-any.whl https://files.pythonhosted.org/packages/55/e9/e8fb2e3a031cb6... (56kB)
100% || 61kB 3.1MB/s
Collecting google-auth-httplib2
Using cached https://files.pythonhosted.org/packages/33/49/c814d6d438b823441552198f096fcd0377fd6c88714dbed34f1d3c8c4389/google_auth_httplib2-0.0.3-py2.py3-none-any.whl https://files.pythonhosted.org/packages/33/49/c814d6d438b823...
Collecting google-auth-oauthlib
Using cached https://files.pythonhosted.org/packages/46/df/d1f94ee2cffe5a83721f262efe51f3b2dcdd3b616caf007b8490e824c550/google_auth_oauthlib-0.2.0-py2.py3-none-any.whl https://files.pythonhosted.org/packages/46/df/d1f94ee2cffe5a...
Collecting six<2dev,>=1.6.1 (from google-api-python-client)
Using cached https://files.pythonhosted.org/packages/73/fb/00a976f728d0d1fecfe898238ce23f502a721c0ac0ecfedb80e0d88c64e9/six-1.12.0-py2.py3-none-any.whl https://files.pythonhosted.org/packages/73/fb/00a976f728d0d1...
Collecting httplib2<1dev,>=0.9.2 (from google-api-python-client)
Using cached https://files.pythonhosted.org/packages/ce/ed/803905d670b52fa0edfdd135337e545b4496c2ab3a222f1449b7256eb99f/httplib2-0.12.0.tar.gz https://files.pythonhosted.org/packages/ce/ed/803905d670b52f...
Collecting uritemplate<4dev,>=3.0.0 (from google-api-python-client)
Downloading https://files.pythonhosted.org/packages/e5/7d/9d5a640c4f8bf2c8b1afc015e9a9d8de32e13c9016dcc4b0ec03481fb396/uritemplate-3.0.0-py2.py3-none-any.whl https://files.pythonhosted.org/packages/e5/7d/9d5a640c4f8bf2...
Collecting google-auth>=1.4.1 (from google-api-python-client)
Using cached https://files.pythonhosted.org/packages/4e/85/71b2dfbf5b4241cd031cc333ed71f90a271074a97cb2c517bb65f07a1a90/google_auth-1.6.2-py2.py3-none-any.whl https://files.pythonhosted.org/packages/4e/85/71b2dfbf5b4241...
Collecting requests-oauthlib>=0.7.0 (from google-auth-oauthlib)
Using cached https://files.pythonhosted.org/packages/c2/e2/9fd03d55ffb70fe51f587f20bcf407a6927eb121de86928b34d162f0b1ac/requests_oauthlib-1.2.0-py2.py3-none-any.whl https://files.pythonhosted.org/packages/c2/e2/9fd03d55ffb70f...
Collecting cachetools>=2.0.0 (from google-auth>=1.4.1->google-api-python-client)
Using cached https://files.pythonhosted.org/packages/39/2b/d87fc2369242bd743883232c463f28205902b8579cb68dcf5b11eee1652f/cachetools-3.1.0-py2.py3-none-any.whl https://files.pythonhosted.org/packages/39/2b/d87fc2369242bd...
Collecting pyasn1-modules>=0.2.1 (from google-auth>=1.4.1->google-api-python-client)
Using cached https://files.pythonhosted.org/packages/da/98/8ddd9fa4d84065926832bcf2255a2b69f1d03330aa4d1c49cc7317ac888e/pyasn1_modules-0.2.4-py2.py3-none-any.whl https://files.pythonhosted.org/packages/da/98/8ddd9fa4d84065...
Collecting rsa>=3.1.4 (from google-auth>=1.4.1->google-api-python-client)
Using cached https://files.pythonhosted.org/packages/02/e5/38518af393f7c214357079ce67a317307936896e961e35450b70fad2a9cf/rsa-4.0-py2.py3-none-any.whl https://files.pythonhosted.org/packages/02/e5/38518af393f7c2...
Collecting oauthlib>=3.0.0 (from requests-oauthlib>=0.7.0->google-auth-oauthlib)
Using cached https://files.pythonhosted.org/packages/16/95/699466b05b72b94a41f662dc9edf87fda4289e3602ecd42d27fcaddf7b56/oauthlib-3.0.1-py2.py3-none-any.whl https://files.pythonhosted.org/packages/16/95/699466b05b72b9...
Collecting requests>=2.0.0 (from requests-oauthlib>=0.7.0->google-auth-oauthlib)
Using cached https://files.pythonhosted.org/packages/7d/e3/20f3d364d6c8e5d2353c72a67778eb189176f08e873c9900e10c0287b84b/requests-2.21.0-py2.py3-none-any.whl https://files.pythonhosted.org/packages/7d/e3/20f3d364d6c8e5...
Collecting pyasn1<0.5.0,>=0.4.1 (from pyasn1-modules>=0.2.1->google-auth>=1.4.1->google-api-python-client)
Using cached https://files.pythonhosted.org/packages/7b/7c/c9386b82a25115cccf1903441bba3cbadcfae7b678a20167347fa8ded34c/pyasn1-0.4.5-py2.py3-none-any.whl https://files.pythonhosted.org/packages/7b/7c/c9386b82a25115...
Collecting urllib3<1.25,>=1.21.1 (from requests>=2.0.0->requests-oauthlib>=0.7.0->google-auth-oauthlib)
Using cached https://files.pythonhosted.org/packages/62/00/ee1d7de624db8ba7090d1226aebefab96a2c71cd5cfa7629d6ad3f61b79e/urllib3-1.24.1-py2.py3-none-any.whl https://files.pythonhosted.org/packages/62/00/ee1d7de624db8b...
Collecting certifi>=2017.4.17 (from requests>=2.0.0->requests-oauthlib>=0.7.0->google-auth-oauthlib)
Using cached https://files.pythonhosted.org/packages/9f/e0/accfc1b56b57e9750eba272e24c4dddeac86852c2bebd1236674d7887e8a/certifi-2018.11.29-py2.py3-none-any.whl https://files.pythonhosted.org/packages/9f/e0/accfc1b56b57e9...
Collecting chardet<3.1.0,>=3.0.2 (from requests>=2.0.0->requests-oauthlib>=0.7.0->google-auth-oauthlib)
Using cached https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e42...
Collecting idna<2.9,>=2.5 (from requests>=2.0.0->requests-oauthlib>=0.7.0->google-auth-oauthlib)
Using cached https://files.pythonhosted.org/packages/14/2c/cd551d81dbe15200be1cf41cd03869a46fe7226e7450af7a6545bfc474c9/idna-2.8-py2.py3-none-any.whl https://files.pythonhosted.org/packages/14/2c/cd551d81dbe152...
Building wheels for collected packages: httplib2
Running setup.py bdist_wheel for httplib2 ... done
Stored in directory: /home/asah/.cache/pip/wheels/6d/41/4b/2b369d6e2b7eaebcdd423516d3fb659c7658c16a2be8fd04ec
Successfully built httplib2
Installing collected packages: six, httplib2, cachetools, pyasn1, pyasn1-modules, rsa, google-auth, google-auth-httplib2, uritemplate, google-api-python-client, oauthlib, urllib3, certifi, chardet, idna, requests, requests-oauthlib, google-auth-oauthlib
Successfully installed cachetools-3.1.0 certifi-2018.11.29 chardet-3.0.4 google-api-python-client-1.7.8 google-auth-1.6.2 google-auth-httplib2-0.0.3 google-auth-oauthlib-0.2.0 httplib2-0.12.0 idna-2.8 oauthlib-3.0.1 pyasn1-0.4.5 pyasn1-modules-0.2.4 requests-2.21.0 requests-oauthlib-1.2.0 rsa-4.0 six-1.12.0 uritemplate-3.0.0 urllib3-1.24.1
$ python quickstart.py
InstalledAppFlow.from_client_secrets_file()...
Traceback (most recent call last):
File "quickstart.py", line 47, in <module>
main()
File "quickstart.py", line 32, in main
'credentials.json', SCOPES)
File "/home/asah/.local/lib/python2.7/site-packages/google_auth_oauthlib/flow.py", line 171, in from_client_secrets_file
with open(client_secrets_file, 'r') as json_file:
IOError: [Errno 2] No such file or directory: 'credentials.json'
$ cat quickstart.py
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/documents.readonly' https://www.googleapis.com/auth/documents.readonly']
# The ID of a sample document.
DOCUMENT_ID = '195j9eDD3ccgjQRttHhJPymLJUCOUjs-jmwTrekvdjFE'
def main():
"""Shows basic usage of the Docs API.
Prints the title of a sample document.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
print("InstalledAppFlow.from_client_secrets_file()...")
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server()
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('docs', 'v1', credentials=creds)
# Retrieve the documents contents from the Docs service.
document = service.documents().get(documentId=DOCUMENT_ID).execute()
print('The title of the document is: {}'.format(document.get('title')))
if __name__ == '__main__':
main()
- detaro 8y agoplease use a github gist or something and don't dump a wall of output here
- sjcoles 8y agoYou are missing a credentials.json file. This is not the code's fault but your own. Read the document you linked: https://developers.google.com/docs/api/quickstart/python https://developers.google.com/docs/api/quickstart/python Read directly above and below the giant blue "ENABLE THE GOOGLE DOCS API" button.
- asah 8y agothanks - I was expecting the code itself to do the credentials dance.