在我的时间轴上的linkedin上看不到使用带有/ ugcPosts端点的create image share API创建的帖子

时间:2019-04-09 05:42:37

标签: python api linkedin linkedin-api

我已经使用下面介绍的create image share api实现了在linkedin上共享图像帖子的所有三个步骤。但是创建的帖子在我的Linkedin提要/帖子或近期活动中不可见。

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

我第一次创建该帖子时,可以使用API​​查看该帖子,并且返回了{"id":"urn:li:share:6521244543193575424"} 发表可见的发布网址: https://www.linkedin.com/feed/update/urn:li:share:6520269375554060288/

第二次以后,使用下面的相同代码创建的代码将返回类似的响应,其中包含已创建的帖子ID和201已创建的状态代码,但帖子在linkedin上不可见。

例如这篇文章 https://www.linkedin.com/feed/update/urn:li:share:6521027773560119296

我检查了我的linkedin个人资料,但在任何地方都找不到我的帖子。

我正在使用的发布最终功能代码(使用上面的文档链接创建图像共享的第三步):

 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

输出:

{"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'}

根据上述成功响应构造的发布网址为: https://www.linkedin.com/feed/update/urn:li:share:6521245116978552832

但是此帖子不可见。

有人可以帮我吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

在提交ugcPosts请求之前,您可能需要验证媒体资产是否可用并准备好发布。

上传图像二进制文件后,向/ assets API提交请求,并检查状态为 Available

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

答案 1 :(得分:0)

使用python在LinkedIn API上共享文本和图像

Python Linkedin,如何在用户墙上共享内容

i#mage上传linkedin API

代码从这里开始

 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')