|
| 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 | +# [START gmail_insert_smime_info] |
| 13 | + |
| 14 | +from __future__ import print_function |
| 15 | + |
| 16 | +import google.auth |
| 17 | +from googleapiclient.discovery import build |
| 18 | +from googleapiclient.errors import HttpError |
| 19 | + |
| 20 | +import create_smime_info |
| 21 | + |
| 22 | + |
| 23 | +def insert_smime_info(): |
| 24 | + """Upload an S/MIME certificate for the user. |
| 25 | + Print the inserted certificate's id |
| 26 | + Returns : Result object with inserted certificate id and other meta-data |
| 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 gmail api client |
| 36 | + service = build('gmail', 'v1', credentials=creds) |
| 37 | + |
| 38 | + |
| 39 | + smime_info = create_smime_info.create_smime_info() |
| 40 | + send_as_email = None |
| 41 | + |
| 42 | + if not send_as_email: |
| 43 | + send_as_email = user_id |
| 44 | + |
| 45 | + # pylint: disable=maybe-no-member |
| 46 | + results = service.users().settings().sendAs().smimeInfo().\ |
| 47 | + insert(userId=user_id, sendAsEmail=send_as_email, body=smime_info)\ |
| 48 | + .execute() |
| 49 | + print(F'Inserted certificate; id: {results["id"]}') |
| 50 | + |
| 51 | + except HttpError as error: |
| 52 | + print(F'An error occurred: {error}') |
| 53 | + results = None |
| 54 | + |
| 55 | + return results |
| 56 | + |
| 57 | + |
| 58 | +if __name__ == '__main__': |
| 59 | + insert_smime_info() |
| 60 | +# [END gmail_insert_smime_info] |
0 commit comments