import flask
import google.oauth2.credentials
import google_auth_oauthlib.flow
import config
import requests

# This variable specifies the name of a file that contains the OAuth 2.0
# information for this application, including its client_id and client_secret.
CLIENT_SECRETS_FILE = "client_secret.json"
# This OAuth 2.0 access scope allows for full read/write access to the
# authenticated user's account and requires requests to use an SSL connection.
c = config.emailConfiguration()
SCOPES = c["service_scopes"]
API_SERVICE_NAME = c["service_name"]
API_VERSION = c["service_version"]


def authorize():
# Create flow instance to manage the OAuth 2.0 Authorization Grant Flow steps.
    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
        CLIENT_SECRETS_FILE, scopes=SCOPES)

    flow.redirect_uri = flask.url_for('oauth2callback', _external=True)

    authorization_url, state = flow.authorization_url(
        # Enable offline access so that you can refresh an access token without
        # re-prompting the user for permission. Recommended for web server apps.
        access_type='offline',
        # Enable incremental authorization. Recommended as a best practice.
        include_granted_scopes='true')

    # Store the state so the callback can verify the auth server response.
    flask.session['state'] = state

    return flask.redirect(authorization_url)

def oauth2Callback():
    # Specify the state when creating the flow in the callback so that it can
    # verified in the authorization server response.
    state = flask.session['state']

    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
        CLIENT_SECRETS_FILE, scopes=SCOPES, state=state)
    flow.redirect_uri = flask.url_for('oauth2callback', _external=True)

    # Use the authorization server's response to fetch the OAuth 2.0 tokens.
    authorization_response = flask.request.url
    flow.fetch_token(authorization_response=authorization_response)

    # Store credentials in the session.
    # ACTION ITEM: In a production app, you likely want to save these
    #              credentials in a persistent database instead.
    credentials = flow.credentials
    flask.session['credentials'] = credentials_to_dict(credentials)

    return flask.redirect(flask.url_for('index'))

def revoke():
    if 'credentials' not in flask.session:
        return ('You need to <a href="/authorize">authorize</a> before ' +
            'testing the code to revoke credentials.')

    credentials = google.oauth2.credentials.Credentials(
        **flask.session['credentials'])

    revoke = requests.post('https://accounts.google.com/o/oauth2/revoke',
        params={'token': credentials.token},
        headers = {'content-type': 'application/x-www-form-urlencoded'})

    status_code = getattr(revoke, 'status_code')
    if status_code == 200:
        print('Credentials successfully revoked.')
    else:
        print('An error occurred.')

    return

def clear_credentials():
    if 'credentials' in flask.session:
        del flask.session['credentials']
    return '<span>Session cleared </span>'

def credentials_to_dict(credentials):
    return {'token': credentials.token,
          'refresh_token': credentials.refresh_token,
          'token_uri': credentials.token_uri,
          'client_id': credentials.client_id,
          'client_secret': credentials.client_secret,
          'scopes': credentials.scopes}

41640704315ac5131e05a469c01934342dec1dbb
<mask token>


def authorize():
    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
        CLIENT_SECRETS_FILE, scopes=SCOPES)
    flow.redirect_uri = flask.url_for('oauth2callback', _external=True)
    authorization_url, state = flow.authorization_url(access_type='offline',
        include_granted_scopes='true')
    flask.session['state'] = state
    return flask.redirect(authorization_url)


def oauth2Callback():
    state = flask.session['state']
    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
        CLIENT_SECRETS_FILE, scopes=SCOPES, state=state)
    flow.redirect_uri = flask.url_for('oauth2callback', _external=True)
    authorization_response = flask.request.url
    flow.fetch_token(authorization_response=authorization_response)
    credentials = flow.credentials
    flask.session['credentials'] = credentials_to_dict(credentials)
    return flask.redirect(flask.url_for('index'))


<mask token>


def clear_credentials():
    if 'credentials' in flask.session:
        del flask.session['credentials']
    return '<span>Session cleared </span>'


def credentials_to_dict(credentials):
    return {'token': credentials.token, 'refresh_token': credentials.
        refresh_token, 'token_uri': credentials.token_uri, 'client_id':
        credentials.client_id, 'client_secret': credentials.client_secret,
        'scopes': credentials.scopes}

<mask token>


def authorize():
    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
        CLIENT_SECRETS_FILE, scopes=SCOPES)
    flow.redirect_uri = flask.url_for('oauth2callback', _external=True)
    authorization_url, state = flow.authorization_url(access_type='offline',
        include_granted_scopes='true')
    flask.session['state'] = state
    return flask.redirect(authorization_url)


def oauth2Callback():
    state = flask.session['state']
    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
        CLIENT_SECRETS_FILE, scopes=SCOPES, state=state)
    flow.redirect_uri = flask.url_for('oauth2callback', _external=True)
    authorization_response = flask.request.url
    flow.fetch_token(authorization_response=authorization_response)
    credentials = flow.credentials
    flask.session['credentials'] = credentials_to_dict(credentials)
    return flask.redirect(flask.url_for('index'))


def revoke():
    if 'credentials' not in flask.session:
        return ('You need to <a href="/authorize">authorize</a> before ' +
            'testing the code to revoke credentials.')
    credentials = google.oauth2.credentials.Credentials(**flask.session[
        'credentials'])
    revoke = requests.post('https://accounts.google.com/o/oauth2/revoke',
        params={'token': credentials.token}, headers={'content-type':
        'application/x-www-form-urlencoded'})
    status_code = getattr(revoke, 'status_code')
    if status_code == 200:
        print('Credentials successfully revoked.')
    else:
        print('An error occurred.')
    return


def clear_credentials():
    if 'credentials' in flask.session:
        del flask.session['credentials']
    return '<span>Session cleared </span>'


def credentials_to_dict(credentials):
    return {'token': credentials.token, 'refresh_token': credentials.
        refresh_token, 'token_uri': credentials.token_uri, 'client_id':
        credentials.client_id, 'client_secret': credentials.client_secret,
        'scopes': credentials.scopes}

<mask token>
CLIENT_SECRETS_FILE = 'client_secret.json'
c = config.emailConfiguration()
SCOPES = c['service_scopes']
API_SERVICE_NAME = c['service_name']
API_VERSION = c['service_version']


def authorize():
    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
        CLIENT_SECRETS_FILE, scopes=SCOPES)
    flow.redirect_uri = flask.url_for('oauth2callback', _external=True)
    authorization_url, state = flow.authorization_url(access_type='offline',
        include_granted_scopes='true')
    flask.session['state'] = state
    return flask.redirect(authorization_url)


def oauth2Callback():
    state = flask.session['state']
    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
        CLIENT_SECRETS_FILE, scopes=SCOPES, state=state)
    flow.redirect_uri = flask.url_for('oauth2callback', _external=True)
    authorization_response = flask.request.url
    flow.fetch_token(authorization_response=authorization_response)
    credentials = flow.credentials
    flask.session['credentials'] = credentials_to_dict(credentials)
    return flask.redirect(flask.url_for('index'))


def revoke():
    if 'credentials' not in flask.session:
        return ('You need to <a href="/authorize">authorize</a> before ' +
            'testing the code to revoke credentials.')
    credentials = google.oauth2.credentials.Credentials(**flask.session[
        'credentials'])
    revoke = requests.post('https://accounts.google.com/o/oauth2/revoke',
        params={'token': credentials.token}, headers={'content-type':
        'application/x-www-form-urlencoded'})
    status_code = getattr(revoke, 'status_code')
    if status_code == 200:
        print('Credentials successfully revoked.')
    else:
        print('An error occurred.')
    return


def clear_credentials():
    if 'credentials' in flask.session:
        del flask.session['credentials']
    return '<span>Session cleared </span>'


def credentials_to_dict(credentials):
    return {'token': credentials.token, 'refresh_token': credentials.
        refresh_token, 'token_uri': credentials.token_uri, 'client_id':
        credentials.client_id, 'client_secret': credentials.client_secret,
        'scopes': credentials.scopes}

import flask
import google.oauth2.credentials
import google_auth_oauthlib.flow
import config
import requests
CLIENT_SECRETS_FILE = 'client_secret.json'
c = config.emailConfiguration()
SCOPES = c['service_scopes']
API_SERVICE_NAME = c['service_name']
API_VERSION = c['service_version']


def authorize():
    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
        CLIENT_SECRETS_FILE, scopes=SCOPES)
    flow.redirect_uri = flask.url_for('oauth2callback', _external=True)
    authorization_url, state = flow.authorization_url(access_type='offline',
        include_granted_scopes='true')
    flask.session['state'] = state
    return flask.redirect(authorization_url)


def oauth2Callback():
    state = flask.session['state']
    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
        CLIENT_SECRETS_FILE, scopes=SCOPES, state=state)
    flow.redirect_uri = flask.url_for('oauth2callback', _external=True)
    authorization_response = flask.request.url
    flow.fetch_token(authorization_response=authorization_response)
    credentials = flow.credentials
    flask.session['credentials'] = credentials_to_dict(credentials)
    return flask.redirect(flask.url_for('index'))


def revoke():
    if 'credentials' not in flask.session:
        return ('You need to <a href="/authorize">authorize</a> before ' +
            'testing the code to revoke credentials.')
    credentials = google.oauth2.credentials.Credentials(**flask.session[
        'credentials'])
    revoke = requests.post('https://accounts.google.com/o/oauth2/revoke',
        params={'token': credentials.token}, headers={'content-type':
        'application/x-www-form-urlencoded'})
    status_code = getattr(revoke, 'status_code')
    if status_code == 200:
        print('Credentials successfully revoked.')
    else:
        print('An error occurred.')
    return


def clear_credentials():
    if 'credentials' in flask.session:
        del flask.session['credentials']
    return '<span>Session cleared </span>'


def credentials_to_dict(credentials):
    return {'token': credentials.token, 'refresh_token': credentials.
        refresh_token, 'token_uri': credentials.token_uri, 'client_id':
        credentials.client_id, 'client_secret': credentials.client_secret,
        'scopes': credentials.scopes}

import flask
import google.oauth2.credentials
import google_auth_oauthlib.flow
import config
import requests

# This variable specifies the name of a file that contains the OAuth 2.0
# information for this application, including its client_id and client_secret.
CLIENT_SECRETS_FILE = "client_secret.json"
# This OAuth 2.0 access scope allows for full read/write access to the
# authenticated user's account and requires requests to use an SSL connection.
c = config.emailConfiguration()
SCOPES = c["service_scopes"]
API_SERVICE_NAME = c["service_name"]
API_VERSION = c["service_version"]


def authorize():
# Create flow instance to manage the OAuth 2.0 Authorization Grant Flow steps.
    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
        CLIENT_SECRETS_FILE, scopes=SCOPES)

    flow.redirect_uri = flask.url_for('oauth2callback', _external=True)

    authorization_url, state = flow.authorization_url(
        # Enable offline access so that you can refresh an access token without
        # re-prompting the user for permission. Recommended for web server apps.
        access_type='offline',
        # Enable incremental authorization. Recommended as a best practice.
        include_granted_scopes='true')

    # Store the state so the callback can verify the auth server response.
    flask.session['state'] = state

    return flask.redirect(authorization_url)

def oauth2Callback():
    # Specify the state when creating the flow in the callback so that it can
    # verified in the authorization server response.
    state = flask.session['state']

    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
        CLIENT_SECRETS_FILE, scopes=SCOPES, state=state)
    flow.redirect_uri = flask.url_for('oauth2callback', _external=True)

    # Use the authorization server's response to fetch the OAuth 2.0 tokens.
    authorization_response = flask.request.url
    flow.fetch_token(authorization_response=authorization_response)

    # Store credentials in the session.
    # ACTION ITEM: In a production app, you likely want to save these
    #              credentials in a persistent database instead.
    credentials = flow.credentials
    flask.session['credentials'] = credentials_to_dict(credentials)

    return flask.redirect(flask.url_for('index'))

def revoke():
    if 'credentials' not in flask.session:
        return ('You need to <a href="/authorize">authorize</a> before ' +
            'testing the code to revoke credentials.')

    credentials = google.oauth2.credentials.Credentials(
        **flask.session['credentials'])

    revoke = requests.post('https://accounts.google.com/o/oauth2/revoke',
        params={'token': credentials.token},
        headers = {'content-type': 'application/x-www-form-urlencoded'})

    status_code = getattr(revoke, 'status_code')
    if status_code == 200:
        print('Credentials successfully revoked.')
    else:
        print('An error occurred.')

    return

def clear_credentials():
    if 'credentials' in flask.session:
        del flask.session['credentials']
    return '<span>Session cleared </span>'

def credentials_to_dict(credentials):
    return {'token': credentials.token,
          'refresh_token': credentials.refresh_token,
          'token_uri': credentials.token_uri,
          'client_id': credentials.client_id,
          'client_secret': credentials.client_secret,
          'scopes': credentials.scopes}
