1

I have a bot that checks gmail for new messages and sends me notifications with their text to telegram. My app is hosted on google cloud and it is running in test mode. According to this answer, OAuth token will expire after 1 week. I tried all suggestions mentioned in that answer, but none helped. I just want to run my app on a server.

I tried to create a new token, but when I use them, I get this error:

oauthlib.oauth2.rfc6749.errors.MismatchingStateError: (mismatching_state) CSRF Warning! State not equal in request and response.

My request code:

creds = None
if os.path.exists('token.json'):
    creds = Credentials.from_authorized_user_file('token.json', self.SCOPES)

if not creds or not creds.valid:
    if creds and creds.expired and creds.refresh_token:
        creds.refresh(Request())
    else:
        flow = InstalledAppFlow.from_client_secrets_file(
            'credentials.json', self.SCOPES)
        creds = flow.run_local_server(port=0)

    with open('token.json', 'w') as token:
        token.write(creds.to_json())

service = build('gmail', 'v1', credentials=creds)
try:
    # Do my stuff
    text = self.__get_new_msgs_text(service, 'me')
    return text
except NotFindException:
    raise NotFindException()
Neuron
  • 5,141
  • 5
  • 38
  • 59

1 Answers1

2

If your application is still in the testing phase then the refresh token will expire after 7 days you need to set your application to production.

enter image description here

App in prodcution

Can be found on the consent screen in google cloud console for your project.

enter image description here

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • I did my app status to "in production", but with same oauth credentials I have same error. If I generate new oauth credentials I have 400: redirect_uri_mismatch, I add redirect_uri from context menu below to my credentials and then go to this uri, I have error message in my console: oauthlib.oauth2.rfc6749.errors.MismatchingStateError: (mismatching_state) CSRF Warning! State not equal in request and response. May be I am doing smth wrong? – Иван Светушков Aug 26 '21 at 09:04
  • redirect_uri_mismatch is a completely different error message and has nothing to do with your refresh token being expired. This video will show you how to fix that https://www.youtube.com/watch?v=V0-4LnHwFho – Linda Lawton - DaImTo Aug 26 '21 at 10:57
  • I have redirect_uri_mismatch error when trying to fix "refresh token being expired" error, and error in your video is about java script origin redirect, but I have error according to authorize redicret uris. I try to enter this uri into my OAuth 2.0 Client IDs, but it isn't help me. My message in context menu below: redirect_uri: http://localhost:53466/. I copy this uri to console but it doesn't help me. – Иван Светушков Aug 26 '21 at 11:47
  • Oh im sorry i grabed the wrong link. this is the one for server sided redirect uri https://www.youtube.com/watch?v=QHz1Rs6lZHQ. I would guess it has something to do with the fact that You cant set your project to production if you have a localhost redirect uri it needs to go to your domain. – Linda Lawton - DaImTo Aug 26 '21 at 11:56
  • I do exactly same, but it is'n work even in test mode, I think i need to test this on another gmail. Anyway thanks for your help! – Иван Светушков Aug 26 '21 at 12:23
  • If you cant get it working check my profile here hit me up on twitter. – Linda Lawton - DaImTo Aug 26 '21 at 12:25