amosbastian / fpl

An asynchronous Python wrapper for the Fantasy Premier League API.
https://fpl.readthedocs.io
MIT License
308 stars 98 forks source link

Login fails #127

Open spiros26 opened 1 year ago

spiros26 commented 1 year ago

I can't get the login function to work. Maybe fpl has changed something and the headers don't work?

ivarbratberg commented 1 year ago

Same here, it says state = response.url.query["state"] keyerror

ivarbratberg commented 1 year ago

/site-packages/fpl/fpl.py) in login(self, email, password) 589 ssl=ssl_context, 590 headers={"User-Agent": "Dalvik/2.1.0 (Linux; U; Android 5.1; PRO 5 Build/LMY47D)"}) as response: --> 591 state = response.url.query["state"] 592 if state == "fail":

Tonio101 commented 1 year ago

Same, seems like its an auth issue. API requires a Cookie now.

Tonio101 commented 1 year ago

@spiros26 @ivarbratberg, Its an auth issue, I am still not sure how to retrieve the pl_profile and datadome, will look through their API to see if theres a way. As a workaround you have to manually edit the code, provide the Cookie and recompile. It seems like its a one-time thing, I have not had to refresh or get a new version of pl_profile and datadome.

  1. Obtain the pl_profile and datadome from the Session cookie, once you've logged into your PL account. You can access the cookies if you log on in chrome and then right click and select inspect > Application > Cookies

Use the code snippet in fpl.py file.

        custom_cookie = {
            'pl_profile': "",
            'datadome': ""
        }

        headers = {
            "User-Agent": "Dalvik/2.1.0 (Linux; U; Android 5.1; PRO 5 Build/LMY47D)"
        }

        async with self.session.post(login_url, data=payload,
                                     ssl=ssl_context,
                                     cookies=custom_cookie,
                                     headers=headers) as response:
            if 'state' not in response.url.query:
                raise ValueError(f"Unsuccessful login: {response.status_code}")

            state = response.url.query["state"]
            if state == "fail":
                reason = response.url.query["reason"]
                raise ValueError(f"Login not successful, reason: {reason}")
amosbastian commented 1 year ago

I released a new version a few days ago that included #126

This PR includes the ability to grab a cookie from the FPL_COOKIE environment variables and add it to the headers if specified

So maybe you can try that out?

kernalkue commented 1 year ago

I'm having a very hard time figuring out how to put a cookie into an environment variable. Do I format it as json?

ivarbratberg commented 1 year ago

I tried to set the variables like this in fply.py, but I got a 403. I ensured code change was active and latest package.

cookie = '{ "pl_profile": "eyJzIjogIld6SXNOa....=", "datadome": "1UF....."}'

    if not cookie:
        raise Exception('No coolies provided')
        cookie = os.getenv('FPL_COOKIE')
    headers = {
        "User-Agent": "Dalvik/2.1.0 (Linux; U; Android 5.1; PRO 5

Build/LMY47D)",

On Fri, Aug 18, 2023 at 9:58 AM kernalkue @.***> wrote:

I'm having a very hard time figuring out how to put a cookie into an environment variable. Do I format it as json?

— Reply to this email directly, view it on GitHub https://github.com/amosbastian/fpl/issues/127#issuecomment-1683519827, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADY74A2U4275J4D364KJQVDXV4OC3ANCNFSM6AAAAAA25PDQPY . You are receiving this because you were mentioned.Message ID: @.***>

ThatPalAl commented 1 year ago

Any hope for a solution here? I am unable to login using the pl_profile and data dome, seems like it's still unresolved, right?

Divinebosst commented 12 months ago

The only solution is to use a selenium bot to login, but it's not effective because the fpl webapp uses datadome for bot detection.

Tonio101 commented 12 months ago

Any hope for a solution here? I am unable to login using the pl_profile and data dome, seems like it's still unresolved, right?

@ThatPalAl Should work, please pass in the correct format for the cookie:

import asyncio
import aiohttp
from fpl import FPL

async def main():
    async with aiohttp.ClientSession() as session:
        fpl = FPL(session)
        await fpl.login(
            email="",
            password="",
            cookie = """datadome=;pl_profile="""
        )
        user = await fpl.get_user()
    print(user)

asyncio.run(main())
ThatPalAl commented 12 months ago

@Tonio101 Not sure how to interpret that, so I need to replace just the 'datadome' with the value from my current session and same for the 'pl_profile'? I've put String values instead of those keywords but it's still the same 403 response. Should it look like this: cookie = """STRING_VALUE_OF_DATADTOME=;STRING_VALUE_OF_PL_PROFILE=""" or is that wrong?

mustardpower commented 12 months ago

You need to add the values of the cookies after the equals character. So would be:

cookie = """datadome=STRING_VALUE_OF_DATADTOME;pl_profile=STRING_VALUE_OF_PL_PROFILE"""