LinkedIn RestSharp and OAuthBase Example

£可爱£侵袭症+ 提交于 2019-12-23 04:52:42

问题


anyone ever used C# in combination with the library RestSharp and OAuthBase in order get some interaction with LinkedIn?

I'm looking for a working example using these tools to do proper authorization (oAuth 2.0) and to publish a post using the share API on LinkedIn.

So far I've been successful using these tools to obtain valid access tokens (I can use it to obtain profile information for example), but posting via the share API got me stuck on authentication.

Any help would be very much appreciated!!


回答1:


it turned out to be much simpler than I was thinking.... (doesn't it allways?)

The main point to take into account is: oAuth 2.0 does not require signatures, nonce, timestamps, authorization headers ... none of that.

If you want to post on LinkedIn using the sahres API and using oAuth2.0 ... OAuthbase is not needed.

Simply follow the oauth 2.0 authentication flow as described here: http://developer.linkedin.com/documents/authentication

And then you can use the following code as a starting point:

var shareMsg = new
            {
                comment = "Testing out the LinkedIn Share API with JSON",
                content = new
                {
                    title = "Test post to LinkedIn",
                    submitted_url = "http://www.somewebsite.com",
                    submitted_image_url = "http://www.somewebsite.com/image.png"
                },
                visibility = new
                {
                    code = "anyone"
                }
            };

            String requestUrl = "https://api.linkedin.com/v1/people/~/shares?oauth2_access_token=" + accessToken;

            RestClient rc = new RestClient();
            RestRequest request = new RestRequest(requestUrl, Method.POST);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("x-li-format", "json");

            request.RequestFormat = DataFormat.Json;
            request.AddBody(shareMsg);

            RestResponse restResponse = (RestResponse)rc.Execute(request);
            ResponseStatus responseStatus = restResponse.ResponseStatus;

Happy coding!!



来源:https://stackoverflow.com/questions/15601171/linkedin-restsharp-and-oauthbase-example

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