【问题标题】:How to upload image to LinkedIn for Ugc image post in C#如何将图像上传到 LinkedIn 以在 C# 中发布 Ugc 图像
【发布时间】:2019-05-23 14:14:07
【问题描述】:

所以我正在关注 Linkedin Documentation 在 Linkedin 上实现 Create Image Share。

文档列出了三个步骤:

  1. 注册要上传的图片。
  2. 将您的图片上传到 LinkedIn。
  3. 创建图像共享。

虽然我能够执行第一步以获取 uploadUrl,但在执行第 2 步时收到 400 响应错误和空白错误消息。

文档将第二步列为:

curl -i --upload-file /Users/peter/Desktop/superneatimage.png --header "Authorization: Bearer redacted" 'https://api.linkedin.com/mediaUpload/C5522AQGTYER3k3ByHQ/feedshare-uploadedImage/0?ca=vector_feedshare&cn=uploads&m=AQJbrN86Zm265gAAAWemyz2pxPSgONtBiZdchrgG872QltnfYjnMdb2j3A&app=1953784&sync=0&v=beta&ut=2H-IhpbfXrRow1'

这是我在 C# 代码中的第 2 步:

private bool UploadImageBinaryFile(RequestUploadUrlResponse uploadDetails)
    {
        using (HttpClient client = new HttpClient())
        {
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", #access_token#);
            client.DefaultRequestHeaders.Add("X-Restli-Protocol-Version", "2.0.0");
            MultipartFormDataContent form = new MultipartFormDataContent();
            string fileAddress = image_path + "image.png";
            byte[] fileBytes = File.ReadAllBytes(fileAddress);
            string name = "upload-file";
            form.Add(new ByteArrayContent(fileBytes), name);
            HttpResponseMessage response = client.PutAsync
                (
                    uploadDetails.value.uploadMechanism.mediaUploadHttpRequest.uploadUrl,
                    form
            ).Result;

            if (response.IsSuccessStatusCode)//<--getting 400 error Bad Request here
            {
                string responseBody = response.Content.ReadAsStringAsync().Result;
                return true;
            }
            else
            {
                ErrorResponseHandler(response.Content.ReadAsStringAsync().Result);
                return false;
            }
        }
    }

我已确保从第一步开始就可以成功上传uploadUrl。当我使用 URL https://api.linkedin.com/v2/assets/#id# 检查状态时,我能够看到状态为 WAITING_UPLOAD

我哪里错了?

【问题讨论】:

    标签: c# linkedin-api


    【解决方案1】:

    我找到了解决方案,基本上我们只需要在内容中传递二进制文件,而我是在表单中传递它。

    using (HttpClient client = new HttpClient())
    {
        try
        {
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accesstoken);
            var content = new ByteArrayContent(File.ReadAllBytes(fileAddress));
            HttpResponseMessage response = client.PutAsync(url, content).Result;
            if (response.IsSuccessStatusCode)
            {
                //response is empty. Have to call the checkAssetStatus to see if the asset is 'AVAILABLE'
                string responseBody = response.Content.ReadAsStringAsync().Result;
    
            }
            else
            {
                //handleError();
            }
        }
        catch (HttpRequestException ex)
        {
            Console.WriteLine("\nException Caught!");
            Console.WriteLine("Message :{0} ", ex.Message);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-04-15
      • 1970-01-01
      • 2023-01-25
      • 2015-05-17
      • 2012-06-10
      • 2016-04-02
      • 2014-06-09
      • 1970-01-01
      • 2020-11-04
      相关资源
      最近更新 更多