tomquirk / linkedin-api

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

Linkedin function not working(LinkedinSessionExpired) #357

Closed rayrvl closed 3 months ago

rayrvl commented 7 months ago

Hi tomquirk,

Thank you for your great package! I had already used your app before in 2020 without problems, but now that I have tried to replicate it, I ran into a problem.

After I load the package, I can't even use the first step by Linkedin function. For example, this is not working api = Linkedin('lab.point.test001@gmail.com', '*************'). And the error is 'LinkedinSessionExpired'.

I would appreciate if you could help me with this problem since I would love to take advantage of this library again, I am running the program in python==3.9.0. I hope your answer Thanks

Linkedin-api erro LinkedinSessionExpired

Saharsha-N commented 7 months ago

This commonly means the cookies used in the request are expired. Try calling Linkedin( ... cookies=None) or deleting the email.com.jr file. This will force the API to request new cookies. Beware, expired cookies can mean that Linkedin has restricted your account.

EDIT: check linkedin_api/settings.py for cookie file location

rchvalbo commented 7 months ago

Either your cookies are bad/expired, or you're passing the wrong cookies to the LinkedIn Client. I recommend actually managing and passing the cookies to the LinkedIn Client yourself. That's been the most stable approach for me. If you want to grab cookies using automation I recommend using selenium to build out a small login flow to get the cookies (That's what I did).

Here's how I load/store the cookies, and then grab the cookie_jar. My problem in the past is that I was trying to grab the cookies via RequestsCookieJar.get() instead of just passing the cookie jar itself to Linkedin Client.

My LoadCookies and GetCookies functions:

    def LoadCookies(self):
        try:
            cookies = json.load(open(self.getCookiePath()))
        except Exception as e:
            print("Cookies not found!")
            cookies = None
            return
        self.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),
                }
            )
            self.cookie_jar.set_cookie(cookie)

        self.cookieRepo.save(self.cookie_jar, self.username)

    def GetCookies(self):
        return self.cookie_jar

And here's me initializing the Linkedin Client:

self._cookieManager.LoadCookies()

        self.api = Linkedin(
            username=username, 
            password=None,
            refresh_cookies=True,
            cookies=self._cookieManager.GetCookies()
            )

As you can see, I'm using GetCookies() from my class and all that does is return the cookie_jar.