Post created using create image share API with /ugcPosts endpoint is not visible on linkedin on my timeline

不羁岁月 提交于 2020-07-06 20:22:05

问题


I have implemented all three steps for sharing an image post on linkedin using create image share api described below. But created posts are not visible on my linkedin feed/posts or recent activities.

https://docs.microsoft.com/en-us/linkedin/consumer/integrations/self-serve/share-on-linkedin#create-an-image-share

First time I created the post is visible using API and it returned {"id":"urn:li:share:6521244543193575424"} Posted Visible post url: https://www.linkedin.com/feed/update/urn:li:share:6520269375554060288/

Second time onwards created with same code below returns similar response with created post id and with 201 created status code but post is not visible on linkedin.

For example this post https://www.linkedin.com/feed/update/urn:li:share:6521027773560119296

I checked my linkedin profile but couldn't find my posts any where.

Code for Final function for posting I am using(3rd step for creating image share using the documentation link above) :

 def share_on_linkedin(self, asset, title, description):
    # asset = "urn:li:digitalmediaAsset:C5122AQEAatG9rZ7MhQ"

    headers = {
    'Content-Type':'application/json',
    'X-Restli-Protocol-Version': '2.0.0',
    'Authorization': 'Bearer '+Constant.ACCESS_TOKEN
    }
    payload = {
        "author": "urn:li:person:leRbOTCFKK",
        "lifecycleState": "PUBLISHED",
        "specificContent": {
            "com.linkedin.ugc.ShareContent": {
                "shareCommentary": {
                    "text": description
                },
                "shareMediaCategory": "IMAGE",
                "media": [
                    {
                        "status": "READY",
                        "description": {
                            "text": "Center stage!"
                        },
                        "media": asset,
                        "title": {
                            "text": title
                        }
                    }
                ]
            }
        },
        "visibility": {
            "com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC"
        }
    }

    try:
        data = json.dumps(payload, sort_keys=True, indent=4)
        url = "https://api.linkedin.com/v2/ugcPosts"
        response = requests.post(url , headers=headers, data=data )
        print response.text

        result = json.loads(response.text)  
        if response.status_code==200 or response.status_code==201:
            print response.text
            print response.headers              
            posted_url =  "https://www.linkedin.com/feed/update/"+result['id']
                            print posted_url 
            return True, posted_url
        return False, None

    except Exception as e:
        print e

Output:

{"id":"urn:li:share:6521245116978552832"}
{'Content-Length': '41', 'X-RestLi-Protocol-Version': '2.0.0', 'X-Li-Pop': 'prod-tmu1', 'X-LI-ResponseOrigin': 'RGW', 'X-RestLi-Id': 'urn:li:share:6521245116978552832', 'X-LI-UUID': 'aKr30Z+1kxWAo4kEzioAAA==', 'X-LI-Route-Key': '"b=SB83:g=115:u=3:i=1554785994:t=1554872227:s=AQH26er48VUD_YiXQIgAqujebI53eswQ"', 'X-Li-Fabric': 'prod-lsg1', 'Connection': 'keep-alive', 'Location': '/ugcPosts/urn%3Ali%3Ashare%3A6521245116978552832', 'Set-Cookie': 'lidc="b=SB83:g=115:u=3:i=1554785994:t=1554872227:s=AQH26er48VUD_YiXQIgAqujebI53eswQ"', 'Date': 'Tue, 09 Apr 2019 04:59:55 GMT', 'X-LI-Proto': 'http/1.1', 'Content-Type': 'application/json'}

Posted url constructed from above successful response is: https://www.linkedin.com/feed/update/urn:li:share:6521245116978552832

But this post is not visible.

Can anybody help me with this.

Thanks!


回答1:


You may need to verify your media asset is available and ready to be posted before submitting your ugcPosts request.

After you've uploaded your image binary, submit a request to the /assets API and check the status is AVAILABLE

GET https://api.linkedin.com/v2/assets/C5122AQEAatG9rZ7MhQ




回答2:


Use python to share text and image on LinkedIn API

Python Linkedin, How to share content on user's wall

i#mage upload linkedin api

code start from here

 def imageShare(request):

