def METHOD_NAME(request):
    application_instance = request.lti_user.application_instance
    course_service = request.find_service(name="course")
    scopes = FILES_SCOPES
    if application_instance.settings.get("canvas", "folders_enabled"):
        scopes += FOLDERS_SCOPES
    if application_instance.developer_key and (
        # If the instance could add a new course with sections...
        application_instance.settings.get("canvas", "sections_enabled")
        # ... or any of it's existing courses have sections
        or course_service.any_with_setting("canvas", "sections_enabled", True)
    ):
        scopes += SECTIONS_SCOPES
    if application_instance.settings.get("canvas", "groups_enabled"):
        scopes += GROUPS_SCOPES
    auth_url = urlunparse(
        (
            "https",
            application_instance.lms_host(),
            "login/oauth2/auth",
            "",
            urlencode(
                {
                    "client_id": application_instance.developer_key,
                    "response_type": "code",
                    "redirect_uri": request.route_url("canvas_api.oauth.callback"),
                    "state": OAuthCallbackSchema(request).state_param(),
                    "scope": " ".join(scopes),
                }
            ),
            "",
        )
    )
    return HTTPFound(location=auth_url)