linkedin-developers / linkedin-api-python-client

Official Python client library for LinkedIn APIs
Other
173 stars 35 forks source link

Help in listing comments for a post #47

Open FrancescoSaverioZuppichini opened 1 year ago

FrancescoSaverioZuppichini commented 1 year ago

I was wondering how to get a list of comments for a post, I am also just trying curl

curl -X GET 'https://api.linkedin.com/rest/socialActions/urn:li:share:7105273271557726208/comments' \
-H 'Authorization: Bearer <MY_TOKEN>' \
-H 'LinkedIn-Version: 202304' 

and getting

{"status":403,"serviceErrorCode":100,"code":"ACCESS_DENIED","message":"Not enough permissions to access: socialActionsV2.GET_ALL.20230401"}%      

which is weird since I can create a post with that token with the old apis (not the rest ones)

Thanks

FrancescoSaverioZuppichini commented 1 year ago

Also trying with the apis

response = restli_client.get(
    resource_path="/socialActions/urn:li:share:7105273271557726208/comments",
    access_token=access_token,
    version_string=API_VERSION,
)

print(response.entity)

error:

{'status': 400, 'code': 'ILLEGAL_ARGUMENT', 'message': 'Resource method could not be resolved from request parameters.'}
yeyuchen198 commented 1 year ago

Hello, I have the same problem. I want to get the post list data and dynamic review list data of a company. Can this be achieved through this official LinkedIn API?

Rykarix commented 1 year ago

Please could you provide more examples for use cases using this library? It would also be helpful if the readme for this library had a more comprehensive guide, specifically on how to refer to the documentation to build the requests properly?

For others trying to use this library, if something isn't working then you can use the requests library to build your own function. Here's an example that adheres to the query tunneling migration guide:

import requests
import json
from datetime import datetime
from typing import Optional
import os
from dotenv import load_dotenv

load_dotenv()
ACCESS_TOKEN = os.getenv('ACCESS_TOKEN')

def timestamp_datetime_to_ms(timestamp_datetime):
    return int(timestamp_datetime.timestamp() * 1000)

def stringdate_to_ms(stringdate):
    # string format in the form of 'YYYYMM', to keep in format with 'LinkedIn-Version'
    return timestamp_datetime_to_ms(datetime.strptime(stringdate, "%Y%m"))

# Code using POST method
def get_organizationPageStatistics(
    linkedin_version: str,
    organization_urn: str,
    start: Optional[str] = None,
    end: Optional[str] = None,
    granularity: Optional[str] = None,
) -> requests.Response:
    API_URL = "https://api.linkedin.com/rest/organizationPageStatistics?q=organization&organization=urn:li:organization:2414183&timeIntervals.timeGranularityType=DAY&timeIntervals.timeRange.start=1551398400000&timeIntervals.timeRange.end=1552003200000"
    base_url = API_URL.split("?")[0]

    required_params = {
        "q": "organization",
        "organization": organization_urn,
    }

    if (start and end and granularity) is not None:
        if granularity not in ["DAY", "MONTH"]:
            raise ValueError(
                "Invalid granularity type. Must be either 'DAY' or 'MONTH'"
            )

        additional_params = {
            "timeIntervals.timeGranularityType": granularity,
            "timeIntervals.timeRange.start": stringdate_to_ms(start),
            "timeIntervals.timeRange.end": stringdate_to_ms(end),
        }
    elif (start is None and end is not None) or (end is None and start is not None):
        raise ValueError("Must provide both start and end dates")

    else:
        additional_params = {}

    headers = {
        "X-HTTP-Method-Override": "GET",
        "Content-Type": "application/x-www-form-urlencoded",
        "Authorization": f"Bearer {ACCESS_TOKEN}",
        "LinkedIn-Version": linkedin_version,
    }

    combined_params = required_params | additional_params
    response = requests.post(base_url, headers=headers, params=combined_params)

    return response

LINKEDIN_VERSION = "202308"
URN = "urn:li:organization:2414183"

# Usage example
data_post = get_organizationPageStatistics(
    LINKEDIN_VERSION, URN, start="201903", end="201904", granularity="MONTH"
)
print(json.dumps(data_post, indent=2))

Hopefully it helps others who are learning (like me xD)