0

I tried to search it up on google and stack overflow, but nothing seems to be working. I want to remotely login to my google account using python.

here is my code:

import urllib.request

def unread_messages(user, passwd):
    auth_handler = urllib.request.HTTPBasicAuthHandler()
    auth_handler.add_password(
        realm='New mail feed',
        uri='https://mail.google.com',
        user='%s@gmail.com' % user,
        passwd=passwd
    )
    opener = urllib.request.build_opener(auth_handler)
    urllib.request.install_opener(opener)
    feed = urllib.request.urlopen('https://mail.google.com/mail/feed/atom')
    return feed.read()


print(unread_messages('test@gmail.com', 'testpass'))

output:

Traceback (most recent call last):
  File "G:\Storage\USER1\Desktop\L\googlelogin\main.py", line 17, in <module>
    print(unread_messages('kunalkisku060@gmail.com', 'kunal1234'))
  File "G:\Storage\USER1\Desktop\L\googlelogin\main.py", line 13, in unread_messages
    feed = urllib.request.urlopen('https://mail.google.com/mail/feed/atom')
  File "C:\Python39\lib\urllib\request.py", line 214, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python39\lib\urllib\request.py", line 523, in open
    response = meth(req, response)
  File "C:\Python39\lib\urllib\request.py", line 632, in http_response
    response = self.parent.error(
  File "C:\Python39\lib\urllib\request.py", line 561, in error
    return self._call_chain(*args)
  File "C:\Python39\lib\urllib\request.py", line 494, in _call_chain
    result = func(*args)
  File "C:\Python39\lib\urllib\request.py", line 641, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 401: Unauthorized

1 Answers1

0

I think someone has already answered your question here: Logging in to google using python?

Dharman
  • 30,962
  • 25
  • 85
  • 135
sherxyar
  • 83
  • 6