How to use LinkedIn's Share API with the Rails Gem - ruby-on-rails

I'm running Rails 3, with the LinkedIn API gem: here
I've been unable to get the Share API working as described here
This works fine:
response = client.add_share(:comment => 'new share API')
This Fails with:
response = client.add_share(:comment => 'new share API', :title => 'Linkedin Share API', :url => 'http://developer.linkedin.com/docs/DOC-1212', :image_url => 'http://images.bizjournals.com/travel/cityscapes/thumbs/sm_sanfrancisco.jpg')
Error:
LinkedIn::Errors::GeneralError ((400): Invalid xml {Expected elements 'post-network-update#http://api.linkedin.com/v1 id#http://api.linkedin.com/v1 visibility#http://api.linkedin.com/v1 comment#http://api.linkedin.com/v1 attribution#http://api.linkedin.com/v1 content#http://api.linkedin.com/v1 private-message#http://api.linkedin.com/v1 share-target-reach#http://api.linkedin.com/v1' instead of 'image-url#http://api.linkedin.com/v1' here in element share#http://api.linkedin.com/v1, Expected elements 'post-network-update#http://api.linkedin.com/v1 id#http://api.linkedin.com/v1 attribution#http://api.linkedin.com/v1 content#http://api.linkedin.com/v1 private-message#http://api.linkedin.com/v1 share-target-reach#http://api.linkedin.com/v1' instead of 'url#http://api.linkedin.com/v1' here in element share#http://api.linkedin.com/v1}):
Any ideas? Thanks

You're doing it wrong. This is the XML in of sample request in https://developer.linkedin.com/documents/share-api#toggleview:id=xml
<share>
<comment>Check out the LinkedIn Share API!</comment>
<content>
<title>LinkedIn Developers Documentation On Using the Share API</title>
<description>Leverage the Share API to maximize engagement on user-generated content on LinkedIn</description>
<submitted-url>https://developer.linkedin.com/documents/share-api</submitted-url>
<submitted-image-url>http://m3.licdn.com/media/p/3/000/124/1a6/089a29a.png</submitted-image-url>
</content>
<visibility>
<code>anyone</code>
</visibility>
</share>
So the request should look something like this based in the sample request:
response = client.add_share(:comment => 'Sample Job',
:content => { :title => 'LinkedIn Developers Documentation On Using the Share API', :description => 'Leverage the Share API to maximize engagement on user-generated content on LinkedIn', :'submitted-url' => 'https://developer.linkedin.com/documents/share-api', :'submitted-image-url' => 'http://m3.licdn.com/media/p/3/000/124/1a6/089a29a.png' } )

Related

Facebook og:tags not working in my rails application

I am trying to add og:tags for sharing my URL on facebook. below is my code
in my view file,
- content_for :og_meta_tags do
%meta{:content => "#{FbAppId}", :property => "fb:app_id"}/
%meta{:content => "Travelibro blog", :property => "og:name"}
%meta{:content => #journey.photos.first.image.url, :property => "og:image"}
%meta{:content => "website", :property => "og:type"}/
%meta{:content => user_journey_url, :property => "og:url"}/
%meta{:content => "Travelibro - #{#journey.name}", :property => "og:title"}/
%meta{:content => "The TraveLibro app allows your to capture your live journeys On-The-Go with simple check-ins, picture uploads and thoughts to create a beautiful timeline of your travels. It enables friends travelling together to share experiences and post moments to a shared timeline and loved ones to see them in real time.", :property => "og:description"}
and my application.html.haml file contains the below code,
<!doctype html>
%html
%head{:prefix => "og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# website: http://ogp.me/ns/website#"}
%link{:href => "http://test.com", :rel => "canonical"}
= yield :og_meta_tags
.
.
.
but, when I scrape a new information from facebook debugger tool, I get the following errors,
a required property 'og:title' of type 'string' was not provided.
It's not picking up my og:tags at all.
For example, below is the URL to be share on the facebook :
http://travelibro.com/monish-shah/journeys/bangalore-day-trip-2149
when I put this URL to facebook debugger tool, I get the following error
Object at URL 'http://travelibro.com/monish-shah/journeys/bangalore-day-trip-2149' of type 'website' is invalid because a required property 'og:title' of type 'string' was not provided.

How to track API calls on my ruby on rails app using google analytics?

I created an API for external applications to log in or make specific web calls using OAuth. What I'm looking for is a way to track the number of times these API calls are being used.
Is there an option for me?
You could send the events with the measurement protocol.
require "net/http"
require "uri"
uri = URI.parse("http://www.google-analytics.com/collect")
Net::HTTP.post_form(uri, {"v" => "1",
"tid" => "UA-XXXX-1",
"cid" => "555",
"t" => "event",
"ec" => "API",
"ea" => "request",
"el" => "data/get",
"ev" => "5"})
I believe you can do this with Google Events:
https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide
There are various Ruby libraries for interacting with GA.

Writing general notes for API docs using swagger-docs

I am using swagger to document my Rails REST API using swagger-docs. I am able to document every method but the problem is that I want to write general guidelines() for using API. I cannot find any method that allows me to do so. Any help will be appreciated.
Something as shown in the image below:
You just need to set the "Description" attribute to some appropriate text when you initially configure Swagger.
Have a look at the first example from https://github.com/richhollis/swagger-docs:
Swagger::Docs::Config.register_apis({
"1.0" => {
# the extension used for the API
:api_extension_type => :json,
# the output location where your .json files are written to
:api_file_path => "public/api/v1/",
# the URL base path to your API
:base_path => "http://api.somedomain.com",
# if you want to delete all .json files at each generation
:clean_directory => false,
# add custom attributes to api-docs
:attributes => {
:info => {
"title" => "Swagger Sample App",
"description" => "This is a sample description.",
"termsOfServiceUrl" => "http://helloreverb.com/terms/",
"contact" => "apiteam#wordnik.com",
"license" => "Apache 2.0",
"licenseUrl" => "http://www.apache.org/licenses/LICENSE-2.0.html"
}
}
}
})
The example app at petstore.swagger.io shows you the general output and layout you'll get.

How to pull Google Analytics stats?

Is Google API Ruby client the best option?
I have a site example.com with users and I want them to see their google analytics stats on example.com, how can I do it ?
I can see the example but I'm not able to figure out how to begin.
I also use the google-api-ruby-client gem and set it up about the same way that is outlined in the link you provided (https://gist.github.com/joost/5344705).
Just follow the steps outlined in the link to set up a Google Analytics client:
# you need to set this according to your situation/needs
SERVICE_ACCOUNT_EMAIL_ADDRESS = '...' # looks like 12345#developer.gserviceaccount.com
PATH_TO_KEY_FILE = '...' # the path to the downloaded .p12 key file
PROFILE = '...' # your GA profile id, looks like 'ga:12345'
require 'google/api_client'
# set up a client instance
client = Google::APIClient.new
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 => 'https://www.googleapis.com/auth/analytics.readonly',
:issuer => SERVICE_ACCOUNT_EMAIL_ADDRESS,
:signing_key => Google::APIClient::PKCS12.load_key(PATH_TO_KEY_FILE, 'notasecret')
).tap { |auth| auth.fetch_access_token! }
api_method = client.discovered_api('analytics','v3').data.ga.get
# make queries
result = client.execute(:api_method => api_method, :parameters => {
'ids' => PROFILE,
'start-date' => Date.new(1970,1,1).to_s,
'end-date' => Date.today.to_s,
'dimensions' => 'ga:pagePath',
'metrics' => 'ga:pageviews',
'filters' => 'ga:pagePath==/url/to/user'
})
puts result.data.rows.inspect
To display statistics for a user's page in your app, you have to adjust the metrics and filters parameters when making the query. The query above for example will return a result object containing all pageviews for the page with url example.com/url/to/user.
Caveat: this answer was written a long time ago and Google released a new, incompatible version of the gem. Please consult https://github.com/google/google-api-ruby-client/blob/master/MIGRATING.md

Twitter User Hash for Sorcery Authentication

I'm using the Sorcery gem and their external module to authenticate with Twitter. I've got the authentication working, but I want to store the user's Twitter profile image URL in my database after a successful log in. Sorcery seems to have a configuration option that's meant to do exactly what I want:
config.twitter.user_info_mapping = {:nickname => "screen_name"}
Maybe I've missed something in the Sorcery documentation, but I can't find any information about what "keys" are available. I tried this to no avail:
config.twitter.user_info_mapping = {:nickname => "screen_name", :avatar_url => "profile_image_url"}
Has anyone found documentation about this?
That is just what you get from twitter in json format.
Here is a twitter documentation about it https://dev.twitter.com/docs/api/1/get/account/verify_credentials
config.twitter.user_info_mapping = {:username => "screen_name",
:realname => "name",
:location => "place",
:web => "url",
:bio => "description"}

Resources