Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to authenticate using Cookies #463

Open
harrynou123 opened this issue Nov 18, 2024 · 4 comments
Open

How to authenticate using Cookies #463

harrynou123 opened this issue Nov 18, 2024 · 4 comments

Comments

@harrynou123
Copy link

Hello,

I would like to authenticate with the linkedin-api library using cookies, but I’m unsure of the correct process. Could you please provide detailed guidance on how to set up authentication using cookies?

Specifically, I’d like to understand:

  1. How to structure the cookie file (if any specific format is required)?
  2. How to pass the cookie file to the linkedin-api library during initialization?
  3. Whether additional parameters (e.g., username, password) are needed alongside cookies?

I’m looking for clear steps or an example that demonstrates this authentication method.

Thank you in advance for your assistance!

@ccmtozzi
Copy link

ccmtozzi commented Nov 18, 2024

@harrynou123, this is how I'm doing on my projects, I hope that helps!

from requests.cookies import RequestsCookieJar, create_cookie
from linkedin_api.cookie_repository import CookieRepository

# Use cookies from the browser to create a cookie jar
cookies = json.load(open('./cookies/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)

# Save cookies
new_repo = CookieRepository()
new_repo.save(cookie_jar, username)

# Authenticate using Linkedin API
api = Linkedin(username, password, cookies=cookie_jar)

@y-aoub
Copy link

y-aoub commented Nov 22, 2024

Hi,

Is there an automated way to retrieve the cookies directly from LinkedIn without manually exporting them via a browser extension ?

@thomas-nunes
Copy link

thomas-nunes commented Dec 10, 2024

Hi,

Is there an automated way to retrieve the cookies directly from LinkedIn without manually exporting them via a browser extension ?

I didn't find a way for the moment but you would need to use your credientials to get it, at this point you can use the credentials to use Linkedin api

Btw if you find a way to scrap it i'm interested

@moxplod
Copy link

moxplod commented Dec 15, 2024

@harrynou123, this is how I'm doing on my projects, I hope that helps!

from requests.cookies import RequestsCookieJar, create_cookie
from linkedin_api.cookie_repository import CookieRepository

# Use cookies from the browser to create a cookie jar
cookies = json.load(open('./cookies/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)

# Save cookies
new_repo = CookieRepository()
new_repo.save(cookie_jar, username)

# Authenticate using Linkedin API
api = Linkedin(username, password, cookies=cookie_jar)

Looks like you still have to pass username password here?
Thought of you just did cookies, you would not need the user and password

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants