I am trying to access yahoo mailbox, I have created a yahoo application for the same purpose. After allowing permission to access mailbox, I have access_token and refresh_token for oauth.
I am trying to authenticate using imaplib and I always get authentication failed error.
This is the piece of code I wrote, while I can refresh the auth_tokens, I havent been able to access the mailbox so far
import base64
from oauthlib.oauth2 import BackendApplicationClient
from requests.auth import HTTPBasicAuth
from oauth2.clients.imap import imaplib
USERNAME = 'XXX'
# imap settings
IMAP_SERVER = 'imap.mail.yahoo.com'
IMAP_PORT = 993
# application secrets
client_id = 'XXX'
client_secret = 'XXX'
authorization_base_url = 'https://api.login.yahoo.com/oauth2/request_auth'
token_url = 'https://api.login.yahoo.com/oauth2/get_token'
refresh_token = 'refresh token...'
client = BackendApplicationClient(client_id=client_id)
oauth = OAuth2Session(client=client)
token = oauth.refresh_token(token_url, refresh_token=refresh_token, auth=HTTPBasicAuth(client_id, client_secret))
access_token = token['access_token']
mail = imaplib.IMAP4_SSL(IMAP_SERVER, IMAP_PORT)
auth_string = base64.b64encode(f'\x00{USERNAME}\x00{access_token}'.encode()).decode()
mail.authenticate('XOAUTH2', lambda x: auth_string)
# I get imaplib.IMAP4.error: AUTHENTICATE command error: BAD [b'[AUTHENTICATIONFAILED] AUTHENTICATE Invalid credentials']
mail.select('inbox')
Right after, mail.authenticate('XOAUTH2', lambda x: auth_string)
I get error imaplib.IMAP4.error: AUTHENTICATE command error: BAD [b'[AUTHENTICATIONFAILED] AUTHENTICATE Invalid credentials']