Add a new impersonate sample

This commit is contained in:
Thomas Darde 2017-02-12 15:13:19 +01:00
parent 90d90d00b2
commit 172e04b272
1 changed files with 33 additions and 0 deletions

View File

@ -120,5 +120,38 @@ module Samples
end
end
end
desc 'impersonate and update email signature', 'Update the email signature of another user'
method_option :impersonated_email, type: :string, required: true
def update_email_signature(new_signature_content)
gmail = Gmail::GmailService.new
# You can download a client_secret.json from the service account page
# of your developer's console
attrs = {
json_key_io: 'client_secret.json',
scope: [ Gmail::AUTH_GMAIL_SETTINGS_BASIC ]
}
auth = Google::Auth::ServiceAccountCredentials.make_creds(attrs)
impersonate_auth = auth.dup
impersonate_auth.sub = impersonated_email
user_id = impersonated_email
send_as_email = update_user_setting_send_as
gmail.authorization = impersonate_auth
send_as_object = {"signature": new_signature_content}
# options: {} is necessary for method to be called correctly.
result = service.patch_user_setting_send_as(user_id, send_as_email, send_as_object, options: {})
puts "signature of #{impersonated_email} is now: #{result.signature}"
end
end
end