You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

使用LinkedIn API发布企业主页投票遇400错误:如何配置rw_organization权限?

LinkedIn API发布企业主页投票遇400错误:如何配置rw_organization权限?

我正在使用LinkedIn API在我的LinkedIn企业主页上发布投票,但一直遇到以下错误:

Failed to post poll. Response: 400 {"message":"Organization permissions must be used when using organization as author","status":400}

我认为问题和缺失权限有关。我知道可能需要rw_organization权限,但不知道如何启用它。在设置应用程序时,我已经授予了所有可用权限,但找不到添加或启用rw_organization的具体选项。

代码如下:

import mysql.connector
import requests
import random

def shorten_option(option, max_length=25):
    ...

def extract_hashtag(question_group):
  ...
def fetch_poll_data():
  ...

def post_linkedin_poll(question, options, extracted_hashtag):
    access_token = "..."
    organization_id = "..."  
    api_url = "https://api.linkedin.com/rest/posts"
    additional_hashtags = "#Practice"
    post_text = f"{question}\n\n{extracted_hashtag} {additional_hashtags}"
    poll_data = {
        "question": question,
        "options": [{"text": option} for option in options],
        "settings": {"duration": "THREE_DAYS"}  
    }
    payload = {
        "author": f"urn:li:organization:{organization_id}",  
        "commentary": post_text,
        "visibility": {"com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC"},
        "distribution": {
            "feedDistribution": "MAIN_FEED",
            "targetEntities": [],
            "thirdPartyDistributionChannels": []
        },
        "lifecycleState": "PUBLISHED",
        "content": {
            "poll": poll_data
        },
        "isReshareDisabledByAuthor": False
    }

    # Set the headers
    headers = {
        "Authorization": f"Bearer {access_token}",
        "Content-Type": "application/json",
        "LinkedIn-Version": "202408" 
    }

    try:
        response = requests.post(api_url, json=payload, headers=headers)
        if response.status_code == 201:
            print("Poll posted successfully on LinkedIn!")
        elif response.status_code == 500:
            print(f"LinkedIn Server Error: {response.text}.")
        else:
            print(f"Failed to post poll. Response: {response.status_code} {response.text}")

    except Exception as e:
        print(f"An error occurred while posting the poll: {e}")

def main():
    # Fetch poll data from the database
    question, options, extracted_hashtag = fetch_poll_data()

    if question and options and extracted_hashtag:
        # Post the poll to LinkedIn
        post_linkedin_poll(question, options, extracted_hashtag)
    else:
        print("Failed to fetch poll data. No poll posted.")
if __name__ == "__main__":
    main()

有没有人知道如何解决这个问题,或者如何配置应用程序以使用发布所需的权限?

非常感谢任何帮助!

备注:内容来源于stack exchange,提问作者user2842069

火山引擎 最新活动