LinkedIn RestSharp和OAuthBase示例

时间:2013-03-24 16:46:22

标签: oauth-2.0 linkedin restsharp

有人曾经将C#与库RestSharpOAuthBase结合使用,以便与LinkedIn进行一些互动吗?

我正在寻找使用这些工具进行适当授权(oAuth 2.0)并使用LinkedIn上的共享API发布帖子的工作示例。

到目前为止,我已成功使用这些工具获取有效的访问权限(例如,我可以用它来获取配置文件信息),但通过共享API发布使我无法进行身份验证。

非常感谢任何帮助!!

1 个答案:

答案 0 :(得分:1)

事实证明它比我想的要简单得多....(不是总是这样吗?)

要考虑的要点是:oAuth 2.0不需要签名,nonce,时间戳,授权标头......都不是。

如果您想使用sahres API在LinkedIn上发布并使用oAuth2.0 ...则不需要OAuthbase。

只需按照此处所述的oauth 2.0身份验证流程: http://developer.linkedin.com/documents/authentication

然后您可以使用以下代码作为起点:

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;

快乐的编码!!

相关问题