Skip to content

Commit eede25c

Browse files
Drive-v3: Appdata_snippet, change_snippet, drive_snippet (googleworkspace#300)
* create drive.py * recover_drives.py * Drive: Appdata_snippet, change_snippet, drive_snippet Co-authored-by: anuraggoogler <[email protected]>
1 parent 27016d5 commit eede25c

7 files changed

Lines changed: 425 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""Copyright 2022 Google LLC
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
"""
15+
16+
# [START drive_fetch_appdata_folder]
17+
18+
from __future__ import print_function
19+
20+
import google.auth
21+
from googleapiclient.discovery import build
22+
from googleapiclient.errors import HttpError
23+
24+
25+
def fetch_appdata_folder():
26+
"""List out application data folder and prints folder ID.
27+
Returns : Folder ID
28+
29+
Load pre-authorized user credentials from the environment.
30+
TODO(developer) - See https://developers.google.com/identity
31+
for guides on implementing OAuth2 for the application.
32+
"""
33+
creds, _ = google.auth.default()
34+
35+
try:
36+
# call drive api client
37+
service = build('drive', 'v3', credentials=creds)
38+
39+
# pylint: disable=maybe-no-member
40+
file = service.files().get(fileId='appDataFolder',
41+
fields='id').execute()
42+
print(F'Folder ID: {file.get("id")}')
43+
44+
except HttpError as error:
45+
print(F'An error occurred: {error}')
46+
file = None
47+
48+
return file.get('id')
49+
50+
51+
if __name__ == '__main__':
52+
fetch_appdata_folder()
53+
# [END drive_fetch_appdata_folder]
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""Copyright 2022 Google LLC
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
"""
15+
16+
# [START drive_list_appdata]
17+
18+
from __future__ import print_function
19+
20+
import google.auth
21+
from googleapiclient.discovery import build
22+
from googleapiclient.errors import HttpError
23+
24+
25+
def list_appdata():
26+
"""List all files inserted in the application data folder
27+
prints file titles with Ids.
28+
Returns : List of items
29+
30+
Load pre-authorized user credentials from the environment.
31+
TODO(developer) - See https://developers.google.com/identity
32+
for guides on implementing OAuth2 for the application.
33+
"""
34+
creds, _ = google.auth.default()
35+
36+
try:
37+
# call drive api client
38+
service = build('drive', 'v3', credentials=creds)
39+
40+
# pylint: disable=maybe-no-member
41+
response = service.files().list(spaces='appDataFolder',
42+
fields='nextPageToken, files(id, '
43+
'name)', pageSize=10).execute()
44+
for file in response.get('files', []):
45+
# Process change
46+
print(F'Found file: {file.get("name")}, {file.get("id")}')
47+
48+
except HttpError as error:
49+
print(F'An error occurred: {error}')
50+
response = None
51+
52+
return response.get('files')
53+
54+
55+
if __name__ == '__main__':
56+
list_appdata()
57+
# [END drive_list_appdata]
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""Copyright 2022 Google LLC
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
"""
15+
16+
# [START drive_upload_appdata]
17+
18+
from __future__ import print_function
19+
20+
import google.auth
21+
from googleapiclient.discovery import build
22+
from googleapiclient.errors import HttpError
23+
from googleapiclient.http import MediaFileUpload
24+
25+
26+
def upload_appdata():
27+
"""Insert a file in the application data folder and prints file Id.
28+
Returns : ID's of the inserted files
29+
30+
Load pre-authorized user credentials from the environment.
31+
TODO(developer) - See https://developers.google.com/identity
32+
for guides on implementing OAuth2 for the application.
33+
"""
34+
creds, _ = google.auth.default()
35+
36+
try:
37+
# call drive api client
38+
service = build('drive', 'v3', credentials=creds)
39+
40+
# pylint: disable=maybe-no-member
41+
file_metadata = {
42+
'name': 'abc.txt',
43+
'parents': ['appDataFolder']
44+
}
45+
media = MediaFileUpload('abc.txt',
46+
mimetype='text/txt',
47+
resumable=True)
48+
file = service.files().create(body=file_metadata, media_body=media,
49+
fields='id').execute()
50+
print(F'File ID: {file.get("id")}')
51+
52+
except HttpError as error:
53+
print(F'An error occurred: {error}')
54+
file = None
55+
56+
return file.get('id')
57+
58+
59+
if __name__ == '__main__':
60+
upload_appdata()
61+
# [END drive_upload_appdata]
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""
2+
Copyright 2022 Google LLC
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
"""
13+
# [START drive_fetch_changes]
14+
15+
from __future__ import print_function
16+
17+
import google.auth
18+
from googleapiclient.discovery import build
19+
from googleapiclient.errors import HttpError
20+
21+
22+
def fetch_changes(saved_start_page_token):
23+
"""Retrieve the list of changes for the currently authenticated user.
24+
prints changed file's ID
25+
Args:
26+
saved_start_page_token : StartPageToken for the current state of the
27+
account.
28+
Returns: saved start page token.
29+
30+
Load pre-authorized user credentials from the environment.
31+
TODO(developer) - See https://developers.google.com/identity
32+
for guides on implementing OAuth2 for the application.
33+
"""
34+
creds, _ = google.auth.default()
35+
try:
36+
# create gmail api client
37+
service = build('drive', 'v3', credentials=creds)
38+
39+
# Begin with our last saved start token for this user or the
40+
# current token from getStartPageToken()
41+
page_token = saved_start_page_token
42+
# pylint: disable=maybe-no-member
43+
44+
while page_token is not None:
45+
response = service.changes().list(pageToken=page_token,
46+
spaces='drive').execute()
47+
for change in response.get('changes'):
48+
# Process change
49+
print(F'Change found for file: {change.get("fileId")}')
50+
if 'newStartPageToken' in response:
51+
# Last page, save this token for the next polling interval
52+
saved_start_page_token = response.get('newStartPageToken')
53+
page_token = response.get('nextPageToken')
54+
55+
except HttpError as error:
56+
print(F'An error occurred: {error}')
57+
saved_start_page_token = None
58+
59+
return saved_start_page_token
60+
61+
62+
if __name__ == '__main__':
63+
# saved_start_page_token is the token number
64+
fetch_changes(saved_start_page_token=209)
65+
# [END drive_fetch_changes]
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""
2+
Copyright 2022 Google LLC
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
"""
13+
# [START drive_fetch_start_page_token]
14+
15+
from __future__ import print_function
16+
17+
import google.auth
18+
from googleapiclient.discovery import build
19+
from googleapiclient.errors import HttpError
20+
21+
22+
def fetch_start_page_token():
23+
"""Retrieve page token for the current state of the account.
24+
Returns & prints : start page token
25+
26+
Load pre-authorized user credentials from the environment.
27+
TODO(developer) - See https://developers.google.com/identity
28+
for guides on implementing OAuth2 for the application.
29+
"""
30+
creds, _ = google.auth.default()
31+
32+
try:
33+
# create gmail api client
34+
service = build('drive', 'v3', credentials=creds)
35+
36+
# pylint: disable=maybe-no-member
37+
response = service.changes().getStartPageToken().execute()
38+
print(F'Start token: {response.get("startPageToken")}')
39+
40+
except HttpError as error:
41+
print(F'An error occurred: {error}')
42+
response = None
43+
44+
return response.get('startPageToken')
45+
46+
47+
if __name__ == '__main__':
48+
fetch_start_page_token()
49+
# [End drive_fetch_start_page_token]
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""
2+
Copyright 2022 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
"""
16+
# [START drive_create_drive]
17+
18+
from __future__ import print_function
19+
20+
import uuid
21+
22+
import google.auth
23+
from googleapiclient.discovery import build
24+
from googleapiclient.errors import HttpError
25+
26+
27+
def create_drive():
28+
"""Create a drive.
29+
Returns:
30+
Id of the created drive
31+
32+
Load pre-authorized user credentials from the environment.
33+
TODO(developer) - See https://developers.google.com/identity
34+
for guides on implementing OAuth2 for the application.
35+
"""
36+
creds, _ = google.auth.default()
37+
38+
try:
39+
# create gmail api client
40+
service = build('drive', 'v3', credentials=creds)
41+
42+
drive_metadata = {'name': 'Project Resources'}
43+
request_id = str(uuid.uuid4())
44+
# pylint: disable=maybe-no-member
45+
drive = service.drives().create(body=drive_metadata,
46+
requestId=request_id,
47+
fields='id').execute()
48+
print(F'Drive ID: {drive.get("id")}')
49+
50+
except HttpError as error:
51+
print(F'An error occurred: {error}')
52+
drive = None
53+
54+
return drive.get('id')
55+
56+
57+
if __name__ == '__main__':
58+
create_drive()
59+
# [END drive_create_drive]

0 commit comments

Comments
 (0)