├── .github ├── CODEOWNERS ├── ISSUE_TEMPLATE.md ├── dependabot.yml ├── linters │ ├── .htmlhintrc │ ├── .yaml-lint.yml │ └── sun_checks.xml ├── pull_request_template.md ├── scripts │ ├── authorize.sh │ ├── lint.sh │ └── test.sh ├── snippet-bot.yml ├── sync-repo-settings.yaml └── workflows │ ├── automation.yml │ ├── lint.yml │ └── test.yml ├── .gitignore ├── .pylintrc ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── SECURITY.md ├── admin_sdk ├── directory │ ├── README.md │ ├── quickstart.py │ └── requirements.txt ├── reports │ ├── README.md │ ├── quickstart.py │ └── requirements.txt └── reseller │ ├── README.md │ ├── quickstart.py │ └── requirements.txt ├── apps_script ├── execute │ └── execute.py └── quickstart │ ├── README.md │ ├── quickstart.py │ └── requirements.txt ├── calendar └── quickstart │ ├── README.md │ ├── quickstart.py │ └── requirements.txt ├── chat ├── client-libraries │ └── cloud │ │ ├── README.md │ │ ├── authentication_utils.py │ │ ├── create_membership_user_cred.py │ │ ├── create_membership_user_cred_for_app.py │ │ ├── create_membership_user_cred_for_group.py │ │ ├── create_message_app_cred.py │ │ ├── create_message_user_cred.py │ │ ├── create_message_user_cred_at_mention.py │ │ ├── create_message_user_cred_message_id.py │ │ ├── create_message_user_cred_request_id.py │ │ ├── create_message_user_cred_thread_key.py │ │ ├── create_message_user_cred_thread_name.py │ │ ├── create_space_user_cred.py │ │ ├── delete_message_app_cred.py │ │ ├── delete_message_user_cred.py │ │ ├── get_membership_app_cred.py │ │ ├── get_membership_user_cred.py │ │ ├── get_message_app_cred.py │ │ ├── get_message_user_cred.py │ │ ├── get_space_app_cred.py │ │ ├── get_space_user_cred.py │ │ ├── list_memberships_app_cred.py │ │ ├── list_memberships_user_cred.py │ │ ├── list_messages_user_cred.py │ │ ├── list_spaces_app_cred.py │ │ ├── list_spaces_user_cred.py │ │ ├── requirements.txt │ │ ├── set_up_space_user_cred.py │ │ ├── update_message_app_cred.py │ │ ├── update_message_user_cred.py │ │ └── update_space_user_cred.py └── quickstart │ ├── README.md │ ├── quickstart.py │ └── requirements.txt ├── classroom ├── quickstart │ ├── README.md │ ├── quickstart.py │ └── requirements.txt └── snippets │ ├── base_test.py │ ├── classroom_add_alias_existing.py │ ├── classroom_add_alias_new.py │ ├── classroom_add_attachment.py │ ├── classroom_add_student.py │ ├── classroom_add_teacher.py │ ├── classroom_all_submissions.py │ ├── classroom_create_course.py │ ├── classroom_create_coursework.py │ ├── classroom_get_course.py │ ├── classroom_invite_guardian.py │ ├── classroom_list_course_aliases.py │ ├── classroom_list_courses.py │ ├── classroom_list_student_submissions.py │ ├── classroom_list_submissions.py │ ├── classroom_patch_course.py │ ├── classroom_update_course.py │ ├── test_classroom_create_course.py │ ├── test_classroom_get_course.py │ ├── test_classroom_list_course_aliases.py │ ├── test_classroom_list_courses.py │ ├── test_classroom_patch_course.py │ └── test_classroom_update_course.py ├── docs ├── mail-merge │ ├── README.md │ ├── docs_mail_merge.py │ ├── docs_mail_merge_test.py │ └── requirements.txt ├── output-json │ ├── output-json.py │ └── output_json.py └── quickstart │ ├── README.md │ ├── quickstart.py │ └── requirements.txt ├── drive ├── activity-v2 │ ├── quickstart.py │ └── requirements.txt ├── driveapp │ ├── README.md │ ├── client_secrets.json │ ├── document.txt │ └── main.py ├── quickstart │ ├── README.md │ ├── quickstart.py │ └── requirements.txt └── snippets │ ├── drive-v2 │ ├── app data snippet │ │ ├── fetch_appdata_folder.py │ │ ├── list_appdata.py │ │ ├── test_fetch_appdata_folder.py │ │ ├── test_list_appdata.py │ │ ├── test_upload_appdata.py │ │ └── upload_app_data.py │ ├── change snippet │ │ ├── fetch_changes.py │ │ ├── fetch_start_page_token.py │ │ ├── test_fetch_changes.py │ │ └── test_fetch_start_page_token.py │ ├── drive_snippet │ │ ├── create_drive.py │ │ └── recover_drives.py │ ├── file snippet │ │ ├── create_folder.py │ │ ├── create_shortcut.py │ │ ├── download_file.py │ │ ├── export_pdf.py │ │ ├── move_file_to_folder.py │ │ ├── search_file.py │ │ ├── share_file.py │ │ ├── test_create_folder.py │ │ ├── test_create_shortcut.py │ │ ├── test_download_file.py │ │ ├── test_export_pdf.py │ │ ├── test_move_file_to_folder.py │ │ ├── test_search_file.py │ │ ├── test_share_file.py │ │ ├── test_touch_file.py │ │ ├── test_upload_basic.py │ │ ├── test_upload_revision.py │ │ ├── test_upload_to_folder.py │ │ ├── test_upload_with_conversion.py │ │ ├── touch_file.py │ │ ├── upload_basic.py │ │ ├── upload_revision.py │ │ ├── upload_to_folder.py │ │ └── upload_with_conversion.py │ └── team_drive_snippets │ │ ├── create_team_drive.py │ │ └── recover_team_drives.py │ └── drive-v3 │ ├── app_data_snippet │ ├── fetch_appdata_folder.py │ ├── list_appdata.py │ ├── test_fetch_appdata_folder.py │ ├── test_list_appdata.py │ ├── test_upload_appdata.py │ └── upload_appdata.py │ ├── change_snippet │ ├── fetch_changes.py │ ├── fetch_start_page_token.py │ ├── test_fetch_changes.py │ └── test_fetch_start_page_token.py │ ├── drive_snippet │ ├── create_drive.py │ └── recover_drives.py │ ├── file_snippet │ ├── create_folder.py │ ├── create_shortcut.py │ ├── download_file.py │ ├── export_pdf.py │ ├── move_file_to_folder.py │ ├── search_file.py │ ├── share_file.py │ ├── test_create_folder.py │ ├── test_create_shortcut.py │ ├── test_download_file.py │ ├── test_export_pdf.py │ ├── test_move_file_to_folder.py │ ├── test_search_file.py │ ├── test_share_file.py │ ├── test_touch_file.py │ ├── test_upload_basic.py │ ├── test_upload_revision.py │ ├── test_upload_to_folder.py │ ├── test_upload_with_conversion.py │ ├── touch_file.py │ ├── upload_basic.py │ ├── upload_revision.py │ ├── upload_to_folder.py │ └── upload_with_conversion.py │ └── team_drive_snippets │ ├── create_team_drive.py │ └── recover_team_drives.py ├── events └── next18 │ ├── README.md │ ├── customer_data_service.py │ ├── customer_spreadsheet_reader.py │ ├── presentation_reader.py │ ├── presentation_writer.py │ ├── qbr_tool.py │ └── spreadsheet_writer.py ├── forms ├── quickstart │ ├── README.md │ ├── quickstart.py │ └── requirements.txt └── snippets │ ├── add_item.py │ ├── add_responder.py │ ├── anyone_with_link_responder.py │ ├── convert_form.py │ ├── create_form.py │ ├── create_watch.py │ ├── delete_watch.py │ ├── duplicate_form.py │ ├── get_responders.py │ ├── list_watches.py │ ├── publish_form.py │ ├── remove_responder.py │ ├── renew_watch.py │ ├── retrieve_all_responses.py │ ├── retrieve_contents.py │ ├── retrieve_single_response.py │ ├── stop_accepting_responses.py │ ├── supports_publishing.py │ ├── unpublish_form.py │ └── update_form.py ├── gmail ├── quickstart │ ├── README.md │ ├── quickstart.py │ └── requirements.txt └── snippet │ ├── base_test.py │ ├── files │ ├── cert.p12 │ ├── certs.csv │ └── photo.jpg │ ├── list_messages.py │ ├── requirements.txt │ ├── send mail │ ├── create_draft.py │ ├── create_draft_with_attachment.py │ ├── send_message.py │ ├── test_create_draft.py │ ├── test_create_draft_with_attachment.py │ ├── test_send_message.py │ └── test_send_message_with_attachment.py │ ├── settings snippets │ ├── create_filter.py │ ├── enable_auto_reply.py │ ├── enable_forwarding.py │ ├── test_create_filter.py │ ├── test_enable_auto_reply.py │ ├── test_enable_forwarding.py │ ├── test_update_signature.py │ └── update_signature.py │ ├── smime snippets │ ├── create_smime_info.py │ ├── insert_cert_from_csv.py │ ├── insert_smime_info.py │ ├── test_create_smime_info.py │ ├── test_insert_cert_from_csv.py │ ├── test_insert_smime_info.py │ ├── test_update_smime_cert.py │ ├── test_update_smime_from_csv.py │ ├── update_smime_cert.py │ └── update_smime_from_csv.py │ └── thread │ ├── test_thread.py │ └── threads.py ├── meet ├── README.md └── quickstart │ ├── quickstart.py │ └── requirements.txt ├── people └── quickstart │ ├── README.md │ ├── quickstart.py │ └── requirements.txt ├── pyproject.toml ├── requirements.txt ├── sheets ├── images │ └── googlelogo_color_272x92dp.png ├── quickstart │ ├── README.md │ ├── quickstart.py │ └── requirements.txt └── snippets │ ├── base_test.py │ ├── sheets_append_values.py │ ├── sheets_batch_get_values.py │ ├── sheets_batch_update.py │ ├── sheets_batch_update_values.py │ ├── sheets_conditional_formatting.py │ ├── sheets_create.py │ ├── sheets_filter_views.py │ ├── sheets_get_values.py │ ├── sheets_pivot_tables.py │ ├── sheets_update_values.py │ ├── test_sheets_append_values.py │ ├── test_sheets_batch_get_values.py │ ├── test_sheets_batch_update.py │ ├── test_sheets_batch_update_values.py │ ├── test_sheets_conditional_formatting.py │ ├── test_sheets_create.py │ ├── test_sheets_filter_views.py │ ├── test_sheets_get_values.py │ ├── test_sheets_pivot_tables.py │ └── test_sheets_update_values.py ├── slides ├── quickstart │ ├── README.md │ ├── quickstart.py │ └── requirements.txt └── snippets │ ├── base_test.py │ ├── slides_copy_presentation.py │ ├── slides_create_bulleted_text.py │ ├── slides_create_image.py │ ├── slides_create_presentation.py │ ├── slides_create_sheets_chart.py │ ├── slides_create_slide.py │ ├── slides_create_textbox_with_text.py │ ├── slides_image_merging.py │ ├── slides_refresh_sheets_chart.py │ ├── slides_simple_text_replace.py │ ├── slides_text_merging.py │ ├── slides_text_style_update.py │ ├── test_slides_copy_presentation.py │ ├── test_slides_create_bulleted_text.py │ ├── test_slides_create_image.py │ ├── test_slides_create_presentation.py │ ├── test_slides_create_sheets_chart.py │ ├── test_slides_create_slide.py │ ├── test_slides_create_textbox_with_text.py │ ├── test_slides_image_merging.py │ ├── test_slides_refresh_sheets_chart.py │ ├── test_slides_simple_text_replace.py │ ├── test_slides_text_merging.py │ └── test_slides_text_style_update.py ├── tasks └── quickstart │ ├── README.md │ ├── quickstart.py │ └── requirements.txt └── vault └── quickstart ├── README.md ├── quickstart.py └── requirements.txt /.github/CODEOWNERS: -------------------------------------------------------------------------------- 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 | # https://help.github.com/en/github/creating-cloning-and-archiving-repositories/about-code-owners 16 | 17 | .github/ @googleworkspace/workspace-devrel-dpe 18 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | # Summary 2 | 3 | TODO... 4 | 5 | ## Expected Behavior 6 | 7 | TODO... 8 | 9 | ## Actual Behavior 10 | 11 | TODo... 12 | 13 | ## Steps to Reproduce the Problem 14 | 15 | 1. 16 | 1. 17 | 1. 18 | 19 | ## Specifications 20 | 21 | - Python version (`python --version`) 22 | - OS (Mac/Linux/Windows) 23 | -------------------------------------------------------------------------------- /.github/linters/.htmlhintrc: -------------------------------------------------------------------------------- 1 | { 2 | "tagname-lowercase": true, 3 | "attr-lowercase": true, 4 | "attr-value-double-quotes": true, 5 | "attr-value-not-empty": false, 6 | "attr-no-duplication": true, 7 | "doctype-first": false, 8 | "tag-pair": true, 9 | "tag-self-close": false, 10 | "spec-char-escape": false, 11 | "id-unique": true, 12 | "src-not-empty": true, 13 | "title-require": false, 14 | "alt-require": true, 15 | "doctype-html5": true, 16 | "id-class-value": false, 17 | "style-disabled": false, 18 | "inline-style-disabled": false, 19 | "inline-script-disabled": false, 20 | "space-tab-mixed-disabled": "space", 21 | "id-class-ad-disabled": false, 22 | "href-abs-or-rel": false, 23 | "attr-unsafe-chars": true, 24 | "head-script-disabled": false 25 | } 26 | -------------------------------------------------------------------------------- /.github/linters/.yaml-lint.yml: -------------------------------------------------------------------------------- 1 | --- 2 | ########################################### 3 | # These are the rules used for # 4 | # linting all the yaml files in the stack # 5 | # NOTE: # 6 | # You can disable line with: # 7 | # # yamllint disable-line # 8 | ########################################### 9 | rules: 10 | braces: 11 | level: warning 12 | min-spaces-inside: 0 13 | max-spaces-inside: 0 14 | min-spaces-inside-empty: 1 15 | max-spaces-inside-empty: 5 16 | brackets: 17 | level: warning 18 | min-spaces-inside: 0 19 | max-spaces-inside: 0 20 | min-spaces-inside-empty: 1 21 | max-spaces-inside-empty: 5 22 | colons: 23 | level: warning 24 | max-spaces-before: 0 25 | max-spaces-after: 1 26 | commas: 27 | level: warning 28 | max-spaces-before: 0 29 | min-spaces-after: 1 30 | max-spaces-after: 1 31 | comments: disable 32 | comments-indentation: disable 33 | document-end: disable 34 | document-start: 35 | level: warning 36 | present: true 37 | empty-lines: 38 | level: warning 39 | max: 2 40 | max-start: 0 41 | max-end: 0 42 | hyphens: 43 | level: warning 44 | max-spaces-after: 1 45 | indentation: 46 | level: warning 47 | spaces: consistent 48 | indent-sequences: true 49 | check-multi-line-strings: false 50 | key-duplicates: enable 51 | line-length: 52 | level: warning 53 | max: 120 54 | allow-non-breakable-words: true 55 | allow-non-breakable-inline-mappings: true 56 | new-line-at-end-of-file: disable 57 | new-lines: 58 | type: unix 59 | trailing-spaces: disable -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | # Description 2 | 3 | Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. 4 | 5 | Fixes # (issue) 6 | 7 | ## Has it been tested? 8 | - [ ] Development testing done 9 | - [ ] Unit or integration test implemented 10 | 11 | ## Checklist 12 | 13 | - [ ] My code follows the style guidelines of this project 14 | - [ ] I have performed a self-review of my own code 15 | - [ ] I have performed a peer-reviewed with team member(s) 16 | - [ ] I have commented my code, particularly in hard-to-understand areas 17 | - [ ] I have made corresponding changes to the documentation 18 | - [ ] My changes generate no new warnings 19 | - [ ] Any dependent changes have been merged and published in downstream modules 20 | -------------------------------------------------------------------------------- /.github/scripts/authorize.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | # Copyright 2020 Google LLC 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | # Utility for generating credentials used by tests. 18 | 19 | # Union of scopes used by samples 20 | SCOPES=( 21 | "https://www.googleapis.com/auth/drive" 22 | "https://www.googleapis.com/auth/drive.activity" 23 | "https://www.googleapis.com/auth/drive.appdata" 24 | "https://mail.google.com/" 25 | "https://www.googleapis.com/auth/classroom.courses" 26 | "https://www.googleapis.com/auth/classroom.announcements" 27 | "https://www.googleapis.com/auth/classroom.rosters" 28 | "https://www.googleapis.com/auth/classroom.topics" 29 | "https://www.googleapis.com/auth/classroom.guardianlinks.students" 30 | "https://www.googleapis.com/auth/classroom.coursework.students" 31 | ) 32 | 33 | if [ -z "$CLIENT_ID_FILE" ]; then 34 | echo "CLIENT_ID_FILE environment not set. Please set and run again." 35 | exit 1 36 | fi 37 | 38 | if [ ! -f "$CLIENT_ID_FILE" ]; then 39 | echo "$CLIENT_ID_FILE not found." 40 | exit 1 41 | fi 42 | 43 | printf -v EXPANDED_SCOPES '%s,' "${SCOPES[@]}" 44 | gcloud auth application-default login \ 45 | --client-id-file="$CLIENT_ID_FILE" \ 46 | --scopes="${EXPANDED_SCOPES}" 47 | 48 | cat "${HOME}/.config/gcloud/application_default_credentials.json" -------------------------------------------------------------------------------- /.github/scripts/lint.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | # Copyright 2020 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 | set -e 17 | 18 | export LC_ALL=C.UTF-8 19 | export LANG=C.UTF-8 20 | 21 | CL_FILE="${HOME}/changed_files.txt" 22 | 23 | if [ -f "${CL_FILE}" ] && ! grep -q .pylintrc "${CL_FILE}"; then 24 | grep ".py$" "${HOME}/changed_files.txt"| xargs pylint 25 | else 26 | find . -iname "*.py" -print0 | xargs pylint 27 | fi 28 | -------------------------------------------------------------------------------- /.github/scripts/test.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | # Copyright 2020 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 | export LC_ALL=C.UTF-8 17 | export LANG=C.UTF-8 18 | export PIPENV_PYTHON="${PYENV_ROOT}/shims/python" 19 | export GOOGLE_APPLICATION_CREDENTIALS="${HOME}/secrets/default_credentials.json" 20 | export SERVICE_ACCOUNT_CREDENTIALS="${HOME}/secrets/service_account.json" 21 | 22 | dirs=() 23 | 24 | if [ -f "requirements.txt" ]; then 25 | pipenv install -r "requirements.txt" 26 | fi 27 | 28 | IFS=$'\n' read -r -d '' -a dirs < <( find . -name '*_test.py' -exec dirname '{}' \; | sort -u ) 29 | 30 | exit_code=0 31 | 32 | for dir in "${dirs[@]}"; do 33 | pushd "${dir}" || exit 34 | if [ -f "requirements.txt" ]; then 35 | # If requirements.txt present, create a new isolated environment 36 | touch Pipfile 37 | pipenv install -r "requirements.txt" 38 | fi 39 | pipenv run python -m unittest discover 40 | status=$? 41 | if [ $status -ne 0 ]; then 42 | exit_code=$status 43 | fi 44 | popd || exit 45 | done 46 | 47 | if [ $exit_code -ne 0 ]; then 48 | echo "Tests failed." 49 | fi 50 | 51 | exit $exit_code -------------------------------------------------------------------------------- /.github/snippet-bot.yml: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /.github/sync-repo-settings.yaml: -------------------------------------------------------------------------------- 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 | # .github/sync-repo-settings.yaml 16 | # See https://github.com/googleapis/repo-automation-bots/tree/main/packages/sync-repo-settings for app options. 17 | rebaseMergeAllowed: true 18 | squashMergeAllowed: true 19 | mergeCommitAllowed: false 20 | deleteBranchOnMerge: true 21 | branchProtectionRules: 22 | - pattern: main 23 | isAdminEnforced: false 24 | requiresStrictStatusChecks: false 25 | requiredStatusCheckContexts: 26 | # .github/workflows/test.yml with a job called "test" 27 | - "test" 28 | # .github/workflows/lint.yml with a job called "lint" 29 | - "lint" 30 | # Google bots below 31 | - "cla/google" 32 | - "snippet-bot check" 33 | - "header-check" 34 | - "conventionalcommits.org" 35 | requiredApprovingReviewCount: 1 36 | requiresCodeOwnerReviews: true 37 | permissionRules: 38 | - team: workspace-devrel-dpe 39 | permission: admin 40 | - team: workspace-devrel 41 | permission: push 42 | -------------------------------------------------------------------------------- /.github/workflows/lint.yml: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 | name: Lint 15 | on: [push, pull_request, workflow_dispatch] 16 | 17 | jobs: 18 | lint: 19 | concurrency: 20 | group: ${{ github.head_ref || github.ref }} 21 | cancel-in-progress: true 22 | runs-on: ubuntu-20.04 23 | steps: 24 | - uses: actions/checkout@v3.0.0 25 | with: 26 | # Full git history is needed to get a proper list of changed files within `super-linter` 27 | fetch-depth: 0 28 | - uses: github/super-linter/slim@v4.9.0 29 | env: 30 | ERROR_ON_MISSING_EXEC_BIT: true 31 | VALIDATE_JSCPD: false 32 | VALIDATE_PYTHON_BLACK: false 33 | VALIDATE_PYTHON_FLAKE8: false 34 | VALIDATE_ALL_CODEBASE: ${{ github.event_name == 'push' }} 35 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 36 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # How to become a contributor and submit your own code 2 | 3 | ## Contributor License Agreements 4 | 5 | We'd love to accept your sample apps and patches! Before we can take them, we 6 | have to jump a couple of legal hurdles. 7 | 8 | Please fill out either the individual or corporate Contributor License Agreement 9 | (CLA). 10 | 11 | * If you are an individual writing original source code and you're sure you 12 | own the intellectual property, then you'll need to sign an 13 | [individual CLA](https://developers.google.com/open-source/cla/individual). 14 | * If you work for a company that wants to allow you to contribute your work, 15 | then you'll need to sign a 16 | [corporate CLA](https://developers.google.com/open-source/cla/corporate). 17 | 18 | Follow either of the two links above to access the appropriate CLA and 19 | instructions for how to sign and return it. Once we receive it, we'll be able to 20 | accept your pull requests. 21 | 22 | ## Contributing A Patch 23 | 24 | 1. Submit an issue describing your proposed change to the repository in question. 25 | 1. The repository owner will respond to your issue promptly. 26 | 1. If your proposed change is accepted, and you haven't already done so, sign a Contributor License Agreement (see details above). 27 | 1. Fork the desired repository, develop and test your code changes. 28 | 1. Ensure that your code adheres to the existing style in the sample to which you are contributing. 29 | 1. Ensure that your code has an appropriate set of unit tests which all pass. 30 | 1. Submit a pull request! 31 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Report a security issue 2 | 3 | To report a security issue, please use [https://g.co/vulnz](https://g.co/vulnz). We use 4 | [https://g.co/vulnz](https://g.co/vulnz) for our intake, and do coordination and disclosure here on 5 | GitHub (including using GitHub Security Advisory). The Google Security Team will 6 | respond within 5 working days of your report on [https://g.co/vulnz](https://g.co/vulnz). 7 | -------------------------------------------------------------------------------- /admin_sdk/directory/README.md: -------------------------------------------------------------------------------- 1 | # Google Admin SDK Directory Python Quickstart 2 | 3 | Complete the steps described in the [Google Admin SDK Directory Python 4 | Quickstart](https://developers.google.com/admin-sdk/directory/v1/quickstart/python), and in about five minutes you'll have a simple 5 | Python command-line application that makes requests to the Google Admin SDK 6 | Directory API. 7 | 8 | ## Install 9 | 10 | ```shell 11 | pip install -r requirements.txt 12 | ``` 13 | 14 | ## Run 15 | 16 | ```shell 17 | python quickstart.py 18 | ``` 19 | -------------------------------------------------------------------------------- /admin_sdk/directory/requirements.txt: -------------------------------------------------------------------------------- 1 | google-api-python-client==1.7.9 2 | google-auth-httplib2==0.0.3 3 | google-auth-oauthlib==0.4.0 -------------------------------------------------------------------------------- /admin_sdk/reports/README.md: -------------------------------------------------------------------------------- 1 | # Google Admin SDK Reports Python Quickstart 2 | 3 | Complete the steps described in the [Google Admin SDK Reports Python 4 | Quickstart](https://developers.google.com/admin-sdk/reports/v1/quickstart/python), 5 | and in about five minutes you'll have a simple Python command-line application 6 | that makes requests to the Google Admin SDK Reports API. 7 | 8 | ## Install 9 | 10 | ```shell 11 | pip install -r requirements.txt 12 | ``` 13 | 14 | ## Run 15 | 16 | ```shell 17 | python quickstart.py 18 | ``` 19 | -------------------------------------------------------------------------------- /admin_sdk/reports/requirements.txt: -------------------------------------------------------------------------------- 1 | google-api-python-client==1.7.9 2 | google-auth-httplib2==0.0.3 3 | google-auth-oauthlib==0.4.0 -------------------------------------------------------------------------------- /admin_sdk/reseller/README.md: -------------------------------------------------------------------------------- 1 | # Google Admin SDK Reseller Python Quickstart 2 | 3 | Complete the steps described in the [Google Admin SDK Reseller Python 4 | Quickstart](https://developers.google.com/admin-sdk/reseller/v1/quickstart/python), 5 | and in about five minutes you'll have a simple Python command-line application 6 | that makes requests to the Google Admin SDK Reseller API. 7 | 8 | ## Install 9 | 10 | ```shell 11 | pip install -r requirements.txt 12 | ``` 13 | 14 | ## Run 15 | 16 | ```shell 17 | python quickstart.py 18 | ``` 19 | -------------------------------------------------------------------------------- /admin_sdk/reseller/requirements.txt: -------------------------------------------------------------------------------- 1 | google-api-python-client==1.7.9 2 | google-auth-httplib2==0.0.3 3 | google-auth-oauthlib==0.4.0 -------------------------------------------------------------------------------- /apps_script/quickstart/README.md: -------------------------------------------------------------------------------- 1 | # Google Apps Script API Python Quickstart 2 | 3 | Complete the steps described in the [Google Apps Script API Python 4 | Quickstart](https://developers.google.com/apps-script/api/quickstart/python), 5 | and in about five minutes you'll have a simple Python command-line application 6 | that makes requests to the Google Apps Script API. 7 | 8 | ## Install 9 | 10 | ```shell 11 | pip install -r requirements.txt 12 | ``` 13 | 14 | ## Run 15 | 16 | ```shell 17 | python quickstart.py 18 | ``` 19 | -------------------------------------------------------------------------------- /apps_script/quickstart/requirements.txt: -------------------------------------------------------------------------------- 1 | google-api-python-client==1.7.9 2 | google-auth-httplib2==0.0.3 3 | google-auth-oauthlib==0.4.0 -------------------------------------------------------------------------------- /calendar/quickstart/README.md: -------------------------------------------------------------------------------- 1 | # Google Calendar Python Quickstart 2 | 3 | Complete the steps described in the [Google Calendar Python Quickstart]( 4 | https://developers.google.com/google-apps/calendar/quickstart/python), and in 5 | about five minutes you'll have a simple Python command-line application that 6 | makes requests to the Google Calendar API. 7 | 8 | ## Install 9 | 10 | ```shell 11 | pip install -r requirements.txt 12 | ``` 13 | 14 | ## Run 15 | 16 | ```shell 17 | python quickstart.py 18 | ``` 19 | -------------------------------------------------------------------------------- /calendar/quickstart/requirements.txt: -------------------------------------------------------------------------------- 1 | google-api-python-client==1.7.9 2 | google-auth-httplib2==0.0.3 3 | google-auth-oauthlib==0.4.0 -------------------------------------------------------------------------------- /chat/client-libraries/cloud/README.md: -------------------------------------------------------------------------------- 1 | # Google Chat API - Cloud Client library samples 2 | 3 | ## Set up 4 | 5 | 1. Add `service_account.json` and/or `client_secrets.json` to the current 6 | folder depending on the credentials used by the samples to run: 7 | 8 | 1. `service_account.json` for 9 | [app credentials](https://developers.google.com/workspace/chat/authenticate-authorize-chat-app) 10 | 11 | 1. `client_secrets.json` for 12 | [user credentials](https://developers.google.com/workspace/chat/authenticate-authorize-chat-user) 13 | 14 | 1. Execute `pip install -r requirements.txt` 15 | 16 | ## Run 17 | 18 | Execute `python replace-with-the-sample-file.py` wih the sample file path of the sample. 19 | 20 | For example, to run the sample `create-message-app-cred`, you should run 21 | `python create-message-app-cred.py`. 22 | -------------------------------------------------------------------------------- /chat/client-libraries/cloud/create_space_user_cred.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Copyright 2024 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 | # It may require modifications to work in your environment. 17 | 18 | # To install the latest published package dependency, execute the following: 19 | # python3 -m pip install google-apps-chat 20 | 21 | # [START chat_create_space_user_cred] 22 | from authentication_utils import create_client_with_user_credentials 23 | from google.apps import chat_v1 as google_chat 24 | 25 | SCOPES = ["https://www.googleapis.com/auth/chat.spaces.create"] 26 | 27 | def create_space_with_user_cred(): 28 | # Create a client 29 | client = create_client_with_user_credentials(SCOPES) 30 | 31 | # Initialize request argument(s) 32 | request = google_chat.CreateSpaceRequest( 33 | space = { 34 | "space_type": 'SPACE', 35 | # Replace DISPLAY_NAME here. 36 | "display_name": 'DISPLAY_NAME' 37 | } 38 | ) 39 | 40 | # Make the request 41 | response = client.create_space(request) 42 | 43 | # Handle the response 44 | print(response) 45 | 46 | create_space_with_user_cred() 47 | 48 | # [END chat_create_space_user_cred] 49 | -------------------------------------------------------------------------------- /chat/client-libraries/cloud/delete_message_app_cred.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Copyright 2024 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 | # It may require modifications to work in your environment. 17 | 18 | # To install the latest published package dependency, execute the following: 19 | # python3 -m pip install google-apps-chat 20 | 21 | # [START chat_delete_message_app_cred] 22 | from authentication_utils import create_client_with_app_credentials 23 | from google.apps import chat_v1 as google_chat 24 | 25 | # This sample shows how to delete a message with app credential 26 | def delete_message_with_app_cred(): 27 | # Create a client 28 | client = create_client_with_app_credentials() 29 | 30 | # Initialize request argument(s) 31 | request = google_chat.DeleteMessageRequest( 32 | # Replace SPACE_NAME and MESSAGE_NAME here 33 | name = "spaces/SPACE_NAME/messages/MESSAGE_NAME", 34 | ) 35 | 36 | # Make the request 37 | response = client.delete_message(request) 38 | 39 | # Handle the response 40 | print(response) 41 | 42 | delete_message_with_app_cred() 43 | 44 | # [END chat_delete_message_app_cred] 45 | -------------------------------------------------------------------------------- /chat/client-libraries/cloud/delete_message_user_cred.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Copyright 2024 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 | # It may require modifications to work in your environment. 17 | 18 | # To install the latest published package dependency, execute the following: 19 | # python3 -m pip install google-apps-chat 20 | 21 | # [START chat_delete_message_user_cred] 22 | from authentication_utils import create_client_with_user_credentials 23 | from google.apps import chat_v1 as google_chat 24 | 25 | SCOPES = ["https://www.googleapis.com/auth/chat.messages"] 26 | 27 | # This sample shows how to delete a message with user credential 28 | def delete_message_with_user_cred(): 29 | # Create a client 30 | client = create_client_with_user_credentials(SCOPES) 31 | 32 | # Initialize request argument(s) 33 | request = google_chat.DeleteMessageRequest( 34 | # Replace SPACE_NAME and MESSAGE_NAME here 35 | name = "spaces/SPACE_NAME/messages/MESSAGE_NAME", 36 | ) 37 | 38 | # Make the request 39 | response = client.delete_message(request) 40 | 41 | # Handle the response 42 | print(response) 43 | 44 | delete_message_with_user_cred() 45 | 46 | # [END chat_delete_message_user_cred] 47 | -------------------------------------------------------------------------------- /chat/client-libraries/cloud/get_membership_app_cred.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Copyright 2024 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 | # It may require modifications to work in your environment. 17 | 18 | # To install the latest published package dependency, execute the following: 19 | # python3 -m pip install google-apps-chat 20 | 21 | # [START chat_get_membership_app_cred] 22 | from authentication_utils import create_client_with_app_credentials 23 | from google.apps import chat_v1 as google_chat 24 | 25 | # This sample shows how to get membership with app credential 26 | def get_membership_with_app_cred(): 27 | # Create a client 28 | client = create_client_with_app_credentials() 29 | 30 | # Initialize request argument(s) 31 | request = google_chat.GetMembershipRequest( 32 | # Replace SPACE_NAME and MEMBER_NAME here 33 | name = 'spaces/SPACE_NAME/members/MEMBER_NAME', 34 | ) 35 | 36 | # Make the request 37 | response = client.get_membership(request) 38 | 39 | # Handle the response 40 | print(response) 41 | 42 | get_membership_with_app_cred() 43 | 44 | # [END chat_get_membership_app_cred] 45 | -------------------------------------------------------------------------------- /chat/client-libraries/cloud/get_membership_user_cred.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Copyright 2024 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 | # It may require modifications to work in your environment. 17 | 18 | # To install the latest published package dependency, execute the following: 19 | # python3 -m pip install google-apps-chat 20 | 21 | # [START chat_get_membership_user_cred] 22 | from authentication_utils import create_client_with_user_credentials 23 | from google.apps import chat_v1 as google_chat 24 | 25 | SCOPES = ["https://www.googleapis.com/auth/chat.memberships.readonly"] 26 | 27 | # This sample shows how to get membership with user credential 28 | def get_membership_with_user_cred(): 29 | # Create a client 30 | client = create_client_with_user_credentials(SCOPES) 31 | 32 | # Initialize request argument(s) 33 | request = google_chat.GetMembershipRequest( 34 | # Replace SPACE_NAME and MEMBER_NAME here 35 | name = 'spaces/SPACE_NAME/members/MEMBER_NAME', 36 | ) 37 | 38 | # Make the request 39 | response = client.get_membership(request) 40 | 41 | # Handle the response 42 | print(response) 43 | 44 | get_membership_with_user_cred() 45 | 46 | # [END chat_get_membership_user_cred] 47 | -------------------------------------------------------------------------------- /chat/client-libraries/cloud/get_message_app_cred.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Copyright 2024 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 | # It may require modifications to work in your environment. 17 | 18 | # To install the latest published package dependency, execute the following: 19 | # python3 -m pip install google-apps-chat 20 | 21 | # [START chat_get_message_app_cred] 22 | from authentication_utils import create_client_with_app_credentials 23 | from google.apps import chat_v1 as google_chat 24 | 25 | # This sample shows how to get message with app credential 26 | def get_message_with_app_cred(): 27 | # Create a client 28 | client = create_client_with_app_credentials() 29 | 30 | # Initialize request argument(s) 31 | request = google_chat.GetMessageRequest( 32 | # Replace SPACE_NAME and MESSAGE_NAME here 33 | name = 'spaces/SPACE_NAME/messages/MESSAGE_NAME', 34 | ) 35 | 36 | # Make the request 37 | response = client.get_message(request=request) 38 | 39 | # Handle the response 40 | print(response) 41 | 42 | get_message_with_app_cred() 43 | 44 | # [END chat_get_message_app_cred] 45 | -------------------------------------------------------------------------------- /chat/client-libraries/cloud/get_message_user_cred.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Copyright 2024 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 | # It may require modifications to work in your environment. 17 | 18 | # To install the latest published package dependency, execute the following: 19 | # python3 -m pip install google-apps-chat 20 | 21 | # [START chat_get_message_user_cred] 22 | from authentication_utils import create_client_with_user_credentials 23 | import google.oauth2.credentials 24 | 25 | from google.apps import chat_v1 as google_chat 26 | 27 | SCOPES = ["https://www.googleapis.com/auth/chat.messages.readonly"] 28 | 29 | # This sample shows how to get message with user credential 30 | def get_message_with_user_cred(): 31 | # Create a client 32 | client = create_client_with_user_credentials(SCOPES) 33 | 34 | # Initialize request argument(s) 35 | request = google_chat.GetMessageRequest( 36 | # Replace SPACE_NAME and MESSAGE_NAME here 37 | name = "spaces/SPACE_NAME/messages/MESSAGE_NAME", 38 | ) 39 | 40 | # Make the request 41 | response = client.get_message(request) 42 | 43 | # Handle the response 44 | print(response) 45 | 46 | get_message_with_user_cred() 47 | 48 | # [END chat_get_message_user_cred] 49 | -------------------------------------------------------------------------------- /chat/client-libraries/cloud/get_space_app_cred.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Copyright 2024 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 | # It may require modifications to work in your environment. 17 | 18 | # To install the latest published package dependency, execute the following: 19 | # python3 -m pip install google-apps-chat 20 | 21 | # [START chat_get_space_app_cred] 22 | from authentication_utils import create_client_with_app_credentials 23 | from google.apps import chat_v1 as google_chat 24 | 25 | # This sample shows how to get space with app credential 26 | def get_space_with_app_cred(): 27 | # Create a client 28 | client = create_client_with_app_credentials() 29 | 30 | # Initialize request argument(s) 31 | request = google_chat.GetSpaceRequest( 32 | # Replace SPACE_NAME here 33 | name = "spaces/SPACE_NAME", 34 | ) 35 | 36 | # Make the request 37 | response = client.get_space(request) 38 | 39 | # Handle the response 40 | print(response) 41 | 42 | get_space_with_app_cred() 43 | 44 | # [END chat_get_space_app_cred] 45 | -------------------------------------------------------------------------------- /chat/client-libraries/cloud/get_space_user_cred.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Copyright 2024 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 | # Snippet for GetSpace 17 | 18 | # To install the latest published package dependency, execute the following: 19 | # python3 -m pip install google-apps-chat 20 | 21 | # [START chat_get_space_user_cred] 22 | from authentication_utils import create_client_with_user_credentials 23 | from google.apps import chat_v1 as google_chat 24 | 25 | SCOPES = ["https://www.googleapis.com/auth/chat.spaces.readonly"] 26 | 27 | # This sample shows how to get space with user credential 28 | def get_space_with_user_cred(): 29 | # Create a client 30 | client = create_client_with_user_credentials(SCOPES) 31 | 32 | # Initialize request argument(s) 33 | request = google_chat.GetSpaceRequest( 34 | # Replace SPACE_NAME here 35 | name = "spaces/SPACE_NAME", 36 | ) 37 | 38 | # Make the request 39 | response = client.get_space(request) 40 | 41 | # Handle the response 42 | print(response) 43 | 44 | get_space_with_user_cred() 45 | 46 | # [END chat_get_space_user_cred] 47 | -------------------------------------------------------------------------------- /chat/client-libraries/cloud/list_messages_user_cred.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Copyright 2024 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 | # It may require modifications to work in your environment. 17 | 18 | # To install the latest published package dependency, execute the following: 19 | # python3 -m pip install google-apps-chat 20 | 21 | # [START chat_list_messages_user_cred] 22 | from authentication_utils import create_client_with_user_credentials 23 | from google.apps import chat_v1 as google_chat 24 | 25 | SCOPES = ["https://www.googleapis.com/auth/chat.messages.readonly"] 26 | 27 | # This sample shows how to list messages with user credential 28 | def list_messages_with_user_cred(): 29 | # Create a client 30 | client = create_client_with_user_credentials(SCOPES) 31 | 32 | # Initialize request argument(s) 33 | request = google_chat.ListMessagesRequest( 34 | # Replace SPACE_NAME here 35 | parent = 'spaces/SPACE_NAME', 36 | # Number of results that will be returned at once 37 | page_size = 100 38 | ) 39 | 40 | # Make the request 41 | page_result = client.list_messages(request) 42 | 43 | # Handle the response. Iterating over page_result will yield results and 44 | # resolve additional pages automatically. 45 | for response in page_result: 46 | print(response) 47 | 48 | list_messages_with_user_cred() 49 | 50 | # [END chat_list_messages_user_cred] 51 | -------------------------------------------------------------------------------- /chat/client-libraries/cloud/list_spaces_app_cred.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Copyright 2024 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 | # It may require modifications to work in your environment. 17 | 18 | # To install the latest published package dependency, execute the following: 19 | # python3 -m pip install google-apps-chat 20 | 21 | # [START chat_list_spaces_app_cred] 22 | from authentication_utils import create_client_with_app_credentials 23 | from google.apps import chat_v1 as google_chat 24 | 25 | # This sample shows how to list spaces with app credential 26 | def list_spaces_app_cred(): 27 | # Create a client 28 | client = create_client_with_app_credentials() 29 | 30 | # Initialize request argument(s) 31 | request = google_chat.ListSpacesRequest( 32 | # Filter spaces by space type (SPACE or GROUP_CHAT or DIRECT_MESSAGE) 33 | filter = 'space_type = "SPACE"', 34 | # Number of results that will be returned at once 35 | page_size = 100 36 | ) 37 | 38 | # Make the request 39 | page_result = client.list_spaces(request) 40 | 41 | # Handle the response. Iterating over page_result will yield results and 42 | # resolve additional pages automatically. 43 | for response in page_result: 44 | print(response) 45 | 46 | list_spaces_app_cred() 47 | 48 | # [END chat_list_spaces_app_cred] 49 | -------------------------------------------------------------------------------- /chat/client-libraries/cloud/requirements.txt: -------------------------------------------------------------------------------- 1 | google_auth_oauthlib==1.2.0 2 | protobuf==4.21.12 3 | google-apps-chat==0.1.9 4 | -------------------------------------------------------------------------------- /chat/client-libraries/cloud/set_up_space_user_cred.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Copyright 2024 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 | # It may require modifications to work in your environment. 17 | 18 | # To install the latest published package dependency, execute the following: 19 | # python3 -m pip install google-apps-chat 20 | 21 | # [START chat_set_up_space_user_cred] 22 | from authentication_utils import create_client_with_user_credentials 23 | from google.apps import chat_v1 as google_chat 24 | 25 | SCOPES = ["https://www.googleapis.com/auth/chat.spaces.create"] 26 | 27 | def set_up_space_with_user_cred(): 28 | # Create a client 29 | client = create_client_with_user_credentials(SCOPES) 30 | 31 | # Initialize request argument(s) 32 | request = google_chat.SetUpSpaceRequest( 33 | space = { 34 | "space_type": 'SPACE', 35 | # Replace DISPLAY_NAME here. 36 | "display_name": 'DISPLAY_NAME' 37 | }, 38 | memberships = [{ 39 | "member": { 40 | # Replace USER_NAME here. 41 | "name": 'users/USER_NAME', 42 | "type_": 'HUMAN' 43 | } 44 | }] 45 | ) 46 | 47 | # Make the request 48 | response = client.set_up_space(request) 49 | 50 | # Handle the response 51 | print(response) 52 | 53 | set_up_space_with_user_cred() 54 | 55 | # [END chat_set_up_space_user_cred] 56 | -------------------------------------------------------------------------------- /chat/client-libraries/cloud/update_space_user_cred.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Copyright 2024 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 | # It may require modifications to work in your environment. 17 | 18 | # To install the latest published package dependency, execute the following: 19 | # python3 -m pip install google-apps-chat 20 | 21 | 22 | # [START chat_update_space_user_cred] 23 | from authentication_utils import create_client_with_user_credentials 24 | from google.apps import chat_v1 as google_chat 25 | 26 | SCOPES = ["https://www.googleapis.com/auth/chat.spaces"] 27 | 28 | # This sample shows how to update a space with user credential 29 | def update_space_with_user_cred(): 30 | # Create a client 31 | client = create_client_with_user_credentials(SCOPES) 32 | 33 | # Initialize request argument(s) 34 | request = google_chat.UpdateSpaceRequest( 35 | space = { 36 | # Replace SPACE_NAME here 37 | 'name': 'spaces/SPACE_NAME', 38 | 'display_name': 'New space display name' 39 | }, 40 | # The field paths to update. Separate multiple values with commas. 41 | update_mask = 'displayName' 42 | ) 43 | 44 | # Make the request 45 | response = client.update_space(request) 46 | 47 | # Handle the response 48 | print(response) 49 | 50 | update_space_with_user_cred() 51 | 52 | # [END chat_update_space_user_cred] 53 | -------------------------------------------------------------------------------- /chat/quickstart/README.md: -------------------------------------------------------------------------------- 1 | # Google Chat Python Quickstart 2 | 3 | Complete the steps described in the [quickstart instructions]( 4 | https://developers.google.com/workspace/chat/api/guides/quickstart/python), 5 | and in about five minutes you'll have a simple Python command-line 6 | application that makes requests to the Google Chat API. 7 | 8 | ## Install 9 | 10 | `pip install -r requirements` 11 | 12 | ## Run 13 | 14 | After following the quickstart setup instructions, run the sample: 15 | 16 | `python3 quickstart.py` 17 | -------------------------------------------------------------------------------- /chat/quickstart/requirements.txt: -------------------------------------------------------------------------------- 1 | google-apps-chat==0.1.0 2 | google-auth-httplib2==0.1.0 3 | google-auth-oauthlib==0.4.0 4 | -------------------------------------------------------------------------------- /classroom/quickstart/README.md: -------------------------------------------------------------------------------- 1 | # Google Classroom Python Quickstart 2 | 3 | Complete the steps described in the [Google Classroom Python Quickstart]( 4 | https://developers.google.com/classroom/quickstart/python), and in 5 | about five minutes you'll have a simple Python command-line application that 6 | makes requests to the Google Classroom API. 7 | 8 | ## Install 9 | 10 | ```shell 11 | pip install -r requirements.txt 12 | ``` 13 | 14 | ## Run 15 | 16 | ```shell 17 | python quickstart.py 18 | ``` 19 | -------------------------------------------------------------------------------- /classroom/quickstart/requirements.txt: -------------------------------------------------------------------------------- 1 | google-api-python-client==1.7.9 2 | google-auth-httplib2==0.0.3 3 | google-auth-oauthlib==0.4.0 -------------------------------------------------------------------------------- /classroom/snippets/classroom_get_course.py: -------------------------------------------------------------------------------- 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 | 17 | # [START classroom_get_course] 18 | import google.auth 19 | from googleapiclient.discovery import build 20 | from googleapiclient.errors import HttpError 21 | 22 | 23 | def classroom_get_course(course_id): 24 | """ 25 | Prints the name of the with specific course_id. 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 | 31 | creds, _ = google.auth.default() 32 | # pylint: disable=maybe-no-member 33 | course = None 34 | try: 35 | service = build("classroom", "v1", credentials=creds) 36 | course = service.courses().get(id=course_id).execute() 37 | print(f"Course found : {course.get('name')}") 38 | except HttpError as error: 39 | print(f"An error occurred: {error}") 40 | print(f"Course not found: {course_id}") 41 | return error 42 | return course 43 | 44 | 45 | if __name__ == "__main__": 46 | # Put the course_id of course whose information needs to be fetched. 47 | classroom_get_course("course_id") 48 | 49 | # [END classroom_get_courses] 50 | -------------------------------------------------------------------------------- /classroom/snippets/classroom_patch_course.py: -------------------------------------------------------------------------------- 1 | """Copyright 2018 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 classroom_patch_course] 17 | import google.auth 18 | from googleapiclient.discovery import build 19 | from googleapiclient.errors import HttpError 20 | 21 | 22 | def classroom_patch_course(course_id): 23 | """ 24 | Patch new course with existing course in the account the user has access to. 25 | Load pre-authorized user credentials from the environment. 26 | TODO(developer) - See https://developers.google.com/identity 27 | for guides on implementing OAuth2 for the application. 28 | """ 29 | # pylint: disable=maybe-no-member 30 | 31 | creds, _ = google.auth.default() 32 | 33 | try: 34 | service = build("classroom", "v1", credentials=creds) 35 | course = {"section": "Period 3", "room": "313"} 36 | course = ( 37 | service.courses() 38 | .patch(id=course_id, updateMask="section,room", body=course) 39 | .execute() 40 | ) 41 | print(f" Course updated are: {course.get('name')}") 42 | return course 43 | except HttpError as error: 44 | print(f"An error occurred: {error}") 45 | 46 | 47 | if __name__ == "__main__": 48 | # Put the course_id of course with whom we need to patch some extra 49 | # information. 50 | classroom_patch_course("course_id") 51 | 52 | # [END classroom_patch_course] 53 | -------------------------------------------------------------------------------- /classroom/snippets/test_classroom_create_course.py: -------------------------------------------------------------------------------- 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 | 14 | import unittest 15 | 16 | import classroom_create_course 17 | from base_test import BaseTest 18 | 19 | 20 | class TestClassroomCreateCourse(BaseTest): 21 | """Unit test class for Create course snippet""" 22 | 23 | def test_classroom_create_course(self): 24 | """Class function for Create course snippet""" 25 | course = classroom_create_course.classroom_create_course() 26 | self.assertIsNotNone(course) 27 | self.delete_course_on_cleanup(course.get("id")) 28 | 29 | 30 | if __name__ == "__main__": 31 | unittest.main() 32 | -------------------------------------------------------------------------------- /classroom/snippets/test_classroom_get_course.py: -------------------------------------------------------------------------------- 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 | 14 | import unittest 15 | 16 | import classroom_create_course 17 | import classroom_get_course 18 | from base_test import BaseTest 19 | 20 | 21 | class TestClassroomGetCourse(BaseTest): 22 | """Unit test class for Get course snippet""" 23 | 24 | def test_classroom_get_course(self): 25 | """Unit test method for Get course snippet""" 26 | course = classroom_create_course.classroom_create_course() 27 | self.assertIsNotNone(course) 28 | self.delete_course_on_cleanup(course.get("id")) 29 | course_id = classroom_get_course.classroom_get_course(course.get("id")) 30 | self.assertIsNotNone(course_id) 31 | 32 | 33 | if __name__ == "__main__": 34 | unittest.main() 35 | -------------------------------------------------------------------------------- /classroom/snippets/test_classroom_list_course_aliases.py: -------------------------------------------------------------------------------- 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 | 14 | import unittest 15 | 16 | from base_test import BaseTest 17 | from classroom_create_course import classroom_create_course 18 | from classroom_list_course_aliases import classroom_list_course_aliases 19 | 20 | 21 | class TestClassroomListCourseAliases(BaseTest): 22 | """Unit test class for List course aliases snippet""" 23 | 24 | def test_classroom_list_course_aliases(self): 25 | """Unit test method for List course snippet""" 26 | course = classroom_create_course() 27 | self.assertIsNotNone(course) 28 | self.delete_course_on_cleanup(course.get("id")) 29 | course_aliases = classroom_list_course_aliases(course.get("id")) 30 | self.assertIsNotNone(course_aliases) 31 | 32 | 33 | if __name__ == "__main__": 34 | unittest.main() 35 | -------------------------------------------------------------------------------- /classroom/snippets/test_classroom_list_courses.py: -------------------------------------------------------------------------------- 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 | 14 | import unittest 15 | 16 | import classroom_create_course 17 | import classroom_list_courses 18 | from base_test import BaseTest 19 | 20 | 21 | class TestClassroomListCourses(BaseTest): 22 | """Unit test class for List course snippet""" 23 | 24 | def test_classroom_list_courses(self): 25 | """Unit test method for List course snippet""" 26 | course = classroom_create_course.classroom_create_course() 27 | self.assertIsNotNone(course) 28 | self.delete_course_on_cleanup(course.get("id")) 29 | courses = classroom_list_courses.classroom_list_courses() 30 | self.assertIsNotNone(courses) 31 | 32 | 33 | if __name__ == "__main__": 34 | unittest.main() 35 | -------------------------------------------------------------------------------- /classroom/snippets/test_classroom_patch_course.py: -------------------------------------------------------------------------------- 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 | 14 | import unittest 15 | 16 | import classroom_create_course 17 | import classroom_patch_course 18 | from base_test import BaseTest 19 | 20 | 21 | class TestClassroomPatchCourse(BaseTest): 22 | """Unit test class for Patch course snippet""" 23 | 24 | def test_classroom_patch_course(self): 25 | """Unit test method for Patch course snippet""" 26 | course = classroom_create_course.classroom_create_course() 27 | self.assertIsNotNone(course) 28 | self.delete_course_on_cleanup(course.get("id")) 29 | course = classroom_patch_course.classroom_patch_course(course.get("id")) 30 | self.assertIsNotNone(course) 31 | 32 | 33 | if __name__ == "__main__": 34 | unittest.main() 35 | -------------------------------------------------------------------------------- /classroom/snippets/test_classroom_update_course.py: -------------------------------------------------------------------------------- 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 | 14 | import unittest 15 | 16 | import classroom_create_course 17 | import classroom_update_course 18 | from base_test import BaseTest 19 | 20 | 21 | class TestClassroomUpdateCourse(BaseTest): 22 | """Unit test class for Get course snippet""" 23 | 24 | def test_classroom_update_course(self): 25 | """Unit test method for Get course snippet""" 26 | course = classroom_create_course.classroom_create_course() 27 | self.assertIsNotNone(course) 28 | self.delete_course_on_cleanup(course.get("id")) 29 | course = classroom_update_course.classroom_update_course(course.get("id")) 30 | self.assertIsNotNone(course) 31 | 32 | 33 | if __name__ == "__main__": 34 | unittest.main() 35 | -------------------------------------------------------------------------------- /docs/mail-merge/requirements.txt: -------------------------------------------------------------------------------- 1 | google-auth==2.3.3 2 | google-api-python-client==2.31.0 3 | oauth2client==4.1.3 4 | -------------------------------------------------------------------------------- /docs/output-json/output-json.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # Copyright ©2018-2019 Google LLC 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 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 | ouput-json.py (Python 2.x or 3.x) 17 | Google Docs (REST) API output-json sample app 18 | """ 19 | # [START output_json_python] 20 | import json 21 | 22 | from apiclient import discovery 23 | from httplib2 import Http 24 | from oauth2client import client, file, tools 25 | 26 | # Set doc ID, as found at `https://docs.google.com/document/d/YOUR_DOC_ID/edit` 27 | DOCUMENT_ID = "YOUR_DOC_ID" 28 | 29 | # Set the scopes and discovery info 30 | SCOPES = "https://www.googleapis.com/auth/documents.readonly" 31 | DISCOVERY_DOC = "https://docs.googleapis.com/$discovery/rest?version=v1" 32 | 33 | # Initialize credentials and instantiate Docs API service 34 | store = file.Storage("token.json") 35 | creds = store.get() 36 | if not creds or creds.invalid: 37 | flow = client.flow_from_clientsecrets("credentials.json", SCOPES) 38 | creds = tools.run_flow(flow, store) 39 | service = discovery.build( 40 | "docs", 41 | "v1", 42 | http=creds.authorize(Http()), 43 | discoveryServiceUrl=DISCOVERY_DOC, 44 | ) 45 | 46 | # Do a document "get" request and print the results as formatted JSON 47 | result = service.documents().get(documentId=DOCUMENT_ID, includeTabsContent=True).execute() 48 | print(json.dumps(result, indent=4, sort_keys=True)) 49 | # [END output_json_python] 50 | -------------------------------------------------------------------------------- /docs/output-json/output_json.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # Copyright ©2018-2019 Google LLC 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 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 | ouput-json.py (Python 2.x or 3.x) 17 | Google Docs (REST) API output-json sample app 18 | """ 19 | # [START output_json_python] 20 | import json 21 | 22 | import google.auth 23 | from googleapiclient.discovery import build 24 | from googleapiclient.errors import HttpError 25 | 26 | # Set doc ID, as found at `https://docs.google.com/document/d/YOUR_DOC_ID/edit` 27 | DOCUMENT_ID = "195j9eDD3ccgjQRttHhJPymLJUCOUjs-jmwTrekvdjFE" 28 | 29 | # Set the scopes and discovery info 30 | SCOPES = "https://www.googleapis.com/auth/documents.readonly" 31 | DISCOVERY_DOC = "https://docs.googleapis.com/$discovery/rest?version=v1" 32 | 33 | # Initialize credentials and instantiate Docs API service 34 | creds, _ = google.auth.default() 35 | # pylint: disable=maybe-no-member 36 | try: 37 | service = build("docs", "v1", credentials=creds) 38 | 39 | # Do a document "get" request and print the results as formatted JSON 40 | 41 | result = service.documents().get(documentId=DOCUMENT_ID).execute() 42 | print(json.dumps(result, indent=4, sort_keys=True)) 43 | except HttpError as error: 44 | print(f"An error occurred: {error}") 45 | 46 | # [END output_json_python] 47 | -------------------------------------------------------------------------------- /docs/quickstart/README.md: -------------------------------------------------------------------------------- 1 | # Google Docs Python Quickstart 2 | 3 | Complete the steps described in the [quickstart instructions]( 4 | https://developers.google.com/docs/api/quickstart/python), and in about five 5 | minutes you'll have a simple Python command-line application that makes 6 | requests to the Google Docs API. 7 | 8 | ## Install 9 | 10 | ```shell 11 | pip install -r requirements.txt 12 | ``` 13 | 14 | ## Run 15 | 16 | After following the quickstart setup instructions, run the sample: 17 | 18 | ```shell 19 | python quickstart.py 20 | ``` 21 | -------------------------------------------------------------------------------- /docs/quickstart/requirements.txt: -------------------------------------------------------------------------------- 1 | google-api-python-client==1.7.9 2 | google-auth-httplib2==0.0.3 3 | google-auth-oauthlib==0.4.0 -------------------------------------------------------------------------------- /drive/activity-v2/requirements.txt: -------------------------------------------------------------------------------- 1 | google-api-python-client==1.7.9 2 | google-auth-httplib2==0.0.3 3 | google-auth-oauthlib==0.4.0 -------------------------------------------------------------------------------- /drive/driveapp/client_secrets.json: -------------------------------------------------------------------------------- 1 | {"installed":{"auth_uri":"https://accounts.google.com/o/oauth2/auth","client_secret":"ckGVrYYQ_GE7O4rL80ozlEXR","token_uri":"https://accounts.google.com/o/oauth2/token","client_email":"","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","oob"],"client_x509_cert_url":"","client_id":"665081966568.apps.googleusercontent.com","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs"}} -------------------------------------------------------------------------------- /drive/driveapp/document.txt: -------------------------------------------------------------------------------- 1 | This file will be uploaded to Google Drive as plain text. 2 | 3 | Hello world :) 4 | -------------------------------------------------------------------------------- /drive/quickstart/README.md: -------------------------------------------------------------------------------- 1 | # Drive V3 Python Quickstart 2 | 3 | Complete the steps described in the [Drive V3 Python Quickstart]( 4 | https://developers.google.com/drive/v3/web/quickstart/python), and in 5 | about five minutes you'll have a simple Python command-line application that 6 | makes requests to the Drive V3 API. 7 | 8 | ## Prerequisites 9 | - Python 10 | - Create a project 11 | - Activate the Drive API in the Google API Console([the detail page](https://developers.google.com/workspace/guides/create-project)) 12 | - Create a OAuth client ID credential and download the json file ([the detail page](https://developers.google.com/workspace/guides/create-credentials)) 13 | - Rename the json file 14 | 15 | ## Install 16 | 17 | ```shell 18 | pip install -r requirements.txt 19 | ``` 20 | 21 | ## Run 22 | 23 | ```shell 24 | python quickstart.py 25 | ``` 26 | -------------------------------------------------------------------------------- /drive/quickstart/requirements.txt: -------------------------------------------------------------------------------- 1 | google-api-python-client==1.7.9 2 | google-auth-httplib2==0.0.3 3 | google-auth-oauthlib==0.4.0 -------------------------------------------------------------------------------- /drive/snippets/drive-v2/app data snippet/fetch_appdata_folder.py: -------------------------------------------------------------------------------- 1 | """Copyright 2018 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 | import google.auth 18 | from googleapiclient.discovery import build 19 | from googleapiclient.errors import HttpError 20 | 21 | 22 | def fetch_appdata_folder(): 23 | """List out application data folder and prints folder ID. 24 | Returns : Folder ID 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 | # call drive api client 34 | service = build("drive", "v2", credentials=creds) 35 | 36 | # pylint: disable=maybe-no-member 37 | file = service.files().get(fileId="appDataFolder", fields="id").execute() 38 | print(f'Folder ID: {file.get("id")}') 39 | 40 | except HttpError as error: 41 | print(f"An error occurred: {error}") 42 | file = None 43 | 44 | return file.get("id") 45 | 46 | 47 | if __name__ == "__main__": 48 | fetch_appdata_folder() 49 | # [END drive_fetch_appdata_folder] 50 | -------------------------------------------------------------------------------- /drive/snippets/drive-v2/app data snippet/test_fetch_appdata_folder.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | import fetch_appdata_folder 19 | 20 | 21 | class TestFetchAppdataFolder(unittest.TestCase): 22 | """Unit test class for Appdata snippet""" 23 | 24 | @classmethod 25 | def test_list_appdata(cls): 26 | """Test list_appdata""" 27 | file_id = fetch_appdata_folder.fetch_appdata_folder() 28 | cls.assertIsNotNone(cls, file_id) 29 | 30 | 31 | if __name__ == "__main__": 32 | unittest.main() 33 | -------------------------------------------------------------------------------- /drive/snippets/drive-v2/app data snippet/test_list_appdata.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | import list_appdata 19 | 20 | 21 | class TestListAppdata(unittest.TestCase): 22 | """Unit test class for Appdata snippet""" 23 | 24 | @classmethod 25 | def test_list_appdata(cls): 26 | """Test list_appdata""" 27 | files = list_appdata.list_appdata() 28 | cls.assertNotEqual(cls, 0, len(files)) 29 | 30 | 31 | if __name__ == "__main__": 32 | unittest.main() 33 | -------------------------------------------------------------------------------- /drive/snippets/drive-v2/app data snippet/test_upload_appdata.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | import upload_app_data 19 | 20 | 21 | class TestUploadAppdata(unittest.TestCase): 22 | """ 23 | Unit test class for Appdata snippet 24 | """ 25 | 26 | @classmethod 27 | def test_upload_adddata(cls): 28 | """Test upload_appdata 29 | create a text file titled "abc.txt" in order to pass this test 30 | """ 31 | file_id = upload_app_data.upload_appdata() 32 | cls.assertIsNotNone(cls, file_id) 33 | 34 | 35 | if __name__ == "__main__": 36 | unittest.main() 37 | -------------------------------------------------------------------------------- /drive/snippets/drive-v2/change snippet/fetch_start_page_token.py: -------------------------------------------------------------------------------- 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 | 14 | # [START drive_fetch_start_page_token] 15 | import google.auth 16 | from googleapiclient.discovery import build 17 | from googleapiclient.errors import HttpError 18 | 19 | 20 | def fetch_start_page_token(): 21 | """Retrieve page token for the current state of the account. 22 | Returns & prints : start page token 23 | Load pre-authorized user credentials from the environment. 24 | TODO(developer) - See https://developers.google.com/identity 25 | for guides on implementing OAuth2 for the application. 26 | """ 27 | creds, _ = google.auth.default() 28 | 29 | try: 30 | # create drive api client 31 | service = build("drive", "v2", credentials=creds) 32 | 33 | # pylint: disable=maybe-no-member 34 | response = service.changes().getStartPageToken().execute() 35 | print(f'Start token: {response.get("startPageToken")}') 36 | 37 | except HttpError as error: 38 | print(f"An error occurred: {error}") 39 | response = None 40 | 41 | return response.get("startPageToken") 42 | 43 | 44 | if __name__ == "__main__": 45 | fetch_start_page_token() 46 | # [END drive_fetch_start_page_token] 47 | -------------------------------------------------------------------------------- /drive/snippets/drive-v2/change snippet/test_fetch_changes.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | import fetch_changes 19 | import fetch_start_page_token 20 | 21 | 22 | class TestFetchChanges(unittest.TestCase): 23 | """Unit test class for Change snippet""" 24 | 25 | @classmethod 26 | def test_fetch_changes(cls): 27 | """Test fetch_changes""" 28 | start_token = fetch_start_page_token.fetch_start_page_token() 29 | token = fetch_changes.fetch_changes(start_token) 30 | cls.assertIsNotNone(cls, token) 31 | 32 | 33 | if __name__ == "__main__": 34 | unittest.main() 35 | -------------------------------------------------------------------------------- /drive/snippets/drive-v2/change snippet/test_fetch_start_page_token.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | import fetch_start_page_token 19 | 20 | 21 | class TestFetchChanges(unittest.TestCase): 22 | """Unit test class for Change snippet""" 23 | 24 | @classmethod 25 | def test_fetch_start_page_token(cls): 26 | """Test fetch_start_page_token""" 27 | token = fetch_start_page_token.fetch_start_page_token() 28 | cls.assertIsNotNone(cls, token) 29 | 30 | 31 | if __name__ == "__main__": 32 | unittest.main() 33 | -------------------------------------------------------------------------------- /drive/snippets/drive-v2/drive_snippet/create_drive.py: -------------------------------------------------------------------------------- 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 | 17 | # [START drive_create_drive] 18 | import uuid 19 | 20 | import google.auth 21 | from googleapiclient.discovery import build 22 | from googleapiclient.errors import HttpError 23 | 24 | 25 | def create_drive(): 26 | """Create a drive. 27 | Returns: 28 | Id of the created drive 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 | # create drive api client 38 | service = build("drive", "v2", credentials=creds) 39 | 40 | drive_metadata = {"name": "Project Resources"} 41 | request_id = str(uuid.uuid4()) 42 | # pylint: disable=maybe-no-member 43 | drive = ( 44 | service.drives() 45 | .insert(body=drive_metadata, requestId=request_id, fields="id") 46 | .execute() 47 | ) 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] 60 | -------------------------------------------------------------------------------- /drive/snippets/drive-v2/file snippet/create_folder.py: -------------------------------------------------------------------------------- 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 | 17 | # [START drive_create_folder] 18 | import google.auth 19 | from googleapiclient.discovery import build 20 | from googleapiclient.errors import HttpError 21 | 22 | 23 | def create_folder(): 24 | """Create a folder and prints the folder ID 25 | Returns : Folder Id 26 | 27 | Load pre-authorized user credentials from the environment. 28 | TODO(developer) - See https://developers.google.com/identity 29 | for guides on implementing OAuth2 for the application. 30 | """ 31 | creds, _ = google.auth.default() 32 | 33 | try: 34 | # create drive api client 35 | service = build("drive", "v2", credentials=creds) 36 | file_metadata = { 37 | "title": "Invoices", 38 | "mimeType": "application/vnd.google-apps.folder", 39 | } 40 | 41 | # pylint: disable=maybe-no-member 42 | file = service.files().insert(body=file_metadata, fields="id").execute() 43 | print(f'Folder ID: "{file.get("id")}".') 44 | return file.get("id") 45 | 46 | except HttpError as error: 47 | print(f"An error occurred: {error}") 48 | return None 49 | 50 | 51 | if __name__ == "__main__": 52 | create_folder() 53 | # [END drive_create_folder] 54 | -------------------------------------------------------------------------------- /drive/snippets/drive-v2/file snippet/create_shortcut.py: -------------------------------------------------------------------------------- 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 | 17 | # [START drive_create_shortcut] 18 | import google.auth 19 | from googleapiclient.discovery import build 20 | from googleapiclient.errors import HttpError 21 | 22 | 23 | def create_shortcut(): 24 | """Create a third party shortcut 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 drive api client 34 | service = build("drive", "v2", credentials=creds) 35 | file_metadata = { 36 | "title": "Project plan", 37 | "mimeType": "application/vnd.google-apps.drive-sdk", 38 | } 39 | # pylint: disable=maybe-no-member 40 | file = service.files().insert(body=file_metadata, fields="id").execute() 41 | print(f'File ID: {file.get("id")}') 42 | 43 | except HttpError as error: 44 | print(f"An error occurred: {error}") 45 | return file.get("id") 46 | 47 | 48 | if __name__ == "__main__": 49 | create_shortcut() 50 | # [END drive_create_shortcut] 51 | -------------------------------------------------------------------------------- /drive/snippets/drive-v2/file snippet/test_create_folder.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | import create_folder 19 | 20 | 21 | class TestCreateFolder(unittest.TestCase): 22 | """Unit test class for file snippet""" 23 | 24 | @classmethod 25 | def test_create_folder(cls): 26 | """Test create_folder""" 27 | file_id = create_folder.create_folder() 28 | cls.assertIsNotNone(cls, file_id) 29 | 30 | 31 | if __name__ == "__main__": 32 | unittest.main() 33 | -------------------------------------------------------------------------------- /drive/snippets/drive-v2/file snippet/test_create_shortcut.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | import create_shortcut 19 | 20 | 21 | class TestCreateShortcut(unittest.TestCase): 22 | """Unit test class for file snippet""" 23 | 24 | @classmethod 25 | def test_create_shortcut(cls): 26 | """Test create_folder""" 27 | file_id = create_shortcut.create_shortcut() 28 | cls.assertIsNotNone(cls, file_id) 29 | 30 | 31 | if __name__ == "__main__": 32 | unittest.main() 33 | -------------------------------------------------------------------------------- /drive/snippets/drive-v2/file snippet/test_download_file.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | import download_file 19 | 20 | 21 | class TestDownloadFile(unittest.TestCase): 22 | """Unit test class for file snippet""" 23 | 24 | @classmethod 25 | def test_download_file(cls): 26 | """Test Download_file""" 27 | # valid file id 28 | real_file_id = "1KuPmvGq8yoYgbfW74OENMCB5H0n_2Jm9" 29 | file = download_file.download_file(real_file_id=real_file_id) 30 | cls.assertNotEqual(cls, 0, len(file)) 31 | 32 | 33 | if __name__ == "__main__": 34 | unittest.main() 35 | -------------------------------------------------------------------------------- /drive/snippets/drive-v2/file snippet/test_export_pdf.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | import export_pdf 19 | 20 | 21 | class TestExportPdf(unittest.TestCase): 22 | """Unit test class for file snippet""" 23 | 24 | @classmethod 25 | def test_export_pdf(cls): 26 | """Test export_pdf""" 27 | # valid file ID 28 | real_file_id = "1zbp8wAyuImX91Jt9mI-CAX_1TqkBLDEDcr2WeXBbKUY" 29 | file = export_pdf.export_pdf(real_file_id=real_file_id) 30 | cls.assertNotEqual(cls, 0, len(file)) 31 | 32 | 33 | if __name__ == "__main__": 34 | unittest.main() 35 | -------------------------------------------------------------------------------- /drive/snippets/drive-v2/file snippet/test_move_file_to_folder.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | import move_file_to_folder 19 | 20 | 21 | class TestMoveFileToFolder(unittest.TestCase): 22 | """Unit test class for file snippet""" 23 | 24 | @classmethod 25 | def test_move_file_to_folder(cls): 26 | """Test move_file_to_folder""" 27 | file_id = "1KuPmvGq8yoYgbfW74OENMCB5H0n_2Jm9" 28 | folder_id = "1v5eyIbXCr9TZX3eX_44HEExfe7yRj24V" 29 | 30 | update = move_file_to_folder.move_file_to_folder( 31 | file_id=file_id, folder_id=folder_id 32 | ) 33 | cls.assertIsNotNone(cls, 0, len(update)) 34 | 35 | 36 | if __name__ == "__main__": 37 | unittest.main() 38 | -------------------------------------------------------------------------------- /drive/snippets/drive-v2/file snippet/test_search_file.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | import search_file 19 | 20 | 21 | class TestSearchFile(unittest.TestCase): 22 | """Unit test class for file snippet""" 23 | 24 | @classmethod 25 | def test_search_file(cls): 26 | """Test search_file""" 27 | file = search_file.search_file() 28 | cls.assertNotEqual(cls, 0, len(file)) 29 | 30 | 31 | if __name__ == "__main__": 32 | unittest.main() 33 | -------------------------------------------------------------------------------- /drive/snippets/drive-v2/file snippet/test_share_file.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | import share_file 19 | 20 | 21 | class TestShareFile(unittest.TestCase): 22 | """Unit test class for file snippet""" 23 | 24 | @classmethod 25 | def test_share_file(cls): 26 | """Test share_file""" 27 | real_file_id = "1dUiRSoAQKkM3a4nTPeNQWgiuau1KdQ_l" 28 | real_user = "gduser1@workspacesamples.dev" 29 | real_domain = "workspacesamples.dev" 30 | file = share_file.share_file( 31 | real_file_id=real_file_id, 32 | real_user=real_user, 33 | real_domain=real_domain, 34 | ) 35 | cls.assertNotEqual(cls, 0, len(file)) 36 | 37 | 38 | if __name__ == "__main__": 39 | unittest.main() 40 | -------------------------------------------------------------------------------- /drive/snippets/drive-v2/file snippet/test_touch_file.py: -------------------------------------------------------------------------------- 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 | import re 17 | import unittest 18 | from datetime import datetime 19 | 20 | import touch_file 21 | 22 | 23 | class TestTouchFile(unittest.TestCase): 24 | """Unit test class for file snippet""" 25 | 26 | @classmethod 27 | def test_touch_file(cls): 28 | """Test touch_file""" 29 | real_file_id = "1KuPmvGq8yoYgbfW74OENMCB5H0n_2Jm9" 30 | now = datetime.utcnow().isoformat() + "Z" 31 | now = re.sub(r"\d{3}Z", "Z", now) # Truncate microseconds 32 | modified_time = touch_file.touch_file( 33 | real_file_id=real_file_id, real_timestamp=now 34 | ) 35 | cls.assertIsNotNone(cls, modified_time) 36 | 37 | 38 | if __name__ == "__main__": 39 | unittest.main() 40 | -------------------------------------------------------------------------------- /drive/snippets/drive-v2/file snippet/test_upload_basic.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | import upload_basic 19 | 20 | 21 | class TestUploadBasic(unittest.TestCase): 22 | """Unit test class for file snippet""" 23 | 24 | @classmethod 25 | def test_upload_basic(cls): 26 | """Test upload_basic""" 27 | file_id = upload_basic.upload_basic() 28 | cls.assertIsNotNone(cls, file_id) 29 | 30 | 31 | if __name__ == "__main__": 32 | unittest.main() 33 | -------------------------------------------------------------------------------- /drive/snippets/drive-v2/file snippet/test_upload_revision.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | import upload_revision 19 | 20 | 21 | class TestUploadRevision(unittest.TestCase): 22 | """Unit test class for file snippet""" 23 | 24 | @classmethod 25 | def test_upload_revision(cls): 26 | """Test upload_revision""" 27 | 28 | real_file_id = "1KuPmvGq8yoYgbfW74OENMCB5H0n_2Jm9" 29 | file_id = upload_revision.upload_revision(real_file_id=real_file_id) 30 | cls.assertIsNotNone(cls, file_id) 31 | 32 | 33 | if __name__ == "__main__": 34 | unittest.main() 35 | -------------------------------------------------------------------------------- /drive/snippets/drive-v2/file snippet/test_upload_to_folder.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | import upload_to_folder 19 | 20 | 21 | class TestUploadToFolder(unittest.TestCase): 22 | """Unit test class for file snippet""" 23 | 24 | @classmethod 25 | def test_upload_to_folder(cls): 26 | """Test upload_to_folder""" 27 | folder_id = "1s0oKEZZXjImNngxHGnY0xed6Mw-tvspu" 28 | file_id = upload_to_folder.upload_to_folder(folder_id=folder_id) 29 | cls.assertIsNotNone(cls, file_id) 30 | 31 | 32 | if __name__ == "__main__": 33 | unittest.main() 34 | -------------------------------------------------------------------------------- /drive/snippets/drive-v2/file snippet/test_upload_with_conversion.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | import upload_with_conversion 19 | 20 | 21 | class TestUploadWithConversion(unittest.TestCase): 22 | """Unit test class for file snippet""" 23 | 24 | @classmethod 25 | def test_upload_to_folder(cls): 26 | """Test upload_with_conversion""" 27 | file_id = upload_with_conversion.upload_with_conversion() 28 | cls.assertIsNotNone(cls, file_id) 29 | 30 | 31 | if __name__ == "__main__": 32 | unittest.main() 33 | -------------------------------------------------------------------------------- /drive/snippets/drive-v2/file snippet/upload_basic.py: -------------------------------------------------------------------------------- 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 | 17 | # [START drive_upload_basic] 18 | import google.auth 19 | from googleapiclient.discovery import build 20 | from googleapiclient.errors import HttpError 21 | from googleapiclient.http import MediaFileUpload 22 | 23 | 24 | def upload_basic(): 25 | """Insert new file. 26 | Returns : Id's of the file uploaded 27 | 28 | Load pre-authorized user credentials from the environment. 29 | TODO(developer) - See https://developers.google.com/identity 30 | for guides on implementing OAuth2 for the application. 31 | """ 32 | creds, _ = google.auth.default() 33 | 34 | try: 35 | # create drive api client 36 | service = build("drive", "v2", credentials=creds) 37 | 38 | file_metadata = {"title": "photo.jpg"} 39 | media = MediaFileUpload("photo.jpg", mimetype="image/jpeg") 40 | # pylint: disable=maybe-no-member 41 | file = ( 42 | service.files() 43 | .insert(body=file_metadata, media_body=media, fields="id") 44 | .execute() 45 | ) 46 | print(f'File ID: {file.get("id")}') 47 | 48 | except HttpError as error: 49 | print(f"An error occurred: {error}") 50 | file = None 51 | 52 | return file.get("id") 53 | 54 | 55 | if __name__ == "__main__": 56 | upload_basic() 57 | # [END drive_upload_basic] 58 | -------------------------------------------------------------------------------- /drive/snippets/drive-v2/team_drive_snippets/create_team_drive.py: -------------------------------------------------------------------------------- 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_create_team_drive] 17 | import uuid 18 | 19 | import google.auth 20 | from googleapiclient.discovery import build 21 | from googleapiclient.errors import HttpError 22 | 23 | 24 | def create_team_drive(): 25 | """Create a drive for team. 26 | Returns: ID of the created drive 27 | 28 | Load pre-authorized user credentials from the environment. 29 | TODO(developer) - See https://developers.google.com/identity 30 | for guides on implementing OAuth2 for the application. 31 | """ 32 | creds, _ = google.auth.default() 33 | 34 | try: 35 | # call drive api client 36 | service = build("drive", "v2", credentials=creds) 37 | 38 | # pylint: disable=maybe-no-member 39 | team_drive_metadata = {"name": "Project Resources"} 40 | request_id = str(uuid.uuid4()) 41 | team_drive = ( 42 | service.teamdrives() 43 | .insert(body=team_drive_metadata, requestId=request_id, fields="id") 44 | .execute() 45 | ) 46 | print(f'Team Drive ID: {team_drive.get("id")}') 47 | 48 | except HttpError as error: 49 | print(f"An error occurred: {error}") 50 | team_drive = None 51 | 52 | return team_drive.get("id") 53 | 54 | 55 | if __name__ == "__main__": 56 | create_team_drive() 57 | # [END drive_create_team_drive] 58 | -------------------------------------------------------------------------------- /drive/snippets/drive-v3/app_data_snippet/fetch_appdata_folder.py: -------------------------------------------------------------------------------- 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 | import google.auth 18 | from googleapiclient.discovery import build 19 | from googleapiclient.errors import HttpError 20 | 21 | 22 | def fetch_appdata_folder(): 23 | """List out application data folder and prints folder ID. 24 | Returns : Folder ID 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 | # call drive api client 34 | service = build("drive", "v3", credentials=creds) 35 | 36 | # pylint: disable=maybe-no-member 37 | file = service.files().get(fileId="appDataFolder", fields="id").execute() 38 | print(f'Folder ID: {file.get("id")}') 39 | 40 | except HttpError as error: 41 | print(f"An error occurred: {error}") 42 | file = None 43 | 44 | return file.get("id") 45 | 46 | 47 | if __name__ == "__main__": 48 | fetch_appdata_folder() 49 | # [END drive_fetch_appdata_folder] 50 | -------------------------------------------------------------------------------- /drive/snippets/drive-v3/app_data_snippet/test_fetch_appdata_folder.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | import fetch_appdata_folder 19 | 20 | 21 | class TestFetchAppdataFolder(unittest.TestCase): 22 | """Unit test class for Appdata snippet""" 23 | 24 | @classmethod 25 | def test_list_appdata(cls): 26 | """Test list_appdata""" 27 | file_id = fetch_appdata_folder.fetch_appdata_folder() 28 | cls.assertIsNotNone(cls, file_id) 29 | 30 | 31 | if __name__ == "__main__": 32 | unittest.main() 33 | -------------------------------------------------------------------------------- /drive/snippets/drive-v3/app_data_snippet/test_list_appdata.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | import list_appdata 19 | 20 | 21 | class TestListAppdata(unittest.TestCase): 22 | """Unit test class for Appdata snippet""" 23 | 24 | @classmethod 25 | def test_list_appdata(cls): 26 | """Test list_appdata""" 27 | files = list_appdata.list_appdata() 28 | cls.assertNotEqual(cls, 0, len(files)) 29 | 30 | 31 | if __name__ == "__main__": 32 | unittest.main() 33 | -------------------------------------------------------------------------------- /drive/snippets/drive-v3/app_data_snippet/test_upload_appdata.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | import upload_app_data 19 | 20 | 21 | class TestUploadAppdata(unittest.TestCase): 22 | """ 23 | Unit test class for Appdata snippet 24 | """ 25 | 26 | @classmethod 27 | def test_upload_adddata(cls): 28 | """Test upload_appdata 29 | create a text file titled "abc.txt" in order to pass this test 30 | """ 31 | file_id = upload_app_data.upload_appdata() 32 | cls.assertIsNotNone(cls, file_id) 33 | 34 | 35 | if __name__ == "__main__": 36 | unittest.main() 37 | -------------------------------------------------------------------------------- /drive/snippets/drive-v3/change_snippet/fetch_start_page_token.py: -------------------------------------------------------------------------------- 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 | 14 | # [START drive_fetch_start_page_token] 15 | import google.auth 16 | from googleapiclient.discovery import build 17 | from googleapiclient.errors import HttpError 18 | 19 | 20 | def fetch_start_page_token(): 21 | """Retrieve page token for the current state of the account. 22 | Returns & prints : start page token 23 | 24 | Load pre-authorized user credentials from the environment. 25 | TODO(developer) - See https://developers.google.com/identity 26 | for guides on implementing OAuth2 for the application. 27 | """ 28 | creds, _ = google.auth.default() 29 | 30 | try: 31 | # create drive api client 32 | service = build("drive", "v3", credentials=creds) 33 | 34 | # pylint: disable=maybe-no-member 35 | response = service.changes().getStartPageToken().execute() 36 | print(f'Start token: {response.get("startPageToken")}') 37 | 38 | except HttpError as error: 39 | print(f"An error occurred: {error}") 40 | response = None 41 | 42 | return response.get("startPageToken") 43 | 44 | 45 | if __name__ == "__main__": 46 | fetch_start_page_token() 47 | # [END drive_fetch_start_page_token] 48 | -------------------------------------------------------------------------------- /drive/snippets/drive-v3/change_snippet/test_fetch_changes.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | import fetch_changes 19 | import fetch_start_page_token 20 | 21 | 22 | class TestFetchChanges(unittest.TestCase): 23 | """Unit test class for Change snippet""" 24 | 25 | @classmethod 26 | def test_fetch_changes(cls): 27 | """Test fetch_changes""" 28 | start_token = fetch_start_page_token.fetch_start_page_token() 29 | token = fetch_changes.fetch_changes(start_token) 30 | cls.assertIsNotNone(cls, token) 31 | 32 | 33 | if __name__ == "__main__": 34 | unittest.main() 35 | -------------------------------------------------------------------------------- /drive/snippets/drive-v3/change_snippet/test_fetch_start_page_token.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | import fetch_start_page_token 19 | 20 | 21 | class TestFetchChanges(unittest.TestCase): 22 | """Unit test class for Change snippet""" 23 | 24 | @classmethod 25 | def test_fetch_start_page_token(cls): 26 | """Test fetch_start_page_token""" 27 | token = fetch_start_page_token.fetch_start_page_token() 28 | cls.assertIsNotNone(cls, token) 29 | 30 | 31 | if __name__ == "__main__": 32 | unittest.main() 33 | -------------------------------------------------------------------------------- /drive/snippets/drive-v3/drive_snippet/create_drive.py: -------------------------------------------------------------------------------- 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 | 17 | # [START drive_create_drive] 18 | import uuid 19 | 20 | import google.auth 21 | from googleapiclient.discovery import build 22 | from googleapiclient.errors import HttpError 23 | 24 | 25 | def create_drive(): 26 | """Create a drive. 27 | Returns: 28 | Id of the created drive 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 | # create drive api client 38 | service = build("drive", "v3", credentials=creds) 39 | 40 | drive_metadata = {"name": "Project Resources"} 41 | request_id = str(uuid.uuid4()) 42 | # pylint: disable=maybe-no-member 43 | drive = ( 44 | service.drives() 45 | .create(body=drive_metadata, requestId=request_id, fields="id") 46 | .execute() 47 | ) 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] 60 | -------------------------------------------------------------------------------- /drive/snippets/drive-v3/file_snippet/create_folder.py: -------------------------------------------------------------------------------- 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 | 17 | # [START drive_create_folder] 18 | import google.auth 19 | from googleapiclient.discovery import build 20 | from googleapiclient.errors import HttpError 21 | 22 | 23 | def create_folder(): 24 | """Create a folder and prints the folder ID 25 | Returns : Folder Id 26 | 27 | Load pre-authorized user credentials from the environment. 28 | TODO(developer) - See https://developers.google.com/identity 29 | for guides on implementing OAuth2 for the application. 30 | """ 31 | creds, _ = google.auth.default() 32 | 33 | try: 34 | # create drive api client 35 | service = build("drive", "v3", credentials=creds) 36 | file_metadata = { 37 | "name": "Invoices", 38 | "mimeType": "application/vnd.google-apps.folder", 39 | } 40 | 41 | # pylint: disable=maybe-no-member 42 | file = service.files().create(body=file_metadata, fields="id").execute() 43 | print(f'Folder ID: "{file.get("id")}".') 44 | return file.get("id") 45 | 46 | except HttpError as error: 47 | print(f"An error occurred: {error}") 48 | return None 49 | 50 | 51 | if __name__ == "__main__": 52 | create_folder() 53 | # [END drive_create_folder] 54 | -------------------------------------------------------------------------------- /drive/snippets/drive-v3/file_snippet/create_shortcut.py: -------------------------------------------------------------------------------- 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 | 17 | # [START drive_create_shortcut] 18 | import google.auth 19 | from googleapiclient.discovery import build 20 | from googleapiclient.errors import HttpError 21 | 22 | 23 | def create_shortcut(): 24 | """Create a third party shortcut 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 drive api client 34 | service = build("drive", "v3", credentials=creds) 35 | file_metadata = { 36 | "name": "Project plan", 37 | "mimeType": "application/vnd.google-apps.drive-sdk", 38 | } 39 | 40 | # pylint: disable=maybe-no-member 41 | file = service.files().create(body=file_metadata, fields="id").execute() 42 | print(f'File ID: {file.get("id")}') 43 | 44 | except HttpError as error: 45 | print(f"An error occurred: {error}") 46 | return file.get("id") 47 | 48 | 49 | if __name__ == "__main__": 50 | create_shortcut() 51 | # [END drive_create_shortcut] 52 | -------------------------------------------------------------------------------- /drive/snippets/drive-v3/file_snippet/test_create_folder.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | import create_folder 19 | 20 | 21 | class TestCreateFolder(unittest.TestCase): 22 | """Unit test class for file snippet""" 23 | 24 | @classmethod 25 | def test_create_folder(cls): 26 | """Test create_folder""" 27 | file_id = create_folder.create_folder() 28 | cls.assertIsNotNone(cls, file_id) 29 | 30 | 31 | if __name__ == "__main__": 32 | unittest.main() 33 | -------------------------------------------------------------------------------- /drive/snippets/drive-v3/file_snippet/test_create_shortcut.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | import create_shortcut 19 | 20 | 21 | class TestCreateShortcut(unittest.TestCase): 22 | """Unit test class for file snippet""" 23 | 24 | @classmethod 25 | def test_create_shortcut(cls): 26 | """Test create_folder""" 27 | file_id = create_shortcut.create_shortcut() 28 | cls.assertIsNotNone(cls, file_id) 29 | 30 | 31 | if __name__ == "__main__": 32 | unittest.main() 33 | -------------------------------------------------------------------------------- /drive/snippets/drive-v3/file_snippet/test_download_file.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | import download_file 19 | 20 | 21 | class TestDownloadFile(unittest.TestCase): 22 | """Unit test class for file snippet""" 23 | 24 | @classmethod 25 | def test_download_file(cls): 26 | """Test Download_file""" 27 | # valid file id 28 | real_file_id = "1KuPmvGq8yoYgbfW74OENMCB5H0n_2Jm9" 29 | file = download_file.download_file(real_file_id=real_file_id) 30 | cls.assertNotEqual(cls, 0, len(file)) 31 | 32 | 33 | if __name__ == "__main__": 34 | unittest.main() 35 | -------------------------------------------------------------------------------- /drive/snippets/drive-v3/file_snippet/test_export_pdf.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | import export_pdf 19 | 20 | 21 | class TestExportPdf(unittest.TestCase): 22 | """Unit test class for file snippet""" 23 | 24 | @classmethod 25 | def test_export_pdf(cls): 26 | """Test export_pdf""" 27 | # valid file ID 28 | real_file_id = "1zbp8wAyuImX91Jt9mI-CAX_1TqkBLDEDcr2WeXBbKUY" 29 | file = export_pdf.export_pdf(real_file_id=real_file_id) 30 | cls.assertNotEqual(cls, 0, len(file)) 31 | 32 | 33 | if __name__ == "__main__": 34 | unittest.main() 35 | -------------------------------------------------------------------------------- /drive/snippets/drive-v3/file_snippet/test_move_file_to_folder.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | import move_file_to_folder 19 | 20 | 21 | class TestMoveFileToFolder(unittest.TestCase): 22 | """Unit test class for file snippet""" 23 | 24 | @classmethod 25 | def test_move_file_to_folder(cls): 26 | """Test move_file_to_folder""" 27 | file_id = "1KuPmvGq8yoYgbfW74OENMCB5H0n_2Jm9" 28 | folder_id = "1v5eyIbXCr9TZX3eX_44HEExfe7yRj24V" 29 | 30 | update = move_file_to_folder.move_file_to_folder( 31 | file_id=file_id, folder_id=folder_id 32 | ) 33 | cls.assertIsNotNone(cls, 0, len(update)) 34 | 35 | 36 | if __name__ == "__main__": 37 | unittest.main() 38 | -------------------------------------------------------------------------------- /drive/snippets/drive-v3/file_snippet/test_search_file.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | import search_file 19 | 20 | 21 | class TestSearchFile(unittest.TestCase): 22 | """Unit test class for file snippet""" 23 | 24 | @classmethod 25 | def test_search_file(cls): 26 | """Test search_file""" 27 | file = search_file.search_file() 28 | cls.assertNotEqual(cls, 0, len(file)) 29 | 30 | 31 | if __name__ == "__main__": 32 | unittest.main() 33 | -------------------------------------------------------------------------------- /drive/snippets/drive-v3/file_snippet/test_share_file.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | import share_file 19 | 20 | 21 | class TestShareFile(unittest.TestCase): 22 | """Unit test class for file snippet""" 23 | 24 | @classmethod 25 | def test_share_file(cls): 26 | """Test share_file""" 27 | real_file_id = "1dUiRSoAQKkM3a4nTPeNQWgiuau1KdQ_l" 28 | real_user = "gduser1@workspacesamples.dev" 29 | real_domain = "workspacesamples.dev" 30 | file = share_file.share_file( 31 | real_file_id=real_file_id, 32 | real_user=real_user, 33 | real_domain=real_domain, 34 | ) 35 | cls.assertNotEqual(cls, 0, len(file)) 36 | 37 | 38 | if __name__ == "__main__": 39 | unittest.main() 40 | -------------------------------------------------------------------------------- /drive/snippets/drive-v3/file_snippet/test_touch_file.py: -------------------------------------------------------------------------------- 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 | import re 17 | import unittest 18 | from datetime import datetime 19 | 20 | import touch_file 21 | 22 | 23 | class TestTouchFile(unittest.TestCase): 24 | """Unit test class for file snippet""" 25 | 26 | @classmethod 27 | def test_touch_file(cls): 28 | """Test touch_file""" 29 | real_file_id = "1KuPmvGq8yoYgbfW74OENMCB5H0n_2Jm9" 30 | now = datetime.utcnow().isoformat() + "Z" 31 | now = re.sub(r"\d{3}Z", "Z", now) # Truncate microseconds 32 | modified_time = touch_file.touch_file( 33 | real_file_id=real_file_id, real_timestamp=now 34 | ) 35 | cls.assertIsNotNone(cls, modified_time) 36 | 37 | 38 | if __name__ == "__main__": 39 | unittest.main() 40 | -------------------------------------------------------------------------------- /drive/snippets/drive-v3/file_snippet/test_upload_basic.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | import upload_basic 19 | 20 | 21 | class TestUploadBasic(unittest.TestCase): 22 | """Unit test class for file snippet""" 23 | 24 | @classmethod 25 | def test_upload_basic(cls): 26 | """Test upload_basic""" 27 | file_id = upload_basic.upload_basic() 28 | cls.assertIsNotNone(cls, file_id) 29 | 30 | 31 | if __name__ == "__main__": 32 | unittest.main() 33 | -------------------------------------------------------------------------------- /drive/snippets/drive-v3/file_snippet/test_upload_revision.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | import upload_revision 19 | 20 | 21 | class TestUploadRevision(unittest.TestCase): 22 | """Unit test class for file snippet""" 23 | 24 | @classmethod 25 | def test_upload_revision(cls): 26 | """Test upload_revision""" 27 | 28 | real_file_id = "1KuPmvGq8yoYgbfW74OENMCB5H0n_2Jm9" 29 | file_id = upload_revision.upload_revision(real_file_id=real_file_id) 30 | cls.assertIsNotNone(cls, file_id) 31 | 32 | 33 | if __name__ == "__main__": 34 | unittest.main() 35 | -------------------------------------------------------------------------------- /drive/snippets/drive-v3/file_snippet/test_upload_to_folder.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | import upload_to_folder 19 | 20 | 21 | class TestUploadToFolder(unittest.TestCase): 22 | """Unit test class for file snippet""" 23 | 24 | @classmethod 25 | def test_upload_to_folder(cls): 26 | """Test upload_to_folder""" 27 | folder_id = "1s0oKEZZXjImNngxHGnY0xed6Mw-tvspu" 28 | file_id = upload_to_folder.upload_to_folder(folder_id=folder_id) 29 | cls.assertIsNotNone(cls, file_id) 30 | 31 | 32 | if __name__ == "__main__": 33 | unittest.main() 34 | -------------------------------------------------------------------------------- /drive/snippets/drive-v3/file_snippet/test_upload_with_conversion.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | import upload_with_conversion 19 | 20 | 21 | class TestUploadWithConversion(unittest.TestCase): 22 | """Unit test class for file snippet""" 23 | 24 | @classmethod 25 | def test_upload_to_folder(cls): 26 | """Test upload_with_conversion""" 27 | file_id = upload_with_conversion.upload_with_conversion() 28 | cls.assertIsNotNone(cls, file_id) 29 | 30 | 31 | if __name__ == "__main__": 32 | unittest.main() 33 | -------------------------------------------------------------------------------- /drive/snippets/drive-v3/file_snippet/upload_basic.py: -------------------------------------------------------------------------------- 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 | 17 | # [START drive_upload_basic] 18 | import google.auth 19 | from googleapiclient.discovery import build 20 | from googleapiclient.errors import HttpError 21 | from googleapiclient.http import MediaFileUpload 22 | 23 | 24 | def upload_basic(): 25 | """Insert new file. 26 | Returns : Id's of the file uploaded 27 | 28 | Load pre-authorized user credentials from the environment. 29 | TODO(developer) - See https://developers.google.com/identity 30 | for guides on implementing OAuth2 for the application. 31 | """ 32 | creds, _ = google.auth.default() 33 | 34 | try: 35 | # create drive api client 36 | service = build("drive", "v3", credentials=creds) 37 | 38 | file_metadata = {"name": "download.jpeg"} 39 | media = MediaFileUpload("download.jpeg", mimetype="image/jpeg") 40 | # pylint: disable=maybe-no-member 41 | file = ( 42 | service.files() 43 | .create(body=file_metadata, media_body=media, fields="id") 44 | .execute() 45 | ) 46 | print(f'File ID: {file.get("id")}') 47 | 48 | except HttpError as error: 49 | print(f"An error occurred: {error}") 50 | file = None 51 | 52 | return file.get("id") 53 | 54 | 55 | if __name__ == "__main__": 56 | upload_basic() 57 | # [END drive_upload_basic] 58 | -------------------------------------------------------------------------------- /drive/snippets/drive-v3/team_drive_snippets/create_team_drive.py: -------------------------------------------------------------------------------- 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_create_team_drive] 17 | import uuid 18 | 19 | import google.auth 20 | from googleapiclient.discovery import build 21 | from googleapiclient.errors import HttpError 22 | 23 | 24 | def create_team_drive(): 25 | """Create a drive for team. 26 | Returns: ID of the created drive 27 | 28 | Load pre-authorized user credentials from the environment. 29 | TODO(developer) - See https://developers.google.com/identity 30 | for guides on implementing OAuth2 for the application. 31 | """ 32 | creds, _ = google.auth.default() 33 | 34 | try: 35 | # call drive api client 36 | service = build("drive", "v3", credentials=creds) 37 | 38 | # pylint: disable=maybe-no-member 39 | team_drive_metadata = {"name": "Project Resources"} 40 | request_id = str(uuid.uuid4()) 41 | team_drive = ( 42 | service.teamdrives() 43 | .create(body=team_drive_metadata, requestId=request_id, fields="id") 44 | .execute() 45 | ) 46 | print(f'Team Drive ID: {team_drive.get("id")}') 47 | 48 | except HttpError as error: 49 | print(f"An error occurred: {error}") 50 | team_drive = None 51 | 52 | return team_drive.get("id") 53 | 54 | 55 | if __name__ == "__main__": 56 | create_team_drive() 57 | # [END drive_create_team_drive] 58 | -------------------------------------------------------------------------------- /events/next18/README.md: -------------------------------------------------------------------------------- 1 | # Quarterly business review demo 2 | 3 | This sample was created for a talk for Google Cloud NEXT'18 entitled "Building 4 | on the Docs Editors: APIs and Apps Script". It is the implementation of a 5 | command-line tool that: 6 | 7 | * Extracts template variables out of a Google Slides template presentation 8 | * Writes those variables to a Google Sheets spreadsheet 9 | * Adds data to the spreadsheet based on those variables from a stub data service 10 | * Generates new Google Slides presentations using the template and the 11 | spreadsheet data 12 | 13 | ## Getting started 14 | 15 | * Follow the [Sheets API python quickstart](https://developers.google.com/sheets/api/quickstart/python) 16 | * Make sure to save the `credentials.json` file in your working directory 17 | * Enable the Google Slides API, Google Drive API and Google Sheets API in your 18 | developer project 19 | * Run the tool with no arguments to complete the OAuth consent flow: 20 | 21 | ```bash 22 | python qbr_tool.py 23 | ``` 24 | 25 | * Run the tool: 26 | 27 | 28 | ```bash 29 | # Create the spreadsheet from the Google Slides template. 30 | # For example, 13My9SxkotWssCc2F5yaXp2fzGrzoYV6maytr3qAT9GQ 31 | python qbr_tool.py create_sheet --template_id ; 32 | 33 | # Outputs: 34 | # Spreadsheet URL: https://docs.google.com/spreadsheets/d/ 35 | 36 | # Add data from the stub customer service 37 | python qbr_tool.py add_customers \ 38 | --spreadsheet_id \ 39 | --customer_id jupiter 40 | 41 | # Generate the filled in presentation 42 | python qbr_tool.py create_presentations 43 | --spreadsheet_id \ 44 | --customer_id jupiter 45 | 46 | # Outputs: 47 | # jupiter: https://docs.google.com/presentations/d/ 48 | ``` 49 | 50 | -------------------------------------------------------------------------------- /events/next18/presentation_writer.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018 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 | # pylint: disable=E1102 16 | # python3 17 | """Functionality for writing to a presentation.""" 18 | 19 | 20 | class PresentationWriter(object): 21 | """Queues writes for modifying a presentation. 22 | 23 | Call ExecuteBatchUpdate to flush pending writes. 24 | """ 25 | 26 | def __init__(self, slides_service, presentation_id): 27 | self._slides_service = slides_service 28 | self._presentation_id = presentation_id 29 | self._requests = [] 30 | 31 | def ReplaceAllText(self, find_text, replace_text): 32 | request = { 33 | "replaceAllText": { 34 | "replaceText": replace_text, 35 | "containsText": {"text": find_text, "matchCase": True}, 36 | } 37 | } 38 | self._requests.append(request) 39 | 40 | def ReplaceAllShapesWithImage(self, find_text, image_url): 41 | request = { 42 | "replaceAllShapesWithImage": { 43 | "imageUrl": image_url, 44 | "containsText": {"text": find_text, "matchCase": True}, 45 | } 46 | } 47 | self._requests.append(request) 48 | 49 | def ExecuteBatchUpdate(self): 50 | body = {"requests": self._requests} 51 | self._requests = [] 52 | self._slides_service.presentations().batchUpdate( 53 | presentationId=self._presentation_id, body=body 54 | ).execute() 55 | -------------------------------------------------------------------------------- /forms/quickstart/README.md: -------------------------------------------------------------------------------- 1 | # Google Forms Python Quickstart 2 | 3 | Complete the steps described in the [Google Forms Python Quickstart]( 4 | https://developers.google.com/forms/quickstart/python), and in 5 | about five minutes you'll have a simple Python command-line application that 6 | makes requests to the Google Forms API. 7 | 8 | ## Install 9 | 10 | ```shell 11 | pip install -r requirements.txt 12 | ``` 13 | 14 | ## Run 15 | 16 | ```shell 17 | python quickstart.py 18 | ``` 19 | -------------------------------------------------------------------------------- /forms/quickstart/requirements.txt: -------------------------------------------------------------------------------- 1 | google-api-python-client==1.7.9 2 | google-auth-httplib2==0.0.3 3 | google-auth-oauthlib==0.4.0 -------------------------------------------------------------------------------- /forms/snippets/create_form.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 | # https://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 | # [START forms_create_form] 16 | from apiclient import discovery 17 | from httplib2 import Http 18 | from oauth2client import client, file, tools 19 | 20 | SCOPES = "https://www.googleapis.com/auth/drive" 21 | DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1" 22 | 23 | store = file.Storage("token.json") 24 | creds = None 25 | if not creds or creds.invalid: 26 | flow = client.flow_from_clientsecrets("client_secrets.json", SCOPES) 27 | creds = tools.run_flow(flow, store) 28 | 29 | form_service = discovery.build( 30 | "forms", 31 | "v1", 32 | http=creds.authorize(Http()), 33 | discoveryServiceUrl=DISCOVERY_DOC, 34 | static_discovery=False, 35 | ) 36 | 37 | form = { 38 | "info": { 39 | "title": "My new form", 40 | }, 41 | } 42 | # Prints the details of the sample form 43 | result = form_service.forms().create(body=form).execute() 44 | print(result) 45 | # [END forms_create_form] 46 | -------------------------------------------------------------------------------- /forms/snippets/create_watch.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 | # https://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 | # [START forms_create_watch] 16 | from apiclient import discovery 17 | from httplib2 import Http 18 | from oauth2client import client, file, tools 19 | 20 | SCOPES = "https://www.googleapis.com/auth/drive" 21 | DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1" 22 | 23 | store = file.Storage("token.json") 24 | creds = None 25 | if not creds or creds.invalid: 26 | flow = client.flow_from_clientsecrets("client_secret.json", SCOPES) 27 | creds = tools.run_flow(flow, store) 28 | 29 | service = discovery.build( 30 | "forms", 31 | "v1", 32 | http=creds.authorize(Http()), 33 | discoveryServiceUrl=DISCOVERY_DOC, 34 | static_discovery=False, 35 | ) 36 | 37 | watch = { 38 | "watch": { 39 | "target": {"topic": {"topicName": ""}}, 40 | "eventType": "RESPONSES", 41 | } 42 | } 43 | 44 | form_id = "" 45 | 46 | # Print JSON response after form watch creation 47 | result = service.forms().watches().create(formId=form_id, body=watch).execute() 48 | print(result) 49 | # [END forms_create_watch] 50 | -------------------------------------------------------------------------------- /forms/snippets/delete_watch.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 | # https://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 | # [START forms_delete_watch] 16 | from apiclient import discovery 17 | from httplib2 import Http 18 | from oauth2client import client, file, tools 19 | 20 | SCOPES = "https://www.googleapis.com/auth/drive" 21 | DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1" 22 | 23 | store = file.Storage("token.json") 24 | creds = None 25 | if not creds or creds.invalid: 26 | flow = client.flow_from_clientsecrets("client_secret.json", SCOPES) 27 | creds = tools.run_flow(flow, store) 28 | service = discovery.build( 29 | "forms", 30 | "v1", 31 | http=creds.authorize(Http()), 32 | discoveryServiceUrl=DISCOVERY_DOC, 33 | static_discovery=False, 34 | ) 35 | 36 | form_id = "" 37 | watch_id = "" 38 | 39 | # Print JSON response after deleting a form watch 40 | result = ( 41 | service.forms().watches().delete(formId=form_id, watchId=watch_id).execute() 42 | ) 43 | print(result) 44 | # [END forms_delete_watch] 45 | -------------------------------------------------------------------------------- /forms/snippets/list_watches.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 | # https://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 | # [START forms_list_form_watches] 16 | from apiclient import discovery 17 | from httplib2 import Http 18 | from oauth2client import client, file, tools 19 | 20 | SCOPES = "https://www.googleapis.com/auth/drive" 21 | DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1" 22 | 23 | store = file.Storage("token.json") 24 | creds = None 25 | if not creds or creds.invalid: 26 | flow = client.flow_from_clientsecrets("client_secrets.json", SCOPES) 27 | creds = tools.run_flow(flow, store) 28 | service = discovery.build( 29 | "forms", 30 | "v1", 31 | http=creds.authorize(Http()), 32 | discoveryServiceUrl=DISCOVERY_DOC, 33 | static_discovery=False, 34 | ) 35 | 36 | form_id = "" 37 | 38 | # Print JSON list of form watches 39 | result = service.forms().watches().list(formId=form_id).execute() 40 | print(result) 41 | # [END forms_list_form_watches] 42 | -------------------------------------------------------------------------------- /forms/snippets/renew_watch.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 | # https://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 | # [START forms_renew_watch] 16 | from apiclient import discovery 17 | from httplib2 import Http 18 | from oauth2client import client, file, tools 19 | 20 | SCOPES = "https://www.googleapis.com/auth/drive" 21 | DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1" 22 | 23 | store = file.Storage("token.json") 24 | creds = None 25 | if not creds or creds.invalid: 26 | flow = client.flow_from_clientsecrets("client_secrets.json", SCOPES) 27 | creds = tools.run_flow(flow, store) 28 | service = discovery.build( 29 | "forms", 30 | "v1", 31 | http=creds.authorize(Http()), 32 | discoveryServiceUrl=DISCOVERY_DOC, 33 | static_discovery=False, 34 | ) 35 | 36 | form_id = "" 37 | watch_id = "" 38 | 39 | # Print JSON response after renewing a form watch 40 | result = ( 41 | service.forms().watches().renew(formId=form_id, watchId=watch_id).execute() 42 | ) 43 | print(result) 44 | # [END forms_renew_watch] 45 | -------------------------------------------------------------------------------- /forms/snippets/retrieve_all_responses.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 | # https://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 | # [START forms_retrieve_all_responses] 16 | from apiclient import discovery 17 | from httplib2 import Http 18 | from oauth2client import client, file, tools 19 | 20 | SCOPES = "https://www.googleapis.com/auth/forms.responses.readonly" 21 | DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1" 22 | 23 | store = file.Storage("token.json") 24 | creds = None 25 | if not creds or creds.invalid: 26 | flow = client.flow_from_clientsecrets("client_secrets.json", SCOPES) 27 | creds = tools.run_flow(flow, store) 28 | service = discovery.build( 29 | "forms", 30 | "v1", 31 | http=creds.authorize(Http()), 32 | discoveryServiceUrl=DISCOVERY_DOC, 33 | static_discovery=False, 34 | ) 35 | 36 | # Prints the responses of your specified form: 37 | form_id = "" 38 | result = service.forms().responses().list(formId=form_id).execute() 39 | print(result) 40 | # [END forms_retrieve_all_responses] 41 | -------------------------------------------------------------------------------- /forms/snippets/retrieve_contents.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 | # https://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 | # [START forms_retrieve_contents] 16 | from apiclient import discovery 17 | from httplib2 import Http 18 | from oauth2client import client, file, tools 19 | 20 | SCOPES = "https://www.googleapis.com/auth/forms.body.readonly" 21 | DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1" 22 | 23 | store = file.Storage("token.json") 24 | creds = None 25 | if not creds or creds.invalid: 26 | flow = client.flow_from_clientsecrets("client_secrets.json", SCOPES) 27 | creds = tools.run_flow(flow, store) 28 | service = discovery.build( 29 | "forms", 30 | "v1", 31 | http=creds.authorize(Http()), 32 | discoveryServiceUrl=DISCOVERY_DOC, 33 | static_discovery=False, 34 | ) 35 | 36 | # Prints the title of the sample form: 37 | form_id = "" 38 | result = service.forms().get(formId=form_id).execute() 39 | print(result) 40 | # [END forms_retrieve_contents] 41 | -------------------------------------------------------------------------------- /forms/snippets/retrieve_single_response.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 | # https://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 | # [START forms_retrieve_single_response] 16 | from apiclient import discovery 17 | from httplib2 import Http 18 | from oauth2client import client, file, tools 19 | 20 | SCOPES = "https://www.googleapis.com/auth/forms.responses.readonly" 21 | DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1" 22 | 23 | store = file.Storage("token.json") 24 | creds = None 25 | if not creds or creds.invalid: 26 | flow = client.flow_from_clientsecrets("client_secrets.json", SCOPES) 27 | creds = tools.run_flow(flow, store) 28 | service = discovery.build( 29 | "forms", 30 | "v1", 31 | http=creds.authorize(Http()), 32 | discoveryServiceUrl=DISCOVERY_DOC, 33 | static_discovery=False, 34 | ) 35 | 36 | # Prints the specified response from your form: 37 | form_id = "" 38 | response_id = "" 39 | result = ( 40 | service.forms() 41 | .responses() 42 | .get(formId=form_id, responseId=response_id) 43 | .execute() 44 | ) 45 | print(result) 46 | # [END forms_retrieve_single_response] 47 | -------------------------------------------------------------------------------- /gmail/quickstart/README.md: -------------------------------------------------------------------------------- 1 | # Gmail Python Quickstart 2 | 3 | Complete the steps described in the [Gmail Python Quickstart]( 4 | https://developers.google.com/gmail/api/quickstart/python), and in 5 | about five minutes you'll have a simple Python command-line application that 6 | makes requests to the Gmail API. 7 | 8 | ## Install 9 | 10 | ```shell 11 | pip install -r requirements.txt 12 | ``` 13 | 14 | ## Run 15 | 16 | ```shell 17 | python quickstart.py 18 | ``` 19 | -------------------------------------------------------------------------------- /gmail/quickstart/requirements.txt: -------------------------------------------------------------------------------- 1 | google-api-python-client==1.7.8 2 | google-auth-httplib2==0.0.3 3 | google-auth-oauthlib==0.4.0 -------------------------------------------------------------------------------- /gmail/snippet/base_test.py: -------------------------------------------------------------------------------- 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 | import os 16 | import unittest 17 | 18 | from apiclient import discovery 19 | from oauth2client.service_account import ServiceAccountCredentials 20 | 21 | 22 | class BaseTest(unittest.TestCase): 23 | RECIPIENT = "gduser01@workspacesamples.dev" 24 | TEST_USER = "ci-test01@workspacesamples.dev" 25 | FORWARDING_ADDRESS = "gduser01@workspacesamples.dev" 26 | 27 | @classmethod 28 | def setUpClass(cls): 29 | cls.service = cls.create_service() 30 | 31 | @classmethod 32 | def create_credentials(cls): 33 | scope = [ 34 | "https://www.googleapis.com/auth/gmail.compose", 35 | "https://www.googleapis.com/auth/gmail.send", 36 | "https://www.googleapis.com/auth/gmail.labels", 37 | "https://www.googleapis.com/auth/gmail.settings.basic", 38 | "https://www.googleapis.com/auth/gmail.settings.sharing", 39 | "https://mail.google.com/", 40 | ] 41 | credentials = ServiceAccountCredentials.from_json_keyfile_name( 42 | os.environ["SERVICE_ACCOUNT_CREDENTIALS"], scopes=scope 43 | ) 44 | return credentials.create_delegated(BaseTest.TEST_USER) 45 | 46 | @classmethod 47 | def create_service(cls): 48 | credentials = cls.create_credentials() 49 | return discovery.build("gmail", "v1", credentials=credentials) 50 | 51 | 52 | if __name__ == "__main__": 53 | unittest.main() 54 | -------------------------------------------------------------------------------- /gmail/snippet/files/cert.p12: -------------------------------------------------------------------------------- 1 | This file should not be empty, it represents the contents of an S/MIME certificate. 2 | -------------------------------------------------------------------------------- /gmail/snippet/files/certs.csv: -------------------------------------------------------------------------------- 1 | UserId,Certificate File Path,Certificate Password 2 | user1@example.com,files/cert.p12,password 3 | user2@example.com,files/cert.p12, 4 | user3@example.com,files/notfound.p12, 5 | -------------------------------------------------------------------------------- /gmail/snippet/files/photo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googleworkspace/python-samples/fd8b29643cfaa07fb4da947938d7e77f35011859/gmail/snippet/files/photo.jpg -------------------------------------------------------------------------------- /gmail/snippet/requirements.txt: -------------------------------------------------------------------------------- 1 | google-auth==2.3.3 2 | google-api-python-client==2.31.0 3 | oauth2client==4.1.3 4 | numpy==1.22.1 -------------------------------------------------------------------------------- /gmail/snippet/send mail/test_create_draft.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | from create_draft import gmail_create_draft 19 | 20 | 21 | class TestCreateDraft(unittest.TestCase): 22 | """Unit test class for snippet""" 23 | 24 | @classmethod 25 | def test_create_draft(cls): 26 | """Unit test for create draft""" 27 | draft = gmail_create_draft() 28 | cls.assertIsNotNone(cls, draft) 29 | 30 | 31 | if __name__ == "__main__": 32 | unittest.main() 33 | -------------------------------------------------------------------------------- /gmail/snippet/send mail/test_create_draft_with_attachment.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | from create_draft_with_attachment import gmail_create_draft_with_attachment 19 | 20 | 21 | class TestCreateDraftWithAttachment(unittest.TestCase): 22 | """Unit test class for Change snippet""" 23 | 24 | @classmethod 25 | def test_create_draft_with_attachment(cls): 26 | """Test create draft with attachment""" 27 | draft = gmail_create_draft_with_attachment() 28 | cls.assertIsNotNone(cls, draft) 29 | 30 | 31 | if __name__ == "__main__": 32 | unittest.main() 33 | -------------------------------------------------------------------------------- /gmail/snippet/send mail/test_send_message.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | from send_message import gmail_send_message 19 | 20 | 21 | class TestSendMessage(unittest.TestCase): 22 | """Unit test class for snippet""" 23 | 24 | def test_send_message(self): 25 | """test send message""" 26 | send_message = gmail_send_message() 27 | self.assertIsNotNone(self, send_message) 28 | 29 | 30 | if __name__ == "__main__": 31 | unittest.main() 32 | -------------------------------------------------------------------------------- /gmail/snippet/send mail/test_send_message_with_attachment.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | from send_message_with_attachment import gmail_send_message_with_attachment 19 | 20 | 21 | class TestSendMessageWithAttachment(unittest.TestCase): 22 | """Unit test class for gmail snippet""" 23 | 24 | def test_send_message_with_attachment(self): 25 | """test send message with attachment""" 26 | send_message = gmail_send_message_with_attachment() 27 | self.assertIsNotNone(self, send_message) 28 | 29 | 30 | if __name__ == "__main__": 31 | unittest.main() 32 | -------------------------------------------------------------------------------- /gmail/snippet/settings snippets/test_create_filter.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | from create_filter import create_filter 19 | 20 | 21 | class TestCreateFilter(unittest.TestCase): 22 | """Unit test class to implement test case for Snippets""" 23 | 24 | @classmethod 25 | def test_create_file(cls): 26 | """test to create file""" 27 | result = create_filter() 28 | cls.assertIsNotNone(cls, result) 29 | 30 | 31 | if __name__ == "__main__": 32 | unittest.main() 33 | -------------------------------------------------------------------------------- /gmail/snippet/settings snippets/test_enable_auto_reply.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | from enable_auto_reply import enable_auto_reply 19 | 20 | 21 | class TestEnableAutoReply(unittest.TestCase): 22 | """Unit test class for the snippet""" 23 | 24 | @classmethod 25 | def test_enable_auto_reply(cls): 26 | """test to enable auto reply""" 27 | result = enable_auto_reply() 28 | cls.assertIsNotNone(cls, result) 29 | 30 | 31 | if __name__ == "__main__": 32 | unittest.main() 33 | -------------------------------------------------------------------------------- /gmail/snippet/settings snippets/test_enable_forwarding.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | from enable_forwarding import enable_forwarding 19 | 20 | 21 | class TestEnableForwarding(unittest.TestCase): 22 | """Unit test class to test enable forwarding snippet""" 23 | 24 | @classmethod 25 | def test_enable_forwarding(cls): 26 | """test to enable forwarding""" 27 | result = enable_forwarding() 28 | cls.assertIsNotNone(cls, result) 29 | 30 | 31 | if __name__ == "__main__": 32 | unittest.main() 33 | -------------------------------------------------------------------------------- /gmail/snippet/settings snippets/test_update_signature.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | from update_signature import update_signature 19 | 20 | 21 | class TestUpdateSignature(unittest.TestCase): 22 | """Unit test class to test Update signature snippet""" 23 | 24 | @classmethod 25 | def test_update_signature(cls): 26 | """test to update signature""" 27 | result = update_signature() 28 | cls.assertIsNotNone(cls, result) 29 | 30 | 31 | if __name__ == "__main__": 32 | unittest.main() 33 | -------------------------------------------------------------------------------- /gmail/snippet/smime snippets/create_smime_info.py: -------------------------------------------------------------------------------- 1 | """Copyright 2018 Google LLC 2 | Licensed under the Apache License, Version 2.0 (the "License"); 3 | you may not use this file except in compliance with the License. 4 | You may obtain a copy of the License at 5 | http://www.apache.org/licenses/LICENSE-2.0 6 | Unless required by applicable law or agreed to in writing, software 7 | distributed under the License is distributed on an "AS IS" BASIS, 8 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 9 | See the License for the specific language governing permissions and 10 | limitations under the License. 11 | """ 12 | 13 | # [START gmail_create_smime_info] 14 | import base64 15 | 16 | 17 | def create_smime_info(cert_filename, cert_password): 18 | """Create an smimeInfo resource for a certificate from file. 19 | Args: 20 | cert_filename: Name of the file containing the S/MIME certificate. 21 | cert_password: Password for the certificate file, or None if the file is not 22 | password-protected. 23 | Returns : Smime object, including smime information 24 | """ 25 | 26 | smime_info = None 27 | try: 28 | with open(cert_filename, "rb") as cert: 29 | smime_info = {} 30 | data = cert.read().encode("UTF-8") 31 | smime_info["pkcs12"] = base64.urlsafe_b64encode(data).decode() 32 | if cert_password and len(cert_password) > 0: 33 | smime_info["encryptedKeyPassword"] = cert_password 34 | 35 | except (OSError, IOError) as error: 36 | print(f"An error occurred while reading the certificate file: {error}") 37 | smime_info = None 38 | 39 | return smime_info 40 | 41 | 42 | if __name__ == "__main__": 43 | print(create_smime_info(cert_filename="xyz", cert_password="xyz")) 44 | # [END gmail_create_smime_info] 45 | -------------------------------------------------------------------------------- /gmail/snippet/smime snippets/insert_cert_from_csv.py: -------------------------------------------------------------------------------- 1 | """Copyright 2018 Google LLC 2 | Licensed under the Apache License, Version 2.0 (the "License"); 3 | you may not use this file except in compliance with the License. 4 | You may obtain a copy of the License at 5 | http://www.apache.org/licenses/LICENSE-2.0 6 | Unless required by applicable law or agreed to in writing, software 7 | distributed under the License is distributed on an "AS IS" BASIS, 8 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 9 | See the License for the specific language governing permissions and 10 | limitations under the License. 11 | """ 12 | 13 | # [START gmail_insert_cert_from_csv] 14 | import csv 15 | 16 | import create_smime_info 17 | import insert_smime_info 18 | 19 | 20 | def insert_cert_from_csv(csv_filename): 21 | """Upload S/MIME certificates based on the contents of a CSV file. 22 | Each row of the CSV file should contain a user ID, path to the certificate, 23 | and the certificate password. 24 | 25 | Args: 26 | csv_filename: Name of the CSV file. 27 | """ 28 | 29 | try: 30 | with open(csv_filename, "rb") as cert: 31 | csv_reader = csv.reader(cert, delimiter=",") 32 | next(csv_reader, None) # skip CSV file header 33 | for row in csv_reader: 34 | user_id = row[0] 35 | cert_filename = row[1] 36 | cert_password = row[2] 37 | smime_info = create_smime_info.create_smime_info( 38 | cert_filename=cert_filename, cert_password=cert_password 39 | ) 40 | if smime_info: 41 | insert_smime_info.insert_smime_info() 42 | else: 43 | print(f"Unable to read certificate file for user_id: {user_id}") 44 | return smime_info 45 | 46 | except (OSError, IOError) as error: 47 | print(f"An error occured while reading the CSV file: {error}") 48 | 49 | 50 | if __name__ == "__main__": 51 | insert_cert_from_csv(csv_filename="xyz") 52 | # [END gmail_insert_cert_from_csv] 53 | -------------------------------------------------------------------------------- /gmail/snippet/smime snippets/test_create_smime_info.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | from create_smime_info import create_smime_info 19 | 20 | 21 | class TestCreateSmimeInfo(unittest.TestCase): 22 | """Unit test class to test Snippet""" 23 | 24 | @classmethod 25 | def test_create_smime_info(cls): 26 | """test to create smime info""" 27 | # enter the file and password accordingly 28 | smime_info = create_smime_info(cert_filename="abc", cert_password="abc") 29 | cls.assertIsNotNone(cls, smime_info) 30 | 31 | 32 | if __name__ == "__main__": 33 | unittest.main() 34 | -------------------------------------------------------------------------------- /gmail/snippet/smime snippets/test_insert_cert_from_csv.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | from insert_cert_from_csv import insert_cert_from_csv 19 | 20 | 21 | class TestInsertCertFromCsv(unittest.TestCase): 22 | """unittest class for testing the snippetts""" 23 | 24 | @classmethod 25 | def test_insert_cert_from_csv(cls): 26 | """test to insert cert from csv""" 27 | result = insert_cert_from_csv("test.csv") 28 | cls.assertIsNotNone(cls, result) 29 | 30 | 31 | if __name__ == "__main__": 32 | unittest.main() 33 | -------------------------------------------------------------------------------- /gmail/snippet/smime snippets/test_insert_smime_info.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | from insert_smime_info import insert_smime_info 19 | 20 | 21 | class TestInsertSmimeInfo(unittest.TestCase): 22 | """Unit test class for snippet""" 23 | 24 | @classmethod 25 | def test_insert_smime_info(cls): 26 | """test to insert smime info""" 27 | result = insert_smime_info() 28 | cls.assertIsNotNone(cls, result) 29 | 30 | 31 | if __name__ == "__main__": 32 | unittest.main() 33 | -------------------------------------------------------------------------------- /gmail/snippet/smime snippets/test_update_smime_cert.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | from update_smime_cert import update_smime_cert 19 | 20 | 21 | class TestUpdateSmimeCert(unittest.TestCase): 22 | """Unit test class for snippets""" 23 | 24 | @classmethod 25 | def test_update_smime_cert(cls): 26 | """test update smime cert""" 27 | result = update_smime_cert( 28 | user_id="xyz", 29 | send_as_email="yzx", 30 | cert_filename="abc", 31 | cert_password="abc", 32 | expire_dt="cde", 33 | ) 34 | cls.assertIsNotNone(cls, result) 35 | 36 | 37 | if __name__ == "__main__": 38 | unittest.main() 39 | -------------------------------------------------------------------------------- /gmail/snippet/smime snippets/test_update_smime_from_csv.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | from update_smime_from_csv import update_smime_from_csv 19 | 20 | 21 | class TestUpdateSmimeFromCsv(unittest.TestCase): 22 | """unit test class for snippets""" 23 | 24 | @classmethod 25 | def test_update_smime_from_csv(cls): 26 | """test to update smime from csv""" 27 | result = update_smime_from_csv(csv_filename="abc") 28 | cls.assertIsNotNone(cls, result) 29 | 30 | 31 | if __name__ == "__main__": 32 | unittest.main() 33 | -------------------------------------------------------------------------------- /gmail/snippet/thread/test_thread.py: -------------------------------------------------------------------------------- 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 | import unittest 17 | 18 | from threads import show_chatty_threads 19 | 20 | 21 | class TestThreads(unittest.TestCase): 22 | """unit test class for snippets""" 23 | 24 | @classmethod 25 | def test_threads(cls): 26 | """to test threads""" 27 | result = show_chatty_threads() 28 | cls.assertIsNotNone(cls, result) 29 | 30 | 31 | if __name__ == "__main__": 32 | unittest.main() 33 | -------------------------------------------------------------------------------- /meet/README.md: -------------------------------------------------------------------------------- 1 | Additional samples can be found at https://github.com/googleapis/google-cloud-python/tree/main/packages/google-apps-meet -------------------------------------------------------------------------------- /meet/quickstart/requirements.txt: -------------------------------------------------------------------------------- 1 | google-apps-meet==0.1.6 2 | google-auth-httplib2==0.1.0 3 | google-auth-oauthlib==0.4.0 -------------------------------------------------------------------------------- /people/quickstart/README.md: -------------------------------------------------------------------------------- 1 | # Google People Python Quickstart 2 | 3 | Complete the steps described in the [quickstart instructions]( 4 | https://developers.google.com/people/quickstart/python), and in about five 5 | minutes you'll have a simple Python command-line application that makes 6 | requests to the Google People API. 7 | 8 | ## Install 9 | 10 | ```shell 11 | pip install -r requirements.txt 12 | ``` 13 | 14 | ## Run 15 | 16 | After following the quickstart setup instructions, run the sample: 17 | 18 | ```shell 19 | python quickstart.py 20 | ``` 21 | -------------------------------------------------------------------------------- /people/quickstart/requirements.txt: -------------------------------------------------------------------------------- 1 | google-api-python-client==1.7.9 2 | google-auth-httplib2==0.0.3 3 | google-auth-oauthlib==0.4.0 -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [tool.pyink] 2 | line-length = 80 3 | preview = true 4 | pyink-indentation = 2 5 | pyink-use-majority-quotes = true -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | google-auth==2.3.3 2 | google-api-python-client==2.31.0 3 | oauth2client==4.1.3 -------------------------------------------------------------------------------- /sheets/images/googlelogo_color_272x92dp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googleworkspace/python-samples/fd8b29643cfaa07fb4da947938d7e77f35011859/sheets/images/googlelogo_color_272x92dp.png -------------------------------------------------------------------------------- /sheets/quickstart/README.md: -------------------------------------------------------------------------------- 1 | # Google Sheets Python Quickstart 2 | 3 | Complete the steps described in the [quickstart instructions]( 4 | https://developers.google.com/sheets/api/quickstart/python), and in about five 5 | minutes you'll have a simple Python command-line application that makes 6 | requests to the Google Sheets API. 7 | 8 | ## Install 9 | 10 | ```shell 11 | pip install -r requirements.txt 12 | ``` 13 | 14 | 15 | ## Run 16 | 17 | After following the quickstart setup instructions, run the sample: 18 | 19 | ```shell 20 | python quickstart.py 21 | ``` 22 | -------------------------------------------------------------------------------- /sheets/quickstart/requirements.txt: -------------------------------------------------------------------------------- 1 | google-api-python-client==1.7.9 2 | google-auth-httplib2==0.0.3 3 | google-auth-oauthlib==0.4.0 -------------------------------------------------------------------------------- /sheets/snippets/sheets_create.py: -------------------------------------------------------------------------------- 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 | 17 | # [START sheets_create] 18 | import google.auth 19 | from googleapiclient.discovery import build 20 | from googleapiclient.errors import HttpError 21 | 22 | 23 | def create(title): 24 | """ 25 | Creates the Sheet the user has access to. 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 | # pylint: disable=maybe-no-member 32 | try: 33 | service = build("sheets", "v4", credentials=creds) 34 | spreadsheet = {"properties": {"title": title}} 35 | spreadsheet = ( 36 | service.spreadsheets() 37 | .create(body=spreadsheet, fields="spreadsheetId") 38 | .execute() 39 | ) 40 | print(f"Spreadsheet ID: {(spreadsheet.get('spreadsheetId'))}") 41 | return spreadsheet.get("spreadsheetId") 42 | except HttpError as error: 43 | print(f"An error occurred: {error}") 44 | return error 45 | 46 | 47 | if __name__ == "__main__": 48 | # Pass: title 49 | create("mysheet1") 50 | # [END sheets_create] 51 | -------------------------------------------------------------------------------- /sheets/snippets/sheets_get_values.py: -------------------------------------------------------------------------------- 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 | 17 | # [START sheets_get_values] 18 | import google.auth 19 | from googleapiclient.discovery import build 20 | from googleapiclient.errors import HttpError 21 | 22 | 23 | def get_values(spreadsheet_id, range_name): 24 | """ 25 | Creates the batch_update the user has access to. 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 | # pylint: disable=maybe-no-member 32 | try: 33 | service = build("sheets", "v4", credentials=creds) 34 | 35 | result = ( 36 | service.spreadsheets() 37 | .values() 38 | .get(spreadsheetId=spreadsheet_id, range=range_name) 39 | .execute() 40 | ) 41 | rows = result.get("values", []) 42 | print(f"{len(rows)} rows retrieved") 43 | return result 44 | except HttpError as error: 45 | print(f"An error occurred: {error}") 46 | return error 47 | 48 | 49 | if __name__ == "__main__": 50 | # Pass: spreadsheet_id, and range_name 51 | get_values("1CM29gwKIzeXsAppeNwrc8lbYaVMmUclprLuLYuHog4k", "A1:C2") 52 | # [END sheets_get_values] 53 | -------------------------------------------------------------------------------- /sheets/snippets/test_sheets_append_values.py: -------------------------------------------------------------------------------- 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 | 14 | import unittest 15 | 16 | import sheets_append_values 17 | from base_test import BaseTest 18 | 19 | 20 | class Testappendvalues(BaseTest): 21 | """Unit test for append value Sheet snippet""" 22 | 23 | def test_append_values(self): 24 | """test append values function""" 25 | spreadsheet_id = self.create_test_spreadsheet() 26 | self.populate_values(spreadsheet_id) 27 | result = sheets_append_values.append_values( 28 | spreadsheet_id, "Sheet1", "USER_ENTERED", [["A", "B"], ["C", "D"]] 29 | ) 30 | self.assertIsNotNone(result) 31 | self.assertEqual("Sheet1!A1:J10", result.get("tableRange")) 32 | updates = result.get("updates") 33 | self.assertEqual("Sheet1!A11:B12", updates.get("updatedRange")) 34 | self.assertEqual(2, updates.get("updatedRows")) 35 | self.assertEqual(2, updates.get("updatedColumns")) 36 | self.assertEqual(4, updates.get("updatedCells")) 37 | 38 | 39 | if __name__ == "__main__": 40 | unittest.main() 41 | -------------------------------------------------------------------------------- /sheets/snippets/test_sheets_batch_get_values.py: -------------------------------------------------------------------------------- 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 | 14 | import unittest 15 | 16 | import sheets_batch_get_values 17 | from base_test import BaseTest 18 | 19 | 20 | class Testgetvalues(BaseTest): 21 | """Unit test class for get value Sheet snippet""" 22 | 23 | def test_batch_get_values(self): 24 | """test batch get values function""" 25 | spreadsheet_id = self.create_test_spreadsheet() 26 | self.populate_values(spreadsheet_id) 27 | result = sheets_batch_get_values.batch_get_values( 28 | spreadsheet_id, ["A1:A3", "B1:C1"] 29 | ) 30 | self.assertIsNotNone(result) 31 | valueranges = result.get("valueRanges") 32 | self.assertIsNotNone(valueranges) 33 | self.assertEqual(2, len(valueranges)) 34 | values = valueranges[0].get("values") 35 | self.assertEqual(3, len(values)) 36 | 37 | 38 | if __name__ == "__main__": 39 | unittest.main() 40 | -------------------------------------------------------------------------------- /sheets/snippets/test_sheets_batch_update.py: -------------------------------------------------------------------------------- 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 | 14 | import unittest 15 | 16 | import sheets_batch_update 17 | import sheets_create 18 | from base_test import BaseTest 19 | 20 | 21 | class Testbatchupdate(BaseTest): 22 | """Unit test class for Batch update Sheet snippet""" 23 | 24 | def test_batch_update(self): 25 | """test_batch_update function""" 26 | spreadsheet_id = sheets_create.create("Title") 27 | self.populate_values(spreadsheet_id) 28 | response = sheets_batch_update.sheets_batch_update( 29 | spreadsheet_id, "New Title", "Hello", "Goodbye" 30 | ) 31 | self.assertIsNotNone(response) 32 | replies = response.get("replies") 33 | self.assertIsNotNone(replies) 34 | self.assertEqual(2, len(replies)) 35 | find_replace_response = replies[1].get("findReplace") 36 | self.assertIsNotNone(find_replace_response) 37 | self.assertEqual(100, find_replace_response.get("occurrencesChanged")) 38 | 39 | 40 | if __name__ == "__main__": 41 | unittest.main() 42 | -------------------------------------------------------------------------------- /sheets/snippets/test_sheets_batch_update_values.py: -------------------------------------------------------------------------------- 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 | 14 | import unittest 15 | 16 | import sheets_batch_update_values 17 | from base_test import BaseTest 18 | 19 | 20 | class Testbatchupdatevalues(BaseTest): 21 | """Unit test for Batch update value Sheet snippet""" 22 | 23 | def test_batch_update_values(self): 24 | """batch updates values""" 25 | spreadsheet_id = self.create_test_spreadsheet() 26 | result = sheets_batch_update_values.batch_update_values( 27 | spreadsheet_id, "A1:B2", "USER_ENTERED", [["A", "B"], ["C", "D"]] 28 | ) 29 | self.assertIsNotNone(result) 30 | self.assertEqual(1, len(result.get("responses"))) 31 | self.assertEqual(2, result.get("totalUpdatedRows")) 32 | self.assertEqual(2, result.get("totalUpdatedColumns")) 33 | self.assertEqual(4, result.get("totalUpdatedCells")) 34 | 35 | 36 | if __name__ == "__main__": 37 | unittest.main() 38 | -------------------------------------------------------------------------------- /sheets/snippets/test_sheets_conditional_formatting.py: -------------------------------------------------------------------------------- 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 | 14 | import unittest 15 | 16 | import sheets_conditional_formatting 17 | from base_test import BaseTest 18 | 19 | 20 | class Testconditionalformatting(BaseTest): 21 | """Unit test for sheets conditional_formatting value Sheet snippet""" 22 | 23 | def test_conditional_formatting(self): 24 | """sheets_conditional_formatting function""" 25 | spreadsheet_id = self.create_test_spreadsheet() 26 | self.populate_values(spreadsheet_id) 27 | response = sheets_conditional_formatting.conditional_formatting( 28 | spreadsheet_id 29 | ) 30 | self.assertEqual(2, len(response.get("replies"))) 31 | 32 | 33 | if __name__ == "__main__": 34 | unittest.main() 35 | -------------------------------------------------------------------------------- /sheets/snippets/test_sheets_create.py: -------------------------------------------------------------------------------- 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 | 14 | import unittest 15 | 16 | import sheets_create 17 | from base_test import BaseTest 18 | 19 | 20 | class Testsheetscreate(BaseTest): 21 | """Unit test class for Create Sheet snippet""" 22 | 23 | def test_create(self): 24 | """sheet function for Create sheet""" 25 | spreadsheet_id = sheets_create.create("Title") 26 | self.assertIsNotNone(spreadsheet_id) 27 | self.delete_file_on_cleanup(spreadsheet_id) 28 | 29 | 30 | if __name__ == "__main__": 31 | unittest.main() 32 | -------------------------------------------------------------------------------- /sheets/snippets/test_sheets_filter_views.py: -------------------------------------------------------------------------------- 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 | 14 | import unittest 15 | 16 | import sheets_filter_views 17 | from base_test import BaseTest 18 | 19 | 20 | class Testfilterviews(BaseTest): 21 | """Unit test for sheets conditional_formatting value Sheet snippet""" 22 | 23 | def test_filter_views(self): 24 | """test filter view function""" 25 | spreadsheet_id = self.create_test_spreadsheet() 26 | self.populate_values(spreadsheet_id) 27 | sheets_filter_views.filter_views(spreadsheet_id) 28 | 29 | 30 | if __name__ == "__main__": 31 | unittest.main() 32 | -------------------------------------------------------------------------------- /sheets/snippets/test_sheets_get_values.py: -------------------------------------------------------------------------------- 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 | 14 | import unittest 15 | 16 | import sheets_get_values 17 | from base_test import BaseTest 18 | 19 | 20 | class Testgetvalues(BaseTest): 21 | """Unit test class for get value Sheet snippet""" 22 | 23 | def test_get_values(self): 24 | """test_get_values""" 25 | spreadsheet_id = self.create_test_spreadsheet() 26 | self.populate_values(spreadsheet_id) 27 | result = sheets_get_values.get_values(spreadsheet_id, "A1:C2") 28 | self.assertIsNotNone(result) 29 | values = result.get("values") 30 | self.assertIsNotNone(values) 31 | self.assertEqual(2, len(values)) 32 | self.assertEqual(3, len(values[0])) 33 | 34 | 35 | if __name__ == "__main__": 36 | unittest.main() 37 | -------------------------------------------------------------------------------- /sheets/snippets/test_sheets_pivot_tables.py: -------------------------------------------------------------------------------- 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 | 14 | import unittest 15 | 16 | import sheets_pivot_tables 17 | from base_test import BaseTest 18 | 19 | 20 | class Testpivottables(BaseTest): 21 | """Unit test for Pivot tables value Sheet snippet""" 22 | 23 | def test_pivot_tables(self): 24 | """pivot table function""" 25 | spreadsheet_id = self.create_test_spreadsheet() 26 | self.populate_values(spreadsheet_id) 27 | response = sheets_pivot_tables.pivot_tables(spreadsheet_id) 28 | self.assertIsNotNone(response) 29 | 30 | 31 | if __name__ == "__main__": 32 | unittest.main() 33 | -------------------------------------------------------------------------------- /sheets/snippets/test_sheets_update_values.py: -------------------------------------------------------------------------------- 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 | 14 | import unittest 15 | 16 | import sheets_update_values 17 | from base_test import BaseTest 18 | 19 | 20 | class Testupdatesvalues(BaseTest): 21 | """Unit test for update value Sheet snippet""" 22 | 23 | def test_update_values(self): 24 | """test updates_values""" 25 | spreadsheet_id = self.create_test_spreadsheet() 26 | result = sheets_update_values.update_values( 27 | spreadsheet_id, "A1:B2", "USER_ENTERED", [["A", "B"], ["C", "D"]] 28 | ) 29 | self.assertIsNotNone(result) 30 | self.assertEqual(2, result.get("updatedRows")) 31 | self.assertEqual(2, result.get("updatedColumns")) 32 | self.assertEqual(4, result.get("updatedCells")) 33 | 34 | 35 | if __name__ == "__main__": 36 | unittest.main() 37 | -------------------------------------------------------------------------------- /slides/quickstart/README.md: -------------------------------------------------------------------------------- 1 | # Google Slides Python Quickstart 2 | 3 | Complete the steps described in the [quickstart instructions]( 4 | https://developers.google.com/slides/quickstart/python), and in about five 5 | minutes you'll have a simple Python command-line application that makes 6 | requests to the Google Slides API. 7 | 8 | ## Install 9 | 10 | ```shell 11 | pip install -r requirements.txt 12 | ``` 13 | 14 | ## Run 15 | 16 | After following the quickstart setup instructions, run the sample: 17 | 18 | ```shell 19 | python quickstart.py 20 | ``` 21 | -------------------------------------------------------------------------------- /slides/quickstart/requirements.txt: -------------------------------------------------------------------------------- 1 | google-api-python-client==1.7.9 2 | google-auth-httplib2==0.0.3 3 | google-auth-oauthlib==0.4.0 -------------------------------------------------------------------------------- /slides/snippets/slides_copy_presentation.py: -------------------------------------------------------------------------------- 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 | 17 | # [START slides_copy_presentation] 18 | import google.auth 19 | from googleapiclient.discovery import build 20 | from googleapiclient.errors import HttpError 21 | 22 | 23 | def copy_presentation(presentation_id, copy_title): 24 | """ 25 | Creates the copy Presentation the user has access to. 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 | 31 | creds, _ = google.auth.default() 32 | # pylint: disable=maybe-no-member 33 | try: 34 | drive_service = build("drive", "v3", credentials=creds) 35 | body = {"name": copy_title} 36 | drive_response = ( 37 | drive_service.files().copy(fileId=presentation_id, body=body).execute() 38 | ) 39 | presentation_copy_id = drive_response.get("id") 40 | 41 | except HttpError as error: 42 | print(f"An error occurred: {error}") 43 | print("Presentations not copied") 44 | return error 45 | 46 | return presentation_copy_id 47 | # [END slides_copy_presentation] 48 | 49 | 50 | if __name__ == "__main__": 51 | # Put the presentation_id, Page_id of slides whose list needs 52 | # to be submitted. 53 | copy_presentation("16eRvJHRrM8Sej5YA0yCHVzQCPLz31-JhbOa4XpP8Yko", "wspace") 54 | -------------------------------------------------------------------------------- /slides/snippets/slides_create_presentation.py: -------------------------------------------------------------------------------- 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 | 17 | # [START slides_create_presentation] 18 | import google.auth 19 | from googleapiclient.discovery import build 20 | from googleapiclient.errors import HttpError 21 | 22 | 23 | def create_presentation(title): 24 | """ 25 | Creates the Presentation the user has access to. 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 | # pylint: disable=maybe-no-member 32 | try: 33 | service = build("slides", "v1", credentials=creds) 34 | 35 | body = {"title": title} 36 | presentation = service.presentations().create(body=body).execute() 37 | print( 38 | f"Created presentation with ID:{(presentation.get('presentationId'))}" 39 | ) 40 | return presentation 41 | 42 | except HttpError as error: 43 | print(f"An error occurred: {error}") 44 | print("presentation not created") 45 | return error 46 | 47 | 48 | if __name__ == "__main__": 49 | # Put the title of the presentation 50 | 51 | create_presentation("finalp") 52 | 53 | # [END slides_create_presentation] 54 | -------------------------------------------------------------------------------- /slides/snippets/test_slides_copy_presentation.py: -------------------------------------------------------------------------------- 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 | 14 | import unittest 15 | 16 | import slides_copy_presentation 17 | from base_test import BaseTest 18 | 19 | 20 | class TestCopyPresentation(BaseTest): 21 | """Unit test for Copy presentation snippet""" 22 | 23 | def test_copy_presentation(self): 24 | """set title for copy presentation""" 25 | presentation_id = self.create_test_presentation() 26 | copy_id = slides_copy_presentation.copy_presentation( 27 | presentation_id, "My Duplicate Presentation" 28 | ) 29 | self.assertIsNotNone(copy_id) 30 | self.delete_file_on_cleanup(copy_id) 31 | 32 | 33 | if __name__ == "__main__": 34 | unittest.main() 35 | -------------------------------------------------------------------------------- /slides/snippets/test_slides_create_bulleted_text.py: -------------------------------------------------------------------------------- 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 | 14 | import unittest 15 | from pprint import pformat 16 | 17 | import slides_create_bulleted_text 18 | from base_test import BaseTest 19 | 20 | 21 | class TestCreateBulletedText(BaseTest): 22 | """Unit test for create_bulleted_text snippet""" 23 | 24 | def test_create_bulleted_text(self): 25 | """create_bulleted_text function""" 26 | presentation_id = self.create_test_presentation() 27 | page_id = self.add_slides(presentation_id, 1, "BLANK")[0] 28 | box_id = self.create_test_textbox(presentation_id, page_id) 29 | response = slides_create_bulleted_text.create_bulleted_text( 30 | presentation_id, box_id 31 | ) 32 | self.assertEqual(1, len(response.get("replies")), msg=pformat(response)) 33 | 34 | 35 | if __name__ == "__main__": 36 | unittest.main() 37 | -------------------------------------------------------------------------------- /slides/snippets/test_slides_create_image.py: -------------------------------------------------------------------------------- 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 | 14 | import unittest 15 | from pprint import pformat 16 | 17 | import slides_create_image 18 | from base_test import BaseTest 19 | 20 | 21 | class TestCreateTextboxWithText(BaseTest): 22 | """Unit test case for create_image snippet""" 23 | 24 | def test_create_image(self): 25 | """presentation id and page id for create image""" 26 | presentation_id = self.create_test_presentation() 27 | page_id = self.add_slides(presentation_id, 1, "BLANK")[0] 28 | response = slides_create_image.create_image(presentation_id, page_id) 29 | self.assertEqual(1, len(response.get("replies")), msg=pformat(response)) 30 | image_id = response.get("replies")[0].get("createImage").get("objectId") 31 | self.assertIsNotNone(image_id, msg=pformat(response)) 32 | 33 | 34 | if __name__ == "__main__": 35 | unittest.main() 36 | -------------------------------------------------------------------------------- /slides/snippets/test_slides_create_presentation.py: -------------------------------------------------------------------------------- 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 | 14 | import unittest 15 | 16 | import slides_create_presentation 17 | from base_test import BaseTest 18 | 19 | 20 | class TestCreatePresentation(BaseTest): 21 | """Unit test for create presentation snippet""" 22 | 23 | def test_create_presentation(self): 24 | """Set title for create presentation""" 25 | presentation = slides_create_presentation.create_presentation("Title") 26 | self.assertIsNotNone(presentation) 27 | self.delete_file_on_cleanup(presentation.get("presentationId")) 28 | 29 | 30 | if __name__ == "__main__": 31 | unittest.main() 32 | -------------------------------------------------------------------------------- /slides/snippets/test_slides_create_sheets_chart.py: -------------------------------------------------------------------------------- 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 | 14 | import unittest 15 | from pprint import pformat 16 | 17 | import slides_create_sheets_chart 18 | from base_test import BaseTest 19 | 20 | 21 | class TestCreateSheetsChart(BaseTest): 22 | """Unit test for create_sheets_chart snippet""" 23 | 24 | DATA_SPREADSHEET_ID = "17eqFZl_WK4WVixX8PjvjfLD77DraoFwMDXeiHB3dvuM" 25 | CHART_ID = 1107320627 26 | 27 | def test_create_sheets_chart(self): 28 | """create_sheet chart method""" 29 | presentation_id = self.create_test_presentation() 30 | page_id = self.add_slides(presentation_id, 1, "BLANK")[0] 31 | response = slides_create_sheets_chart.create_sheets_chart( 32 | presentation_id, page_id, self.DATA_SPREADSHEET_ID, self.CHART_ID 33 | ) 34 | self.assertEqual(1, len(response.get("replies")), msg=pformat(response)) 35 | chart_id = ( 36 | response.get("replies")[0].get("createSheetsChart").get("objectId") 37 | ) 38 | self.assertIsNotNone(chart_id, msg=pformat(response)) 39 | 40 | 41 | if __name__ == "__main__": 42 | unittest.main() 43 | -------------------------------------------------------------------------------- /slides/snippets/test_slides_create_slide.py: -------------------------------------------------------------------------------- 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 | 14 | import unittest 15 | 16 | import slides_create_slide 17 | from base_test import BaseTest 18 | 19 | 20 | class TestCreateSlide(BaseTest): 21 | """Unit test for create Slide snippet""" 22 | 23 | def test_create_slide(self): 24 | """pass presentation_id and page_id for creating the slides""" 25 | presentation_id = self.create_test_presentation() 26 | self.add_slides(presentation_id, 3) 27 | page_id = "my_page_id" 28 | response = slides_create_slide.create_slide(presentation_id, page_id) 29 | self.assertEqual( 30 | page_id, 31 | response.get("replies")[0].get("createSlide").get("objectId"), 32 | ) 33 | 34 | 35 | if __name__ == "__main__": 36 | unittest.main() 37 | -------------------------------------------------------------------------------- /slides/snippets/test_slides_create_textbox_with_text.py: -------------------------------------------------------------------------------- 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 | 14 | import unittest 15 | from pprint import pformat 16 | 17 | import slides_create_textbox_with_text 18 | from base_test import BaseTest 19 | 20 | 21 | class TestCreateTextboxWithText(BaseTest): 22 | """Unit test for TestCreateTextboxWithText snippet""" 23 | 24 | def test_create_textbox_with_text(self): 25 | """Pass Presentation id and page id""" 26 | presentation_id = self.create_test_presentation() 27 | page_id = self.add_slides(presentation_id, 1, "BLANK")[0] 28 | response = slides_create_textbox_with_text.create_textbox_with_text( 29 | presentation_id, page_id 30 | ) 31 | self.assertEqual(2, len(response.get("replies")), msg=pformat(response)) 32 | box_id = response.get("replies")[0].get("createShape").get("objectId") 33 | self.assertIsNotNone(box_id, msg=pformat(response)) 34 | 35 | 36 | if __name__ == "__main__": 37 | unittest.main() 38 | -------------------------------------------------------------------------------- /slides/snippets/test_slides_image_merging.py: -------------------------------------------------------------------------------- 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 | 14 | import unittest 15 | from pprint import pformat 16 | 17 | import slides_image_merging 18 | from base_test import BaseTest 19 | 20 | 21 | class TestTextMerging(BaseTest): 22 | """Unit test for text merging snippet""" 23 | 24 | TEMPLATE_PRESENTATION_ID = "10QnVUx1X2qHsL17WUidGpPh_SQhXYx40CgIxaKk8jU4" 25 | DATA_SPREADSHEET_ID = "17eqFZl_WK4WVixX8PjvjfLD77DraoFwMDXeiHB3dvuM" 26 | IMAGE_URL = "https://picsum.photos/200" 27 | CHART_ID = 1107320627 28 | CUSTOMER_NAME = "Fake Customer" 29 | 30 | def test_image_merging(self): 31 | """image merging function""" 32 | response = slides_image_merging.image_merging( 33 | self.TEMPLATE_PRESENTATION_ID, self.IMAGE_URL, self.CUSTOMER_NAME 34 | ) 35 | presentation_id = response.get("presentationId") 36 | self.delete_file_on_cleanup(presentation_id) 37 | self.assertIsNotNone(presentation_id, msg=pformat(response)) 38 | self.assertEqual(2, len(response.get("replies")), msg=pformat(response)) 39 | num_replacements = 0 40 | for reply in response.get("replies"): 41 | if isinstance(reply, int): 42 | num_replacements += reply.get("replaceAllShapesWithImage").get( 43 | "occurrencesChanged" 44 | ) 45 | 46 | 47 | if __name__ == "__main__": 48 | unittest.main() 49 | -------------------------------------------------------------------------------- /slides/snippets/test_slides_refresh_sheets_chart.py: -------------------------------------------------------------------------------- 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 | 14 | import unittest 15 | from pprint import pformat 16 | 17 | import slides_refresh_sheets_chart 18 | from base_test import BaseTest 19 | 20 | 21 | class TestCreateSheetsChart(BaseTest): 22 | """Unit test for refresh_sheets_chart snippet""" 23 | 24 | DATA_SPREADSHEET_ID = "17eqFZl_WK4WVixX8PjvjfLD77DraoFwMDXeiHB3dvuM" 25 | CHART_ID = 1107320627 26 | 27 | def test_refresh_sheets_chart(self): 28 | """refresh_sheets_chart method""" 29 | presentation_id = self.create_test_presentation() 30 | page_id = self.add_slides(presentation_id, 1, "BLANK")[0] 31 | chart_id = self.create_test_sheets_chart( 32 | presentation_id, page_id, self.DATA_SPREADSHEET_ID, self.CHART_ID 33 | ) 34 | response = slides_refresh_sheets_chart.refresh_sheets_chart( 35 | presentation_id, chart_id 36 | ) 37 | self.assertEqual(1, len(response.get("replies")), msg=pformat(response)) 38 | 39 | 40 | if __name__ == "__main__": 41 | unittest.main() 42 | -------------------------------------------------------------------------------- /slides/snippets/test_slides_simple_text_replace.py: -------------------------------------------------------------------------------- 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 | 14 | import unittest 15 | from pprint import pformat 16 | 17 | import slides_simple_text_replace 18 | from base_test import BaseTest 19 | 20 | 21 | class TestSimpleTextReplace(BaseTest): 22 | """Unit test for SimpleTextReplace snippet""" 23 | 24 | def test_simple_text_replace(self): 25 | """test_simple_text_replace function""" 26 | presentation_id = self.create_test_presentation() 27 | page_id = self.add_slides(presentation_id, 1, "BLANK")[0] 28 | box_id = self.create_test_textbox(presentation_id, page_id) 29 | response = slides_simple_text_replace.simple_text_replace( 30 | presentation_id, box_id, "MY NEW TEXT" 31 | ) 32 | self.assertEqual(2, len(response.get("replies")), msg=pformat(response)) 33 | 34 | 35 | if __name__ == "__main__": 36 | unittest.main() 37 | -------------------------------------------------------------------------------- /slides/snippets/test_slides_text_merging.py: -------------------------------------------------------------------------------- 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 | 14 | import unittest 15 | 16 | import slides_text_merging 17 | from base_test import BaseTest 18 | 19 | 20 | class TestTextMerging(BaseTest): 21 | """Unit test for SimpleTextReplace snippet""" 22 | 23 | TEMPLATE_PRESENTATION_ID = "10QnVUx1X2qHsL17WUidGpPh_SQhXYx40CgIxaKk8jU4" 24 | DATA_SPREADSHEET_ID = "17eqFZl_WK4WVixX8PjvjfLD77DraoFwMDXeiHB3dvuM" 25 | 26 | def test_text_merging(self): 27 | """text_merging method""" 28 | slides_text_merging.text_merging( 29 | self.TEMPLATE_PRESENTATION_ID, self.DATA_SPREADSHEET_ID 30 | ) 31 | 32 | 33 | if __name__ == "__main__": 34 | unittest.main() 35 | -------------------------------------------------------------------------------- /slides/snippets/test_slides_text_style_update.py: -------------------------------------------------------------------------------- 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 | 14 | import unittest 15 | from pprint import pformat 16 | 17 | import slides_text_style_update 18 | from base_test import BaseTest 19 | 20 | 21 | class TestTextStyleUpdate(BaseTest): 22 | """Unit test for SimpleTextReplace snippet""" 23 | 24 | def test_text_style_update(self): 25 | """test_text_style_update function""" 26 | presentation_id = self.create_test_presentation() 27 | page_id = self.add_slides(presentation_id, 1, "BLANK")[0] 28 | box_id = self.create_test_textbox(presentation_id, page_id) 29 | response = slides_text_style_update.text_style_update( 30 | presentation_id, box_id 31 | ) 32 | self.assertEqual(3, len(response.get("replies")), msg=pformat(response)) 33 | 34 | 35 | if __name__ == "__main__": 36 | unittest.main() 37 | -------------------------------------------------------------------------------- /tasks/quickstart/README.md: -------------------------------------------------------------------------------- 1 | # Google Tasks Python Quickstart 2 | 3 | Complete the steps described in the [quickstart instructions]( 4 | https://developers.google.com/google-apps/tasks/quickstart/python), and in about 5 | five minutes you'll have a simple Python command-line application that makes 6 | requests to the Google Tasks API. 7 | 8 | ## Install 9 | 10 | ```shell 11 | pip install -r requirements.txt 12 | ``` 13 | 14 | ## Run 15 | 16 | After following the quickstart setup instructions, run the sample: 17 | 18 | ```shell 19 | python quickstart.py 20 | ``` 21 | -------------------------------------------------------------------------------- /tasks/quickstart/requirements.txt: -------------------------------------------------------------------------------- 1 | google-api-python-client==1.7.9 2 | google-auth-httplib2==0.0.3 3 | google-auth-oauthlib==0.4.0 -------------------------------------------------------------------------------- /vault/quickstart/README.md: -------------------------------------------------------------------------------- 1 | # Google Vault Python Quickstart 2 | 3 | Complete the steps described in the [quickstart instructions]( 4 | https://developers.google.com/vault/quickstart/python), and in about 5 | five minutes you'll have a simple Python command-line application that makes 6 | requests to the Google Vault API. 7 | 8 | ## Install 9 | 10 | ```shell 11 | pip install -r requirements.txt 12 | ``` 13 | 14 | ## Run 15 | 16 | After following the quickstart setup instructions, run the sample: 17 | 18 | ```shell 19 | python quickstart.py 20 | ``` 21 | -------------------------------------------------------------------------------- /vault/quickstart/requirements.txt: -------------------------------------------------------------------------------- 1 | google-api-python-client==1.7.9 2 | google-auth-httplib2==0.0.3 3 | google-auth-oauthlib==0.4.0 --------------------------------------------------------------------------------