Back

appwrite.exception.AppwriteException: app.67735d03002b656bc8f1@service.cloud.appwrite.io (role: appl

  • 0
  • Auth
FrostbyteNinja
6 Feb, 2025, 02:36

Hello Everyone i am using python

TL;DR
Code is trying to authenticate user login but encountering a scope error (missing 'account' scope) when attempting to retrieve user account information. Ensure appropriate scopes are granted to the API key. Second section decodes session token for the dashboard; make sure session token is valid. Also, be consistent with calling functions/service names.
FrostbyteNinja
6 Feb, 2025, 02:36

Hello Everyone i am using python

Database.py

TypeScript

settings = config()
client = Client()
client.set_endpoint(settings.get("APPWRITE", {}).get("ENDPOINT"))
client.set_project(settings.get("APPWRITE", {}).get("PROJECT_ID"))
client.set_key(settings.get("APPWRITE", {}).get("API_KEY_SECRET"))

# Initialize services
database_service = Databases(client)
users_service = Users(client)
account_service = Account(client)
messaging_service = Messaging(client)
teams_service = Teams(client)
strorage_service = Storage(client)
health_service = Health(client)
functions_service = Functions(client)

i am trying to get user session i have tried multiple stuff

First Try

Dashboard.py

TypeScript
@dashboard_bp.route("/dashboard", methods=["GET", "POST"])
def dashboard():
    session_token = request.cookies.get("session")
    if not session_token:
        logging.warning("No session token found.")
        return redirect(url_for("login_bp.login_page"))
    
    print(account_service.get())

    return "Hello"
FrostbyteNinja
6 Feb, 2025, 02:37

Second Try:

TypeScript

current_dir = os.path.dirname(os.path.abspath(__file__))
template_dir = os.path.join(current_dir, "html")

dashboard_bp = Blueprint("dashboard_bp", __name__, template_folder=template_dir)

def decode_session_token(token):
    """Decodes a Base64-encoded, zlib-compressed session token."""
    try:
        parts = token.split(".")
        if len(parts) < 2:
            raise ValueError("Invalid session token format")

        payload = parts[1] + "=" * (-len(parts[1]) % 4)

        raw_decoded = base64.urlsafe_b64decode(payload)

        decompressed = zlib.decompress(raw_decoded)

        decoded_data = json.loads(decompressed.decode("utf-8"))
        return decoded_data
    except Exception as e:
        logging.error(f"Error decoding session token: {e}")
    return None

@dashboard_bp.route("/dashboard", methods=["GET", "POST"])
def dashboard():
    session_token = request.cookies.get("session")
    if not session_token:
        logging.warning("No session token found.")
        return redirect(url_for("login_bp.login_page"))
    

    session_data = decode_session_token(session_token)
    print(f"Decoded session data: {session_data}")

    if not session_data or "userId" not in session_data or "$id" not in session_data["userId"]:
        print.warning("Invalid session data.")
        return redirect(url_for("login_bp.login_page"))

    user_id = session_data["userId"]["$id"]
    user_info = account_service.get(user_id=user_id)

    print("User Info:" + user_info)

    if not user_info:
        logging.warning(f"No user found for ID {user_id}")
        return redirect(url_for("login_bp.login_page"))

    logging.info(f"User Info: {user_info}")
    return jsonify(user_info)  # Return user info as JSON
FrostbyteNinja
6 Feb, 2025, 02:41

One more thing when i go to the website

https://cloud.appwrite.io/v1/account/

i get users information

Steven
6 Feb, 2025, 02:44

Where's your code for creating and storing the session?

And what are you calling that's throwing the exception.

And what's the full exception

FrostbyteNinja
6 Feb, 2025, 02:44

i am trying to get the user account using services_account.get()

FrostbyteNinja
6 Feb, 2025, 02:44

the session is active and user is logged in

FrostbyteNinja
6 Feb, 2025, 02:45

but i keep on getting the scope error when i have all scopes active for the api key

FrostbyteNinja
6 Feb, 2025, 02:45

@Steven sorry for ping if you are here

FrostbyteNinja
6 Feb, 2025, 02:48

full error logs here cause i do not have nitro

https://pastecode.io/s/ow849ibp

FrostbyteNinja
6 Feb, 2025, 02:56
TypeScript


@login_bp.route("/user/login", methods=["GET", "POST"])
def login_page():
    auth_3_enabled = False
    if request.method == "POST":
        username = request.form.get("email")
        password = request.form.get("password")

        user = authenticate_user(username, password)


        if user:
            return redirect(url_for("dashboard_bp.dashboard"))  # Replace with actual dashboard route
        else:
            return render_template("LoginPage.html", error="Invalid username or password")

    return render_template("LoginPage.html", auth_3_enabled=auth_3_enabled)
FrostbyteNinja
6 Feb, 2025, 02:56
TypeScript

def authenticate_user(email, password):
    try:
        response = account_service.create_email_password_session(email=email, password=password)
        if response:
            log({"type": "info", "message": f"โœ… [+] User {email} logged in successfully"})
            return response
        else:
            log({"type": "warn", "message": f"โ›” [!] Invalid credentials for {email}"})
            return None
    except Exception as e:
        log({"type": "error", "message": f"โ›” [-] Authentication failed for {email}: {e}"})
        return None
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more