|
| 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 | +# [START admin_sdk_groups_migration_quickstart] |
| 16 | +""" |
| 17 | +Shows basic usage of the Admin SDK Groups Migration API. Inserts a test email |
| 18 | +into a group. |
| 19 | +""" |
| 20 | +from __future__ import print_function |
| 21 | +from apiclient.discovery import build, http |
| 22 | +from httplib2 import Http |
| 23 | +from oauth2client import file, client, tools |
| 24 | +import StringIO |
| 25 | +import random |
| 26 | + |
| 27 | +import apiclient |
| 28 | +from email import Utils |
| 29 | +from email import MIMEText |
| 30 | + |
| 31 | +# Setup the Admin SDK Groups Migration API |
| 32 | +SCOPES = 'https://www.googleapis.com/auth/apps.groups.migration' |
| 33 | +store = file.Storage('credentials.json') |
| 34 | +creds = store.get() |
| 35 | +if not creds or creds.invalid: |
| 36 | + flow = client.flow_from_clientsecrets('client_secret.json', SCOPES) |
| 37 | + creds = tools.run_flow(flow, store) |
| 38 | +service = build('groupsmigration', 'v1', http=creds.authorize(Http())) |
| 39 | + |
| 40 | +# Call the Admin SDK Groups Migration API |
| 41 | +print('Warning: A test email will be inserted into the group entered.') |
| 42 | +groupId = raw_input( |
| 43 | + 'Enter the email address of a Google Group in your domain: ') |
| 44 | + |
| 45 | +# Format an RFC822 message |
| 46 | +message = MIMEText.MIMEText('This is a test.') |
| 47 | +# Generate a random 10 digit number for message Id. |
| 48 | +message['Message-ID'] = '<{0}-{1}>'.format(str(random.randrange(10**10)), |
| 49 | + groupId) |
| 50 | +message['Subject'] = 'Groups Migration API Test (Python)' |
| 51 | +message['From'] = '"Alice Smith" <alice@example.com>' |
| 52 | +message['To'] = groupId |
| 53 | +message['Date'] = Utils.formatdate(localtime=True) |
| 54 | + |
| 55 | +stream = StringIO.StringIO() |
| 56 | +stream.write(message.as_string()) |
| 57 | +media = apiclient.http.MediaIoBaseUpload(stream, |
| 58 | + mimetype='message/rfc822') |
| 59 | + |
| 60 | +result = service.archive().insert(groupId=groupId, |
| 61 | + media_body=media).execute() |
| 62 | +print(result['responseCode']) |
| 63 | +# [END admin_sdk_groups_migration_quickstart] |
0 commit comments