tomquirk / linkedin-api

👨‍💼Linkedin API for Python
MIT License
1.71k stars 401 forks source link

A solution to constant challenge step #331

Open gokaybiz opened 10 months ago

gokaybiz commented 10 months ago
from requests.cookies import RequestsCookieJar, create_cookie
from linkedin_api.cookie_repository import CookieRepository
import json

cookies = json.load(open('./cookies.json')) # Path of exported cookie via https://www.editthiscookie.com/

cookie_jar = RequestsCookieJar()

for cookie_data in cookies:
    cookie = create_cookie(
        domain=cookie_data["domain"],
        name=cookie_data["name"],
        value=cookie_data["value"],
        path=cookie_data["path"],
        secure=cookie_data["secure"],
        expires=cookie_data.get("expirationDate", None),
        rest={
            "HttpOnly": cookie_data.get("httpOnly", False),
            "SameSite": cookie_data.get("sameSite", "unspecified"),
            "HostOnly": cookie_data.get("hostOnly", False),
        }
    )
    cookie_jar.set_cookie(cookie)

new_repo = CookieRepository()
new_repo.save(cookie_jar, 'email_or_username')

Log in to your linkedin account in the browser and export your cookies with EditThisCookie, then save it as cookie.json. That's all.

russell310 commented 9 months ago

@gokaybiz It works fine. But problem is cookie expire time. How to automate the process?

gokaybiz commented 9 months ago

@gokaybiz It works fine. But problem is cookie expire time. How to automate the process?

In line 16, it should be adding the expiration time. What exactly is the problem you are having?

Edit: Ah, now I see what you mean. You can use Selenium to perform web login and dump cookies. But you will probably encounter captcha issue again. So sending an email to yourself when it throws an error and checking the status manually will be the least troublesome method.

SIF-FCHIARI commented 9 months ago

I've done the automated login process, it used to work until recently when Linkedin changed their policies, i'd recommend just creating a notification system and logging error cleanly so as soon as the cookies expire you can change them as they last for a few months

russell310 commented 9 months ago

Already use this manual fixing. But need automation. Can you describe process for selenium in server? As headless how can we put code? Also sometime puzzle came instead of code

sim1029 commented 3 months ago

How long do the cookies typically take to expire? @gokaybiz

gokaybiz commented 2 months ago

@sim1029 Well, it depends on your actions. If you are not spamming it takes ~2 months