-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgphoto_quickstart.py
52 lines (46 loc) · 1.92 KB
/
gphoto_quickstart.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""
Shows basic usage of the Photos v1 API.
Creates a Photos v1 API service and prints the names and ids of the last 10 albums
the user has access to.
"""
from __future__ import print_function
import os
import pickle
import json
from googleapiclient.discovery import build
from google.auth.transport.requests import Request
from google_auth_oauthlib.flow import InstalledAppFlow
import google_auth_httplib2 # This gotta be installed for build() to work
# Setup the Photo v1 API
scopes=['https://www.googleapis.com/auth/photoslibrary',
'https://www.googleapis.com/auth/photoslibrary.sharing']
scopes=['https://www.googleapis.com/auth/photoslibrary',
'https://www.googleapis.com/auth/photoslibrary.sharing',
'https://www.googleapis.com/auth/photoslibrary.readonly.appcreateddata',
'https://www.googleapis.com/auth/photoslibrary.edit.appcreateddata']
SCOPES = ['https://www.googleapis.com/auth/photoslibrary.readonly']
SCOPES = scopes
creds = None
if(os.path.exists("token.pickle")):
with open("token.pickle", "rb") as tokenFile:
creds = pickle.load(tokenFile)
if not creds or not creds.valid:
if (creds and creds.expired and creds.refresh_token):
creds.refresh(Request())
else:
# flow = InstalledAppFlow.from_client_secrets_file('client_secret.json', SCOPES)
flow = InstalledAppFlow.from_client_secrets_file('gphoto_oauth.json', SCOPES)
creds = flow.run_local_server(port = 0)
with open("token.pickle", "wb") as tokenFile:
pickle.dump(creds, tokenFile)
service = build('photoslibrary', 'v1', credentials = creds,static_discovery=False)
# Call the Photo v1 API
results = service.albums().list(
pageSize=10, fields="nextPageToken,albums(id,title)").execute()
items = results.get('albums', [])
if not items:
print('No albums found.')
else:
print('Albums:')
for item in items:
print('{0} ({1})'.format(item['title'].encode('utf8'), item['id']))