2

我正在使用 DotNetOpenAuth 框架,该框架非常有助于加快处理数据的速度。

我正在使用它与LinkedIn 交谈,并且无法确定向该站点发布/共享信息的正确方法。这是我正在使用的参考 - http://developer.linkedin.com/documents/share-api

当我执行以下代码时,我总是得到以下错误响应;

发送直接消息或获取响应时出错。远程服务器返回错误:(400) 错误请求。

我正在使用 Oauth v1.0a。

这是我的代码;

        try
        {
            HttpDeliveryMethods resourceHttpMethod = HttpDeliveryMethods.PostRequest | HttpDeliveryMethods.AuthorizationHeaderRequest;

            string xml = "<?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>";
            xml += "<content><title>Survey: Social networks top hiring tool - San Francisco Business Times</title>";
            xml += "<submitted-url>http://sanfrancisco.bizjournals.com/sanfrancisco/stories/2010/06/28/daily34.html</submitted-url>";
            xml += "<submitted-image-url>http://images.bizjournals.com/travel/cityscapes/thumbs/sm_sanfrancisco.jpg</submitted-image-url>";
            xml += "</content><visibility><code>anyone</code></visibility></share>";

            var multiparts = new List<MultipartPostPart>();
            MultipartPostPart part = MultipartPostPart.CreateFormPart("body", xml);
            part.PartHeaders[HttpRequestHeader.ContentType] = "application/xml";
            multiparts.Add(part);

            var resourceEndpoint = new MessageReceivingEndpoint("http://api.linkedin.com/v1/people/~/shares", resourceHttpMethod);
            HttpWebRequest request = consumer.PrepareAuthorizedRequest(resourceEndpoint, accessToken, multiparts);
            request.ServicePoint.Expect100Continue = false;

            IncomingWebResponse response = consumer.Channel.WebRequestHandler.GetResponse(request);
            this.resultsBox.Text = response.GetResponseReader().ReadToEnd();
        }
        catch (Exception ex)
        {

        }

谢谢

4

1 回答 1

1

忘了回信,与来自老式谷歌新闻组的人交谈,我得到了我需要的方向,但想在发布之前验证解决方案。

我搞砸的一件大事是在发送请求数据之前添加多部分数据,而不是自己构建请求并将新内容写入流。

我是使用 OAuth 和 REST 的新手,所以我不知道所有细节,我只想说我已经针对 LinkedIn 和 Twitter REST API 验证了这段代码。

这是新的代码;

        try
        {
            Encoding encoder = Encoding.ASCII;
            HttpDeliveryMethods resourceHttpMethod = HttpDeliveryMethods.PostRequest | HttpDeliveryMethods.AuthorizationHeaderRequest;

            byte[] bytes = encoder.GetBytes(xd.OuterXml.ToString());

            var resourceEndpoint = new MessageReceivingEndpoint(ResourceURL, resourceHttpMethod);
            HttpWebRequest request = _consumer.PrepareAuthorizedRequest(resourceEndpoint, _manager.AccessToken);
            request.ServicePoint.Expect100Continue = false;

            request.Method = "POST";
            request.ContentLength = bytes.Length;
            request.ContentType = "application/xml";

            using (System.IO.Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(bytes, 0, bytes.Length);
            }

            IncomingWebResponse response = _consumer.Channel.WebRequestHandler.GetResponse(request);

            ret = true;
        }
        catch (Exception ex)
        {
            _Logger.Error(OpenLogger.FormatException(ex));
        }
于 2012-04-29T02:05:45.887 回答