Last active
February 12, 2016 11:27
-
-
Save cander/5267753 to your computer and use it in GitHub Desktop.
How to write to the Google Plus moments.insert API endpoint from Ruby.
This file contains hidden or 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
# 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 |
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
thank you!