coddingtonbear / python-myfitnesspal

Access your meal tracking data stored in MyFitnessPal programatically
MIT License
794 stars 136 forks source link

New Error: Unable to log in #88

Closed nathanmerrill closed 4 years ago

nathanmerrill commented 4 years ago

It appears that something has changed on their server. I'm now getting a "Unable to log in" error. My password is 100% correct. I've done some logging in client.py, and my username and password are definitely getting through. I've also created another account, and tried those credentials, and I'm still getting the same error.

I'm not really sure how the server is detecting your script. I'm able to use Postman to log in. I've attempted changing the request to a multipart/form-data, changing the User-Agent, or removing all cookies. (I've verified via Postman that cookies aren't required).

velaar commented 4 years ago

Second that. Not sure if server is detecting it or they changed the authentication process, but something has definitely changed.

nathanmerrill commented 4 years ago

Ok, doing more investigation, something is definitely weird. The following code works:

session = requests.Session()

get_result = session.get('https://www.myfitnesspal.com/account/login', headers={}).content.decode('utf-8')
document = lxml.html.document_fromstring(get_result)
authenticity_token = document.xpath("(//input[@name='authenticity_token']/@value)[1]")[0]

result = session.post(
    'https://www.myfitnesspal.com/account/login',
    data = {
        'utf8': '✓',
        'authenticity_token': authenticity_token,
        'username': username,
        'password': password,
    }
)

content = result.content.decode('utf8')
if 'Incorrect username or password' in content:
    raise ValueError("Incorrect username or password.")

with open("content.html", "w", encoding="utf-8") as f:
    f.write(content)

This looks nearly identical, so I'm struggling to figure out what is different between my code and the code here.

nathanmerrill commented 4 years ago

I'm no longer able to reproduce the issue. I don't know if I somehow fixed my code, accidentally fixed my copy of yours, or if they somehow changed the server back, but logging in works fine now. I'll leave this open, as it appears that velaar is having issues, but unless you hear otherwise, things appear to be working.

velaar commented 4 years ago

@nathanmerrill still having issues. This is where I end up

raise ValueError("Incorrect username or password.") no matter that the password is 100% correct. Tried with both email and username as username

coddingtonbear commented 4 years ago

I'm not able to reproduce this, you folks -- I can gather nutrition information just fine using the latest master & my username/password:

 myfitnesspal day me@adamcoddington.net 2020-01-03                                                                                                                      [11:14:31]
2020-01-03
Breakfast
* Nutricost - Whey Protein Isolate, 1 scoop - 33 grams
  {'calories': 140, 'carbohydrates': 2, 'fat': 1, 'protein': 30}
* Silk - New Silky Smooth - Unsweetened Almondmilk, 1 cup
  {'calories': 30, 'carbohydrates': 1, 'fat': 3, 'protein': 1}
* Oil - Coconut, 1 tbs
  {'calories': 120, 'carbohydrates': 0, 'fat': 14, 'protein': 0}
* Cream, fluid, half and half, 1 fluid ounce
  {'calories': 39, 'carbohydrates': 1, 'fat': 3, 'protein': 1}
* Papa G’s Vegan Organics - Organic Taco Tofu (Net Carbs), 100 g
  {'calories': 200, 'carbohydrates': 2, 'fat': 12, 'protein': 17}

Lunch
* Papa G’s Vegan Organics - Organic Taco Tofu (Net Carbs), 1 container (226 g)
  {'calories': 452, 'carbohydrates': 5, 'fat': 27, 'protein': 38}
* Tillamook - Sharp Cheddar, 56 g
  {'calories': 220, 'carbohydrates': 0, 'fat': 18, 'protein': 14}

Dinner
* Keto Paneer Makhanwala (v1), 1 serving(s)
  {'calories': 1040, 'carbohydrates': 18, 'fat': 85, 'protein': 49}

Snacks

Totals
Calories: 2241
Carbohydrates: 29
Fat: 163
Protein: 150
Water: 0.0

Are you folks sure you're using your myfitnesspal password -- not your Facebook password -- for logging in?

nathanmerrill commented 4 years ago

I was definitely using the correct username and password. I'm wondering whether I somehow triggered something on their server to block me. I have no idea.

coddingtonbear commented 4 years ago

Closing this due to inactivity.