2

This code is taken almost verbatim from mastodon.py's README.md

it always returns

Traceback (most recent call last): File "C:\Users\matth\GitHub\feed_thing\again.py", line 32, in mastodon.log_in( File "C:\Users\matth.virtualenvs\feed_thing-LXMa84iN\lib\site-packages\mastodon\Mastodon.py", line 568, in log_in raise MastodonIllegalArgumentError('Invalid user name, password, or redirect_uris: %s' % e) mastodon.Mastodon.MastodonIllegalArgumentError: Invalid user name, password, or redirect_uris: ('Mastodon API returned error', 400, 'Bad Request', 'invalid_grant')

UPDATE: I get this failure from mastodon.social, but not mstdn.social and not ohai.social.

It turns out to be because of 2 factor auth and the sample code below is not a OAUTH2 dance.

How would I replace the code below with an OAUTH2 dance?

import os
from mastodon import Mastodon

USERNAME_AS_EMAIL = os.environ["USERNAME_AS_EMAIL"]
PASSWORD = os.environ["PASSWORD"]

APP_NAME = "APP_NAME"
Mastodon.create_app(
     APP_NAME,
     api_base_url = 'https://mastodon.social',
     to_file = 'pytooter_clientcred.secret'
)
mastodon = Mastodon(
    client_id = 'pytooter_clientcred.secret',
    api_base_url = 'https://mastodon.social'
)
mastodon.log_in(
    USERNAME_AS_EMAIL,
    PASSWORD,
    to_file = 'pytooter_usercred.secret'
)
mastodon = Mastodon(
    client_id = 'pytooter_clientcred.secret',
    access_token = 'pytooter_usercred.secret',
    api_base_url = 'https://mastodon.social'
)
MatthewMartin
  • 32,326
  • 33
  • 105
  • 164

1 Answers1

2

I found part of the solution. If 2 factor auth is enabled on your account, mastodon.py (as of today) can't handle login. If you disable 2 factor auth, then you can login as expected.

Another hint for people who find this, sometimes you need to delete the *.secret files for mastodon.py to work (i.e. if you've changed server names but didn't change the .secret files)

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164