if request.FILES.getlist('media') and request.POST['postText']:
    # step 1
    try:
        new_asset.clear()

    except:
        pass
    access_token = token[0]
    h = {
        'Authorization': 'Bearer ' + str(access_token),

        'Content-Type': 'multipart/form-data',
        'X-Restli-Protocol-Version': '2.0.0',

    }

    data = {
        "registerUploadRequest": {
            "recipes": [
                "urn:li:digitalmediaRecipe:feedshare-image"
            ],
            "owner": "urn:li:person:mUIlTwceuD",
            "serviceRelationships": [
                {
                    "relationshipType": "OWNER",
                    "identifier": "urn:li:userGeneratedContent"
                }
            ]
        }
    }

    z = requests.post('https://api.linkedin.com/v2/assets?action=registerUpload', headers=h, data=json.dumps(data))
    print(z.text)
    aa = z.json()['value']['uploadMechanism']['com.linkedin.digitalmedia.uploading.MediaUploadHttpRequest'][
        'uploadUrl']
    shareurl.append(aa)
    # print(shareurl)
    bb = z.json()['value']['asset']
    asset = bb.replace('urn:li:digitalmediaAsset:', '')
    new_asset.append(asset)

    # step 2 upload binary image

    upload_url = str(aa)
    postText = request.POST['postText']
    image = request.FILES.getlist('media')
    print('blank image', image)

    for i in image:
        headers = {
            'Authorization': 'Bearer ' + str(access_token),
            'X-Restli-Protocol-Version': '2.0.0',
            'Content-Type': 'image/jpeg,image/png,image/gif',

        }
        response = requests.post(upload_url, headers=headers, data=i.read())

    he = {

        'Authorization': 'Bearer ' + str(access_token),
        'X-Restli-Protocol-Version': '2.0.0',
        'Content-Type': 'multipart/form-data'

    }

    ab = requests.get('https://api.linkedin.com/v2/assets/' + str(new_asset[0]), headers=he)
    print('submittttttttt', ab.content)
    #     return redirect('http://127.0.0.1:8000/social_app/index/')
    #
    #
    #
    # def index(request):
    headers = {
        'content-type': 'application/json',
        'X-Restli-Protocol-Version': '2.0.0',
        'Content-Type': 'multipart/form-data',
        'Authorization': 'Bearer ' + str(access_token),
    }
    data = {
        "author": "urn:li:person:mUIlTwceuD",
        "lifecycleState": "PUBLISHED",
        "specificContent": {
            "com.linkedin.ugc.ShareContent": {
                "shareCommentary": {
                    "text": postText
                },
                "shareMediaCategory": "IMAGE",
                "media": [
                    {
                        "status": "READY",
                        "description": {
                            "text": "Center stage!"
                        },
                        "media": "urn:li:digitalmediaAsset:" + str(new_asset[0]),
                        "title": {
                            "text": "LinkedIn Talent Connect 2018"
                        }
                    }
                ]
            }
        },
        "visibility": {
            "com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC"
        }
    }

    z = requests.post('https://api.linkedin.com/v2/ugcPosts', headers=headers, data=json.dumps(data))
    print(z.text)
    return HttpResponse('Image shared')
else:
if request.method == 'POST':
    post = request.POST['postText']
    print("post = ", post)
    sub = {
        "author": "urn:li:person:mUIlTwceuD",
        "lifecycleState": "PUBLISHED",
        "specificContent": {
            "com.linkedin.ugc.ShareContent": {
                "shareCommentary": {
                    "text": post,
                },
                "shareMediaCategory": "NONE"
            }
        },

        "visibility": {
            "com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC"
        }
    }

    h = {
        'Authorization': 'Bearer ' + str(access_token),
        'Content-Type': 'text/plain',
        'content-type': 'application/json',
        'X-Restli-Protocol-Version': '2.0.0',

    }
    a = requests.post('https://api.linkedin.com/v2/ugcPosts', headers=h, data=json.dumps(sub))
    print(a, a.content, a.text, a.json())
return render(request, 'post.html', {})

return HttpResponse('text sent')


来源:https://stackoverflow.com/questions/55585820/post-created-using-create-image-share-api-with-ugcposts-endpoint-is-not-visible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!