Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post to LinkedIn using Share API

I'm facing difficulty in implementing LinkedIN Share Api in Asp.net application . Can any one help me ? I found the documentation for the LinkedIN Share API (https://developer.linkedin.com/documents/share-api). It is saying that i should create a XML for sharing and should post this to the URL "http://api.linkedin.com/v1/people/~/shares"

I have two doubts after reading this document

  1. How to pass the tokens to the server along with the XML, it is not told in the documentation?
  2. What should be he name/key of the XML Content Posted?

Requirement is : I need to share an update ("just a text ) to a users linked in account. The text to share is given by the user through a text box(so it will only contain text)

I'm Using LinkedIn OAuth Library 0.6.1 For Authentication. Since i didn't find any method (that helps to post) in this library, i'm planning to use Share API directly. And Post to Linkedin by Using the class "HttpWebRequest"

What i did till now:

1.Created a app in linked in, so i got App-Key and App-Key 2.In order to Authenticate, redirects the user to linked in using the BeginAuthMethod in the OAuth Library 0.6.1 like this

var token = OAuthManager.Current.CreateToken(callback: this.AppRedirectUrl);
OAuthManager.Current.BeginAuth (token, endResponse: true, displayAllowDenyScreen: false);

3.After Authentication i receive the response from linked in and i use that auth-token to fetch the usertoken like this

var token = OAuthManager.Current.GetTokenInCallback();
var session = OAuthManager.Current.CompleteAuth(token);
this.UserToken = token.Token;
this.UserSecret = token.TokenSecret;

4.I have created a XML somewhat like this :

<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<share>
    <comment>83% of employers will use social media to hire: 78% LinkedIn, 55% Facebook, 45% Twitter [SF Biz Times] http://example.com</comment>
    <content>
         <title>Survey: Social networks top hiring tool - San Francisco Business Times</title>
    </content>
    <visibility>
        <code>anyone</code>
    </visibility>
</share>

5.So now i have the App-Key, App-Secret, User-Token and User-Secret , and i have the xml and the url to post (ie http://api.linkedin.com/v1/people/~/shares)

How to post this xml to the url using the tokens?. Can any one give some/any information regarding this?

I came across an example in java doing the same. the link is "https://developer.linkedin.com/documents/writing-linkedin-apis".But i need this in .NET

like image 204
shajivk Avatar asked Jul 13 '12 04:07

shajivk


People also ask

What is Ugcpost?

UGC Post is a content API best suited for creating and retrieving posts for organic video posts and video ads. For other media types, it is advised to use the Shares API. As of now, you cannot retrieve the actual video content of a video post. Organic video statistics are available.


1 Answers

Use this method to post to LinkedIn shares. The method assumes that you have the accesstoken handy.

private string linkedinSharesEndPoint = "https://api.linkedin.com/v1/people/~/shares?oauth2_access_token={0}";
private const string defaultUrl = "some-url";
private const string defaultImageUrl = "some-image-url";

public bool PostLinkedInNetworkUpdate(string accessToken, string title, string submittedUrl = defaultUrl, string submittedImageUrl = defaultImageUrl)
{
    var requestUrl = String.Format(linkedinSharesEndPoint, accessToken);
    var message = new
    {
        comment = "Testing out the LinkedIn Share API with JSON",
        content = new Dictionary<string, string>
        { { "title", title },
            { "submitted-url", submittedUrl },
            {"submitted-image-url" , submittedImageUrl}
        },
        visibility = new
        {
            code = "anyone"
        }
    };

    var requestJson = new JavaScriptSerializer().Serialize(message);

    var client = new WebClient();
    var requestHeaders = new NameValueCollection
    {
        { "Content-Type", "application/json" },
        { "x-li-format", "json" }
    };
    client.Headers.Add(requestHeaders);
    var responseJson = client.UploadString(requestUrl, "POST", requestJson);
    var response = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(responseJson);
    return response.ContainsKey("updateKey");
}

Please note that I have made the submittedUrl and the submittedImageUrl optional.

like image 57
naveen Avatar answered Oct 06 '22 00:10

naveen