Skip to content

Instantly share code, notes, and snippets.

@cander

cander/gist:5267753

Last active Feb 12, 2016
Embed
What would you like to do?
How to write to the Google Plus moments.insert API endpoint from Ruby.
# This Sinatra endpoint is something I added to the Google Ruby quick-start
# web app - https://developers.google.com/+/quickstart/ruby
# It demonstrates how to write to the moments.insert API, for which I
# couldn't find any Ruby examples.
post '/write_activity' do
# Check for stored credentials in the current user's session.
if !session[:token]
halt 401, 'User not connected.'
end
# Authorize the client and construct a Google+ service.
$client.authorization.update_token!(session[:token].to_hash)
plus = $client.discovered_api('plus', 'v1')
moment = {
:type => 'http://schemas.google.com/AddActivity',
:target => { :id => Time.now.to_i.to_s,
:description => "description doesn't seem to show up",
:name => 'An example of an AddActivity w/o URL',
:image => 'http://upload.wikimedia.org/wikipedia/en/thumb/3/33/Office_space_album_cover.png/220px-Office_space_album_cover.png' },
}
# post a moment/activity to the vault/profile
req_opts = { :api_method => plus.moments.insert,
:parameters => { :collection => 'vault', :userId => 'me', },
:body_object => moment,
}
response = $client.execute(req_opts).body
# I didn't bother with AJAX for this proof-of-concept:
"<html><body><pre>#{response.inspect}</pre></body></html>"
end
@alisnic

This comment has been minimized.

Copy link

@alisnic alisnic commented Mar 19, 2015

thank you!

@zauzaj

This comment has been minimized.

Copy link

@zauzaj zauzaj commented Feb 12, 2016

Hi,
I'm working on posting activity to user stream on Google Plus via RoR and I tried to use your solution but always getting this error:

undefined method 'moments' for Google::APIClient::API:0x3fc05fc78594 ID:plus:v1

Do you know how I can solve it or maybe something is changed cuz I see it's not active anymore ?

Regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment