def main(client_id, client_secret, scopes):
  """Retrieve and display the access and refresh token."""
  client_config = ClientConfigBuilder(
      client_type=ClientConfigBuilder.CLIENT_TYPE_WEB, client_id=client_id,
      client_secret=client_secret)

  flow = InstalledAppFlow.from_client_config(
      client_config.Build(), scopes=scopes)
  # Note that from_client_config will not produce a flow with the
  # redirect_uris (if any) set in the client_config. This must be set
  # separately.
  flow.redirect_uri = _REDIRECT_URI

  auth_url, _ = flow.authorization_url(prompt='consent')

  print ('Log into the Google Account you use to access your Ad Manager account'
         'and go to the following URL: \n%s\n' % (auth_url))
  print 'After approving the token enter the verification code (if specified).'
  code = raw_input('Code: ').strip()

  try:
    flow.fetch_token(code=code)
  except InvalidGrantError as ex:
    print 'Authentication has failed: %s' % ex
    sys.exit(1)

  print 'Access token: %s' % flow.credentials.token
  print 'Refresh token: %s' % flow.credentials.refresh_token
Retrieve and display the access and refresh token.