R HTTR::POST Linkedin API upload image using uploadUrl add binary file in body / or httr::upload_file

64 views Asked by At

I'm using R 4.1.2 and HTTR 1.4.7 to publish in Linkedin personal feed.

So I have followed the steps shown by Linkedin and Microsoft, here. I have been able to create a share without media. Nevertheless to upload an image to linkedin first you have to register the image as an asset, which I have done as well. After registering the asset (image), I get stuck at the upload image binary file step using R. I have tried two main ways and getting two different errors. I have successfully uploaded and posted with the image using Postman, but can't manage to replicate using R. I hope someone has an idea on how to solve it.

Register the image

headers = c(
  'Content-Type' = 'application/json',
  'Authorization' = 'Bearer $ACCESS_TOKEN'
)

body = '{
  "registerUploadRequest": {
    "recipes": [
      "urn:li:digitalmediaRecipe:feedshare-image"
    ],
    "owner": "urn:li:person:$PERSONALURN",
    "serviceRelationships": [
      {
        "relationshipType": "OWNER",
        "identifier": "urn:li:userGeneratedContent"
      }
    ]
  }
}';

res <- VERB("POST", url = "https://api.linkedin.com/v2/assets?action=registerUpload", body = body, add_headers(headers))

response <- (content(res, 'parsed'))
toJSON(response,pretty = T)
asset <- response$value$asset
asset
uploadurl <- response$value$uploadMechanism$com.linkedin.digitalmedia.uploading.MediaUploadHttpRequest$uploadUrl
uploadurl

First attempt to upload the image using binary file

To publish the image I have tried the following two options:

  1. Get binary data from image and send it in the body as raw

headers = c(
  'Content-Type' = 'application/octet-stream',
  'Authorization' = 'Bearer $ACCESS_TOKEN
)

body <- readBin(image_path, "raw", file.size(image_path))

res <- VERB("POST", url = uploadurl,
            body = binary,
            encode = "raw",
            add_headers(headers))
res

Response [https://api.linkedin.com/mediaUpload/sp/D4E22AQEm_i4fm5W8JA/uploaded-image/0?ca=vector_feedshare&cn=uploads&m=AQI-VWT0sCsTkgAAAY1-nNP5bpwehXR-TL0gDAnxRqa4f-uKMCw5MQR4Pw&app=216311867&sync=0&v=beta&ut=32dJnR-9eaaX81] Date: 2024-02-07 00:40 Status: 201 Content-Type:

response shows success, nevertheless the image doesn't show once I try to publish it in a share.

Second attempt using httr::upload_file

  1. The second way, I'm trying to do it is by using the httr::upload_file() function,but it won't show in the final post.
headers = c(
  'Content-Type' = 'application/octet-stream',
  'Authorization' = 'Bearer $ACCESS_TOKEN'
)

body <- httr::upload_file(image_path)

res <- VERB("POST", url = uploadurl,
            body = list(y = body),
            add_headers(headers))
res

I have replicated the steps using Postman and it works, but then I copy the httr code as they suggest and it doesn't work.

I'll appreciate your approach.

Publish post using the asset id

headers = c(
  'Authorization' = 'Bearer $ACCESSTOKEN,
  # 'X-RestLi-Method' = 'create',
  # 'Content-Type' = 'application/json',
  'X-RestLi-Protocol-Version' = '2.0.0'
)

data <- list(
  author = "urn:li:person:$PERSONALURN",
  lifecycleState = "PUBLISHED",
  specificContent = list(
    "com.linkedin.ugc.ShareContent" = list(
      shareCommentary = list(
        text = "En esta publicación se cargó la imagen via la API de Linkedin con el paquete HTTR de R, esta fue generada en R con datos de la API del INEGI. #API #R #RStudio #RStudioServer #EC2, #AWS"
      ),
      shareMediaCategory = "IMAGE",
      media = list(
        list(
          status = "READY",
          description = list(
            text = "Indicadores de ocupación y remuneraciones en México"
          ),
          media = asset,
          title = list(
            text = "Información más reciente al día de la publicación"
          )
        )
      )
    )
  ),
  visibility = list(
    "com.linkedin.ugc.MemberNetworkVisibility" = "PUBLIC"
  )
)

json_body <- toJSON(data,auto_unbox = T,pretty = T)

res <- VERB("POST", url = "https://api.linkedin.com/v2/ugcPosts", body = json_body, add_headers(headers),encode = "json")
res_share <- content(res)
res_share

Post url: https://www.linkedin.com/embed/feed/update/urn:li:share:7160802477812928512

0

There are 0 answers