code: def require_api_auth(allow_anonymous=False):
      
      def wrapper(f):
          """Wrap function with oauth require decorator."""
          f_oauth_required = oauth2.require_oauth()(f)
  
          @wraps(f)
          def decorated(*args, **kwargs):
              """Require OAuth 2.0 Authentication."""
              if not hasattr(current_user, 'login_via_oauth2'):
                  if not current_user.is_authenticated:
                      if allow_anonymous:
                          return f(*args, **kwargs)
                      abort(401)
                  if current_app.config['ACCOUNTS_JWT_ENABLE']:
                      # Verify the token
                      current_oauth2server.jwt_veryfication_factory(
                          request.headers)
                  # fully logged in with normal session
                  return f(*args, **kwargs)
              else:
                  # otherwise, try oauth2
                  return f_oauth_required(*args, **kwargs)
          return decorated
      return wrapper
docs: Decorator to require API authentication using OAuth token.
  
      :param allow_anonymous: Allow access without OAuth token
          (default: ``False``).