C# 向LinkedIn发布/共享数据

C# 向LinkedIn发布/共享数据,c#,json,asp.net-web-api,http-post,share,C#,Json,Asp.net Web Api,Http Post,Share,我不熟悉Web API。目前,我正在使用C语言的web API方法将数据发布到LinkedIn。我已经在developers.LinkedIn.com上创建了示例应用程序。我能够获得代码和访问令牌。但是,当发布数据或获取个人资料数据时,我得到了401-未经授权的访问错误。以前我可以从链接中获取数据,但现在不行。这是我获得访问令牌后的代码 我在WebResponse WebResponse=request1.GetResponse时出错; 在应用程序的API使用页面中,我可以看到POST Shar

我不熟悉Web API。目前,我正在使用C语言的web API方法将数据发布到LinkedIn。我已经在developers.LinkedIn.com上创建了示例应用程序。我能够获得代码和访问令牌。但是,当发布数据或获取个人资料数据时,我得到了401-未经授权的访问错误。以前我可以从链接中获取数据,但现在不行。这是我获得访问令牌后的代码

我在WebResponse WebResponse=request1.GetResponse时出错; 在应用程序的API使用页面中,我可以看到POST Share使用会增加每个API调用。我正在向用户请求rw_nus权限以写入他的帐户。 我是否写错了URL或需要在通话中发送更多信息? 我应该使用什么URL来获取用户配置文件。我以前使用过这个URL

是否可以将我的web应用程序中的图像或视频共享到LinkedIn

请帮我解决这个问题

string linkedinSharesEndPoint = "https://api.linkedin.com/v1/people     /~/shares";

WebRequest request1 = WebRequest.Create(linkedinSharesEndPoint);
request1.Method = "POST";
request1.Headers.Add("x-li-format", "json");
request1.ContentType = "application/json";

using (var stream = new StreamWriter(request1.GetRequestStream()))
        {
          var shareMsg =
                  new
                  {
                      comment = "hello user",
                      content =
                          new
                          {
                              title = "Api post call"

                          },
                      visibility = new { code = "anyone" }

                  };

            string s = shareMsg.ToString();
            string json = new JavaScriptSerializer().Serialize(shareMsg);
            stream.Write(json);
            stream.Flush();
            stream.Close();
        }
 WebResponse webResponse = request1.GetResponse();
 Stream dataStream = webResponse.GetResponseStream();
 var reader1 = new StreamReader(dataStream);
 string response1 = reader1.ReadToEnd();
 return Request.CreateResponse(HttpStatusCode.OK);  

我已经成功地用php完成了它

首先,数组应该是这样的

$data = [
        'content' => ['title' => 'go ahead BD Team', 
                     'description' => '#riseofthetiger',
                     'submittedUrl' => 'http://yourapp.dev', 
                     'submittedImageUrl' => 'http://yourapp.dev/image.jpg'
                   ],
        'comment' => 'Bangladesh vs India',
        'visibility' => ['code' => 'anyone']

    ];
$headers = array(
            'Content-Type' => 'application/json',
            'x-li-format' => 'json',
        );
你的标题应该是这样的

$data = [
        'content' => ['title' => 'go ahead BD Team', 
                     'description' => '#riseofthetiger',
                     'submittedUrl' => 'http://yourapp.dev', 
                     'submittedImageUrl' => 'http://yourapp.dev/image.jpg'
                   ],
        'comment' => 'Bangladesh vs India',
        'visibility' => ['code' => 'anyone']

    ];
$headers = array(
            'Content-Type' => 'application/json',
            'x-li-format' => 'json',
        );
验证url:&redirect\u uri=&response\u type=code&state=&scope=w\u share

      public String LinkedINAuth(string code, string state)
            {
                string authUrl = "https://www.linkedin.com/oauth/v2/accessToken";

                var sign1 = "grant_type=authorization_code&code=" + code + "&redirect_uri=" + redirectUrl + "&client_id=" + apiKey + "&client_secret=" + apiSecret + "&state=" + state;
                var sign = "grant_type=authorization_code&code=" + HttpUtility.UrlEncode(code) + "&redirect_uri=" + HttpUtility.HtmlEncode(redirectUrl) + "&client_id=" + apiKey + "&client_secret=" + apiSecret + "&state=" + state;

                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
                                                     | SecurityProtocolType.Tls11
                                                     | SecurityProtocolType.Tls12;

                HttpWebRequest webRequest = System.Net.WebRequest.Create(authUrl + "?" + sign1) as HttpWebRequest;
                webRequest.Method = "POST";
                webRequest.Host = "www.linkedin.com";
                webRequest.ContentType = "application/x-www-form-urlencoded";

                Stream dataStream = webRequest.GetRequestStream();

                String postData = String.Empty;
                byte[] postArray = Encoding.ASCII.GetBytes(postData);

                dataStream.Write(postArray, 0, postArray.Length);
                dataStream.Close();

                WebResponse response = webRequest.GetResponse();
                dataStream = response.GetResponseStream();


                StreamReader responseReader = new StreamReader(dataStream);
                String returnVal = responseReader.ReadToEnd().ToString();
                responseReader.Close();
                dataStream.Close();
                response.Close();
                var stri = redirectUrl;
                retval = returnVal.ToString();
                var objects = JsonConvert.DeserializeObject<Accountdsdsd>(retval);
                TokenGlobe = objects.access_token;
                var SentStatus = PostLinkedInNetworkUpdate(TokenGlobe, "Hello API");
                return TokenGlobe;

            }
  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 },
        { "description", description},
        { "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");
        }
         public class Accountdsdsd
    {
        public string access_token { get; set; }
        public string expires_in { get; set; }
    }
    }

}