Post to Linkedin using Share Api

余生长醉 提交于 2019-11-30 05:38:25

问题


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://bit.ly/cCpeOD</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


回答1:


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.



来源:https://stackoverflow.com/questions/11464330/post-to-linkedin-using-share-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!