You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用R的httr包调用LinkedIn API上传图片失败求助

解决方案

关键问题定位

LinkedIn 资产上传对请求格式要求严格,你遇到的问题大概率是请求头或二进制数据处理不符合平台规范,以下是针对性的修复步骤和代码示例:


1. 确保图片上传请求的核心配置正确

LinkedIn 要求图片上传使用 PUT 请求,且请求头必须指定对应图片的 MIME 类型(而非默认的 multipart/form-data),同时要保证二进制数据读取完整。

示例代码:

library(httr)
library(mime)

# 替换为你的图片路径
img_path <- "/path/to/your/image.jpg"
# 替换为注册资产后得到的 uploadUrl
upload_url <- "YOUR_UPLOAD_URL_FROM_ASSET_REGISTRATION"
# 替换为你的 LinkedIn 访问令牌
access_token <- "YOUR_LINKEDIN_ACCESS_TOKEN"

# 获取图片对应的 MIME 类型(自动识别 jpg/png 等)
mime_type <- guess_type(img_path)

# 完整读取图片二进制数据(避免截断)
file_size <- file.info(img_path)$size
img_raw <- readBin(img_path, what = "raw", n = file_size)

# 发送 PUT 请求上传图片
upload_response <- PUT(
  url = upload_url,
  add_headers(
    "Authorization" = paste("Bearer", access_token),
    "Content-Type" = mime_type,
    "Content-Length" = as.character(file_size)  # 显式指定文件大小,确保对齐
  ),
  body = img_raw
)

# 验证上传结果
stop_for_status(upload_response)
cat("图片上传状态码:", status_code(upload_response), "\n")

2. 核对资产注册环节的参数

如果上传成功但动态不显示图片,需确认资产注册时的参数是否正确:

  • fileSize 必须和实际图片文件大小完全一致
  • owner 格式为 urn:li:person:你的个人ID
  • mediaTypeFamily 设为 "IMAGE"

资产注册示例代码:

# 注册资产获取 uploadUrl 和 assetUrn
asset_reg_response <- POST(
  url = "https://api.linkedin.com/v2/assets?action=registerUpload",
  add_headers(
    "Authorization" = paste("Bearer", access_token),
    "Content-Type" = "application/json",
    "X-Restli-Protocol-Version" = "2.0.0"
  ),
  body = list(
    registerUploadRequest = list(
      owner = "urn:li:person:YOUR_PERSON_ID",
      fileSize = as.integer(file_size),
      uploadMechanism = list(
        "com.linkedin.digitalmedia.uploading.MediaUploadHttpRequest" = list(headers = list())
      ),
      mediaTypeFamily = "IMAGE",
      mediaSpecificContent = list(
        image = list(
          displayDescription = list(
            locale = list(language = "en", country = "US"),
            value = "图片描述文本"
          )
        )
      )
    )
  ),
  encode = "json"
)

# 提取 uploadUrl 和 assetUrn
asset_data <- content(asset_reg_response)
upload_url <- asset_data$value$uploadMechanism[[1]]$uploadUrl
asset_urn <- asset_data$value$asset

3. 发布动态时正确关联资产

发布动态时必须引用资产的 assetUrn,并将 media.status 设为 "READY":

# 发布带图片的动态
share_response <- POST(
  url = "https://api.linkedin.com/v2/shares",
  add_headers(
    "Authorization" = paste("Bearer", access_token),
    "Content-Type" = "application/json",
    "X-Restli-Protocol-Version" = "2.0.0"
  ),
  body = list(
    owner = "urn:li:person:YOUR_PERSON_ID",
    text = list(text = "动态正文内容"),
    content = list(
      media = list(
        status = "READY",
        description = list(text = "图片描述"),
        media = asset_urn,
        title = list(text = "图片标题")
      )
    )
  ),
  encode = "json"
)

4. 调试对比请求差异

如果仍未解决,使用 verbose() 打印 R 请求的详细信息,和 Postman 的请求做对比,重点检查:

  • 请求头中的 Content-Type、Content-Length 是否一致
  • 二进制数据的长度是否和文件大小匹配
# 启用调试模式发送请求
upload_response <- PUT(
  url = upload_url,
  add_headers(
    "Authorization" = paste("Bearer", access_token),
    "Content-Type" = mime_type
  ),
  body = img_raw,
  verbose()  # 打印完整请求细节
)

内容的提问来源于stack exchange,提问作者Jorge Heriberto Chavez Rosales

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 19:37:09