1
votes

I'm trying to share my content via REST API. Backend - Ruby/Rails

Initially, I try gems 'linkedin', 'linkedin-oauth2', but I got error "Access to posting shares denied"

gem 'linkedin'

consumer_options = {
:request_token_path => "/uas/oauth/requestToken?scope=r_basicprofile+rw_nus",
:access_token_path  => "/uas/oauth/accessToken",
:authorize_path     => "/uas/oauth/authorize",
:api_host           => "https://api.linkedin.com",
:auth_host          => "https://www.linkedin.com"
}

client = LinkedIn::Client.new('your_consumer_key', 'your_consumer_secret', consumer_options)
client.authorize_from_access(token, token_secret)
client.add_share(:comment => 'test gempost')

gem 'linkedin-oauth2'

api = LinkedIn::API.new(access_token)
api.add_share(content: "hi")

Then I followed instructions from here https://developer.linkedin.com/docs/share-on-linkedin using HTTParty, but get error "Access to posting shares denied" again.

data = { comment: message, visibility: {code: 'anyone'} }

response = HTTParty.post("https://api.linkedin.com/v1/people/~/shares?format=json", 
          headers: { 'Content-Type' => 'application/json'}, 
          query: {oauth2_access_token: linkedin_identity.token}, 
          body: data.to_json)

I have already given all permissions to my application - what can be a reason of getting such error? Did anybody have such experience with posting to Linkedin via API?

1
Did you solved your error. Can you post that solution here as I am facing same issue so may be its help me. Thanksuser1780370

1 Answers

0
votes

try this

data = { comment: message, visibility: {code: 'anyone'} }

response = HTTParty.post("https://api.linkedin.com/v1/people/~/shares?format=json",
  headers: { 'Content-Type' => 'application/json',
    'Authorization' => "Bearer #{your_access_token}"},
  body: data.to_json
)