Skip to content

Commit 9b1457f

Browse files
authored
feat(linkedin): upload OG image as thumbnail for article posts (#8)
1 parent b63fbf6 commit 9b1457f

File tree

1 file changed

+103
-25
lines changed

1 file changed

+103
-25
lines changed

.github/workflows/linkedin-post.yml

Lines changed: 103 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -72,33 +72,111 @@ jobs:
7272
# Build the post payload
7373
echo "Creating LinkedIn post..."
7474
75+
THUMBNAIL_URN=""
76+
7577
if [ -n "$ARTICLE_URL" ]; then
78+
# Fetch OG image from URL
79+
echo "Fetching OG image from: $ARTICLE_URL"
80+
OG_IMAGE=$(curl -s "$ARTICLE_URL" | grep -oP '(?<=property="og:image" content=")[^"]+' | head -1)
81+
82+
if [ -n "$OG_IMAGE" ]; then
83+
echo "Found OG image: $OG_IMAGE"
84+
85+
# Download the image
86+
curl -s -L -o /tmp/og_image "$OG_IMAGE"
87+
88+
if [ -f /tmp/og_image ] && [ -s /tmp/og_image ]; then
89+
# Initialize image upload
90+
echo "Initializing LinkedIn image upload..."
91+
INIT_RESPONSE=$(curl -s -X POST \
92+
"https://api.linkedin.com/rest/images?action=initializeUpload" \
93+
-H "Authorization: Bearer $ACCESS_TOKEN" \
94+
-H "Content-Type: application/json" \
95+
-H "LinkedIn-Version: 202501" \
96+
-H "X-Restli-Protocol-Version: 2.0.0" \
97+
-d "{\"initializeUploadRequest\":{\"owner\":\"$PERSON_URN\"}}")
98+
99+
UPLOAD_URL=$(echo "$INIT_RESPONSE" | jq -r '.value.uploadUrl // empty')
100+
IMAGE_URN=$(echo "$INIT_RESPONSE" | jq -r '.value.image // empty')
101+
102+
if [ -n "$UPLOAD_URL" ] && [ -n "$IMAGE_URN" ]; then
103+
echo "Uploading image to LinkedIn..."
104+
UPLOAD_RESPONSE=$(curl -s -w "\n%{http_code}" -X PUT "$UPLOAD_URL" \
105+
-H "Authorization: Bearer $ACCESS_TOKEN" \
106+
--upload-file /tmp/og_image)
107+
108+
UPLOAD_CODE=$(echo "$UPLOAD_RESPONSE" | tail -1)
109+
if [ "$UPLOAD_CODE" == "201" ] || [ "$UPLOAD_CODE" == "200" ]; then
110+
echo "Image uploaded successfully: $IMAGE_URN"
111+
THUMBNAIL_URN="$IMAGE_URN"
112+
else
113+
echo "Warning: Image upload failed with code $UPLOAD_CODE"
114+
fi
115+
else
116+
echo "Warning: Could not initialize image upload"
117+
echo "Response: $INIT_RESPONSE"
118+
fi
119+
fi
120+
else
121+
echo "No OG image found"
122+
fi
123+
76124
# Post with article share
77-
POST_PAYLOAD=$(jq -n \
78-
--arg author "$PERSON_URN" \
79-
--arg text "$POST_TEXT" \
80-
--arg url "$ARTICLE_URL" \
81-
--arg title "$ARTICLE_TITLE" \
82-
--arg desc "$ARTICLE_DESC" \
83-
'{
84-
"author": $author,
85-
"commentary": $text,
86-
"visibility": "PUBLIC",
87-
"distribution": {
88-
"feedDistribution": "MAIN_FEED",
89-
"targetEntities": [],
90-
"thirdPartyDistributionChannels": []
91-
},
92-
"content": {
93-
"article": {
94-
"source": $url,
95-
"title": $title,
96-
"description": $desc
97-
}
98-
},
99-
"lifecycleState": "PUBLISHED",
100-
"isReshareDisabledByAuthor": false
101-
}')
125+
if [ -n "$THUMBNAIL_URN" ]; then
126+
POST_PAYLOAD=$(jq -n \
127+
--arg author "$PERSON_URN" \
128+
--arg text "$POST_TEXT" \
129+
--arg url "$ARTICLE_URL" \
130+
--arg title "$ARTICLE_TITLE" \
131+
--arg desc "$ARTICLE_DESC" \
132+
--arg thumb "$THUMBNAIL_URN" \
133+
'{
134+
"author": $author,
135+
"commentary": $text,
136+
"visibility": "PUBLIC",
137+
"distribution": {
138+
"feedDistribution": "MAIN_FEED",
139+
"targetEntities": [],
140+
"thirdPartyDistributionChannels": []
141+
},
142+
"content": {
143+
"article": {
144+
"source": $url,
145+
"title": $title,
146+
"description": $desc,
147+
"thumbnail": $thumb
148+
}
149+
},
150+
"lifecycleState": "PUBLISHED",
151+
"isReshareDisabledByAuthor": false
152+
}')
153+
else
154+
POST_PAYLOAD=$(jq -n \
155+
--arg author "$PERSON_URN" \
156+
--arg text "$POST_TEXT" \
157+
--arg url "$ARTICLE_URL" \
158+
--arg title "$ARTICLE_TITLE" \
159+
--arg desc "$ARTICLE_DESC" \
160+
'{
161+
"author": $author,
162+
"commentary": $text,
163+
"visibility": "PUBLIC",
164+
"distribution": {
165+
"feedDistribution": "MAIN_FEED",
166+
"targetEntities": [],
167+
"thirdPartyDistributionChannels": []
168+
},
169+
"content": {
170+
"article": {
171+
"source": $url,
172+
"title": $title,
173+
"description": $desc
174+
}
175+
},
176+
"lifecycleState": "PUBLISHED",
177+
"isReshareDisabledByAuthor": false
178+
}')
179+
fi
102180
else
103181
# Text-only post
104182
POST_PAYLOAD=$(jq -n \

0 commit comments

Comments
 (0)