Last active
August 29, 2015 14:15
-
-
Save richieforeman/97bed6ce4b8d42723002 to your computer and use it in GitHub Desktop.
Reseller Sample (Ruby)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'google/api_client' | |
client = Google::APIClient.new( | |
:application_name => 'Reseller Example!', | |
:application_version => '1.0.0' | |
) | |
RESELLER_ADMIN = '...' | |
PRIVATE_KEY_FILE = 'privatekey.p12' | |
@OAUTH2_SCOPES = [ | |
'https://www.googleapis.com/auth/admin.directory.user', | |
'https://www.googleapis.com/auth/apps.order', | |
'https://www.googleapis.com/auth/siteverification' | |
] | |
SERVICE_ACCOUNT_EMAIL = '...' | |
key = Google::APIClient::KeyUtils.load_from_pkcs12(PRIVATE_KEY_FILE, 'notasecret') | |
client.authorization = Signet::OAuth2::Client.new( | |
:token_credential_uri => 'https://accounts.google.com/o/oauth2/token', | |
:audience => 'https://accounts.google.com/o/oauth2/token', | |
:scope => @OAUTH2_SCOPES.join(' '), | |
:issuer => SERVICE_ACCOUNT_EMAIL, | |
:signing_key => key, | |
:person => RESELLER_ADMIN) | |
client.authorization.fetch_access_token! | |
client.retries = 5 | |
reseller = client.discovered_api('reseller', 'v1') | |
siteVerification = client.discovered_api('siteVerification', 'v1') | |
directory = client.discovered_api('admin', 'directory_v1') | |
DOMAIN = 'richiemchotdogs.com' | |
result = client.execute( | |
:api_method => reseller.customers.get, | |
:parameters => { | |
'customerId' => 'richiemchotdogs.com' | |
} | |
) | |
result = client.execute( | |
:api_method => reseller.customers.insert, | |
:body_object => { | |
'customerId' => 'richiemchotdogs.com', | |
'customerDomain' => 'richiemchotdogs.com', | |
'phoneNumber' => '212.565.0000', | |
'alternateEmail' => 'joe@google.com', | |
'postalAddress' => { | |
'contactName' => 'Joe McHotDogs', | |
'organizationName' => 'Mega Hotdocs, LLC', | |
'locality' => 'NYC', | |
'region' => 'New York', | |
'countryCode' => 'US', | |
'postalCode' => '10009', | |
'addressLine1' => '555 HotDog Lane', | |
'addressLine2' => 'Apartment 9001' | |
} | |
} | |
) | |
result = client.execute( | |
:api_method => reseller.subscriptions.insert, | |
:parameters => {'customerId' => DOMAIN}, | |
:body_object => { | |
'customerId' => DOMAIN, | |
'skuId' => 'Google-Apps-For-Business', | |
'plan' => { | |
'planName' => 'TRIAL' | |
}, | |
'seats' => { | |
'numberOfSeats' => 2, | |
'maximumNumberOfSeats' => 2 | |
}, | |
'renewalSettings' => { | |
'renewalType' => 'CANCEL' | |
}, | |
'purchaseOrderId' => 'LOLZ-9001' | |
} | |
) | |
result = client.execute( | |
:api_method => siteVerification.web_resource.get_token, | |
:body_object => { | |
'site' => { | |
'type' => 'INET_DOMAIN', | |
'identifier' => DOMAIN | |
}, | |
'verificationMethod' => 'DNS_TXT' | |
} | |
) | |
puts result.data.token | |
# Execute the site verification. | |
result = client.execute( | |
:api_method => siteVerification.web_resource.insert, | |
:parameters => {'verificationMethod' => 'DNS_TXT'}, | |
:body_object => { | |
'site' => { | |
'type' => 'INET_DOMAIN', | |
'identifier' => DOMAIN | |
}, | |
'verificationMethod' => 'DNS_TXT' | |
} | |
) | |
# Create a user | |
result = client.execute( | |
:api_method => directory.users.insert, | |
:body_object => { | |
'name' => { | |
'givenName' => 'Joe', | |
'familyName' => 'McHotDogs' | |
}, | |
'password' => 'hotdogz1!', | |
'primaryEmail' => 'joemchotdogs@richiemchotdogs.com' | |
} | |
) | |
# Promote to admin | |
result = client.execute( | |
:api_method => directory.users.make_admin, | |
:parameters => {'userKey' => 'joemchotdogs@richiemchotdogs.com'}, | |
:body_object => { | |
'status' => true | |
} | |
) | |
# find subscription, bump seat count + 1 | |
result = client.execute( | |
:api_method => reseller.subscriptions.list, | |
:parameters => {'customerId' => 'richiemchotdogs.com'} | |
) | |
subscriptionId = result.data.subscriptions[0]['subscriptionId'] | |
result = client.execute( | |
:api_method => reseller.subscriptions.change_seats, | |
:parameters => { | |
'customerId' => 'richiemchotdogs.com', | |
'subscriptionId' => subscriptionId | |
}, | |
:body_object => { | |
'numberOfSeats' => 3, | |
'maximumNumberOfSeats' => 3 | |
} | |
) | |
puts result.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment