Cannot share on LinkedIn using LinkedIn API v. 2
Asked Answered
F

3

5

I am having difficulty getting a share on LinkedIn. I am trying to post a LinkedIn share via LinkedIn API V2 and every time I make the post request I get a request timed out (status 504) answer from the server. Here is my code :

myPost = {
      'author': 'urn:li:person:' + this.uid,
      'lifecycleState': 'PUBLISHED',
      'specificContent': {
          'com.linkedin.ugc.ShareContent': {
              'shareCommentary': {
                  'text': 'Hello World! This is my first Share on LinkedIn!'
              },
              'shareMediaCategory': 'NONE'
          }
      },
      'visibility': {
          'com.linkedin.ugc.MemberNetworkVisibility': 'PUBLIC'
      }
  }

header = {
  'Content-Type': 'application/json',
  'X-Restli-Protocol-Version': '2.0.0',
  'Authorization': 'Bearer ' + token
};

this.http.post('https://api.linkedin.com/v2/ugcPosts', myPost, header).then(res => {
    alert(JSON.stringify(res));
  })
  .catch(err => {
    alert(JSON.stringify(err));
  });

And here is the error message :

{
    "message": "Request timed out",
    "status": 504
}

It's an angular-ionic project and I use the native cordova-plugin-advanced-http to make my post request. I had no issue to sign in with LinkedIn, get my access token and retrieve data from LinkedIn using the same native plugin and the LinkedIn API V2.

On my LinkedIn developer account, to the usage & limits page, I can see the API call to create method.

Farhi answered 22/2, 2019 at 23:44 Comment(2)
Hi, welcome to StackOverflow! It often helps to get an answer if you include what you have already tried and how it did not fit your needs. Also details about error messages you get when you attempt to perform your share would be useful to help diagnose the issue.Necromancy
If you're still encountering timeout issues, please submit a ticket to linkedin.zendesk.com and include the response headers received from your POST request.Pollination
P
6

Timeouts related to POST endpoints in LinkedIn API v2 can happen if LinkedIn is unable to parse the request body. The solution to the example posted here is to turn myPost into a proper JSON string, eg. json.dumps(myPost).

The timeout can also happen if missing the 'Content-Type': 'application/json' header which just bit me while playing with their API using ruby.

Publea answered 27/6, 2019 at 7:36 Comment(1)
Oh man. The missing 'Content-Type': 'application/json' thing did wonders for me. Before this, 10% of my requests were passing. Thank you.Brachio
M
0

helped for me with same issue on python

head = {
'Authorization': 'Bearer '+token, 'X-Restli-Protocol-Version': '2.0.0'
}

body = {
    "author": 'urn:li:person:'+ID,
    "lifecycleState": "PUBLISHED",
    "specificContent": {
        "com.linkedin.ugc.ShareContent": {
            "shareCommentary": {
                "text": "Hello World! This is my first Share on LinkedIn!"
            },
            "shareMediaCategory": "NONE"
        }
    },
    "visibility": {
        "com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC"
    }
}

requests.post('https://api.linkedin.com/v2/ugcPosts', data=json.dumps(body),headers=head)
Mensch answered 31/3, 2019 at 17:49 Comment(1)
i get this issue " Error parsing request body to json Illegal unquoted character ((CTRL-CHAR, code 10)): has to be escaped using backslash to be included in string value\n at [Source: (com.linkedin.data.ByteString$ByteArrayVectorInputStream); line: 7, column: 249]" when my post has "-".Stradivarius
N
0

There's really no reason to be using an obfuscated, js-lib plugin to just share a link on LinkedIn. All you need is:

https://www.linkedin.com/sharing/share-offsite/?url={url}

Source: Microsoft LinkedIn Share URL Documentation.

For example, this works for me:

https://www.linkedin.com/sharing/share-offsite/?url=http://www.wikipedia.org/

Works fine:

No authentication problem at all. Then just make your own button and hyperlink it. No problem.

If you are interested in a regularly maintained GitHub project that keeps track of this so you don't have to, check it out! Social Share URLs

Social Share URLs Image

Naissant answered 17/5, 2020 at 15:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.