Skip to content

Instantly share code, notes, and snippets.

@simonmorley
Created March 26, 2016 13:02
Show Gist options
  • Save simonmorley/1d41a9e1b3cc718e11ec to your computer and use it in GitHub Desktop.
Save simonmorley/1d41a9e1b3cc718e11ec to your computer and use it in GitHub Desktop.
require "base64"
require "google/api_client"
key_file = "./google.p12"
key_secret = 'notasecret'
key = Google::APIClient::KeyUtils.load_from_pkcs12(key_file, key_secret)
service_account_email = '@developer.gserviceaccount.com'
auth_params = {
token_credential_uri: "https://accounts.google.com/o/oauth2/token",
audience: "https://accounts.google.com/o/oauth2/token",
scope: "https://www.googleapis.com/auth/pubsub",
issuer: service_account_email,
signing_key: key
}
client = Google::APIClient.new(
application_name: "pubsubtest", application_version: "v1",
authorization: Signet::OAuth2::Client.new(auth_params)
)
client.authorization.fetch_access_token!
pubsub_api = client.discovered_api("pubsub", "v1beta2")
r = client.execute(
api_method: pubsub_api.projects.topics.publish,
parameters: {
topic: "projects/xxx/topics/topic1"
},
body_object: { messages: [{ data: Base64.strict_encode64("hi simon, how is it hanging?") }] }
)
if r.error?
p r.inspect
raise "fail to publish"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment