Skip to content

💂‍♂ - 🥇 🔁

➡️ 🌈 👈 👆 ✔️ 👆 👩‍💻 🛠️ 🆔.

& 👆 ✔️ 🕸 ➕1️⃣ 🆔 ⚖️ 🎏 ➡ 🎏 🆔 (⚖️ 📱 🈸).

& 👆 💚 ✔️ 🌌 🕸 🔓 ⏮️ 👩‍💻, ⚙️ 🆔 & 🔐.

👥 💪 ⚙️ Oauth2️⃣ 🏗 👈 ⏮️ FastAPI.

✋️ ➡️ 🖊 👆 🕰 👂 🌕 📏 🔧 🔎 👈 🐥 🍖 ℹ 👆 💪.

➡️ ⚙️ 🧰 🚚 FastAPI 🍵 💂‍♂.

❔ ⚫️ 👀

➡️ 🥇 ⚙️ 📟 & 👀 ❔ ⚫️ 👷, & ⤴️ 👥 🔜 👟 🔙 🤔 ⚫️❔ 😥.

main.py

📁 🖼 📁 main.py:

from fastapi import Depends, FastAPI
from fastapi.security import OAuth2PasswordBearer

app = FastAPI()

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")


@app.get("/items/")
async def read_items(token: str = Depends(oauth2_scheme)):
    return {"token": token}

🏃 ⚫️

Info

🥇 ❎ python-multipart.

🤶 Ⓜ. pip install python-multipart.

👉 ↩️ Oauth2️⃣ ⚙️ "📨 📊" 📨 username & password.

🏃 🖼 ⏮️:

$ uvicorn main:app --reload

<span style="color: green;">INFO</span>:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

✅ ⚫️

🚶 🎓 🩺: http://127.0.0.1:8000/docs.

👆 🔜 👀 🕳 💖 👉:

✔ 🔼 ❗

👆 ⏪ ✔️ ✨ 🆕 "✔" 🔼.

& 👆 ➡ 🛠️ ✔️ 🐥 🔒 🔝-▶️️ ↩ 👈 👆 💪 🖊.

& 🚥 👆 🖊 ⚫️, 👆 ✔️ 🐥 ✔ 📨 🆎 username & password (& 🎏 📦 🏑):

Note

⚫️ 🚫 🤔 ⚫️❔ 👆 🆎 📨, ⚫️ 🏆 🚫 👷. ✋️ 👥 🔜 🤚 📤.

👉 ↗️ 🚫 🕸 🏁 👩‍💻, ✋️ ⚫️ 👑 🏧 🧰 📄 🖥 🌐 👆 🛠️.

⚫️ 💪 ⚙️ 🕸 🏉 (👈 💪 👆).

⚫️ 💪 ⚙️ 🥉 🥳 🈸 & ⚙️.

& ⚫️ 💪 ⚙️ 👆, ℹ, ✅ & 💯 🎏 🈸.

password 💧

🔜 ➡️ 🚶 🔙 👄 & 🤔 ⚫️❔ 🌐 👈.

password "💧" 1️⃣ 🌌 ("💧") 🔬 Oauth2️⃣, 🍵 💂‍♂ & 🤝.

Oauth2️⃣ 🔧 👈 👩‍💻 ⚖️ 🛠️ 💪 🔬 💽 👈 🔓 👩‍💻.

✋️ 👉 💼, 🎏 FastAPI 🈸 🔜 🍵 🛠️ & 🤝.

, ➡️ 📄 ⚫️ ⚪️➡️ 👈 📉 ☝ 🎑:

  • 👩‍💻 🆎 username & password 🕸, & 🎯 Enter.
  • 🕸 (🏃‍♂ 👩‍💻 🖥) 📨 👈 username & password 🎯 📛 👆 🛠️ (📣 ⏮️ tokenUrl="token").
  • 🛠️ ✅ 👈 username & password, & 📨 ⏮️ "🤝" (👥 🚫 🛠️ 🙆 👉).
    • "🤝" 🎻 ⏮️ 🎚 👈 👥 💪 ⚙️ ⏪ ✔ 👉 👩‍💻.
    • 🛎, 🤝 ⚒ 🕛 ⏮️ 🕰.
      • , 👩‍💻 🔜 ✔️ 🕹 🔄 ☝ ⏪.
      • & 🚥 🤝 📎, ⚠ 🌘. ⚫️ 🚫 💖 🧲 🔑 👈 🔜 👷 ♾ (🏆 💼).
  • 🕸 🏪 👈 🤝 🍕 👱.
  • 👩‍💻 🖊 🕸 🚶 ➕1️⃣ 📄 🕸 🕸 📱.
  • 🕸 💪 ☕ 🌅 💽 ⚪️➡️ 🛠️.
    • ✋️ ⚫️ 💪 🤝 👈 🎯 🔗.
    • , 🔓 ⏮️ 👆 🛠️, ⚫️ 📨 🎚 Authorization ⏮️ 💲 Bearer ➕ 🤝.
    • 🚥 🤝 🔌 foobar, 🎚 Authorization 🎚 🔜: Bearer foobar.

FastAPI'Ⓜ OAuth2PasswordBearer

FastAPI 🚚 📚 🧰, 🎏 🎚 ⚛, 🛠️ 👫 💂‍♂ ⚒.

👉 🖼 👥 🔜 ⚙️ Oauth2️⃣, ⏮️ 🔐 💧, ⚙️ 📨 🤝. 👥 👈 ⚙️ OAuth2PasswordBearer 🎓.

Info

"📨" 🤝 🚫 🕴 🎛.

✋️ ⚫️ 🏆 1️⃣ 👆 ⚙️ 💼.

& ⚫️ 💪 🏆 🏆 ⚙️ 💼, 🚥 👆 Oauth2️⃣ 🕴 & 💭 ⚫️❔ ⚫️❔ 📤 ➕1️⃣ 🎛 👈 ♣ 👻 👆 💪.

👈 💼, FastAPI 🚚 👆 ⏮️ 🧰 🏗 ⚫️.

🕐❔ 👥 ✍ 👐 OAuth2PasswordBearer 🎓 👥 🚶‍♀️ tokenUrl 🔢. 👉 🔢 🔌 📛 👈 👩‍💻 (🕸 🏃 👩‍💻 🖥) 🔜 ⚙️ 📨 username & password ✔ 🤚 🤝.

from fastapi import Depends, FastAPI
from fastapi.security import OAuth2PasswordBearer

app = FastAPI()

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")


@app.get("/items/")
async def read_items(token: str = Depends(oauth2_scheme)):
    return {"token": token}

Tip

📥 tokenUrl="token" 🔗 ⚖ 📛 token 👈 👥 🚫 ✍. ⚫️ ⚖ 📛, ⚫️ 🌓 ./token.

↩️ 👥 ⚙️ ⚖ 📛, 🚥 👆 🛠️ 🔎 https://example.com/, ⤴️ ⚫️ 🔜 🔗 https://example.com/token. ✋️ 🚥 👆 🛠️ 🔎 https://example.com/api/v1/, ⤴️ ⚫️ 🔜 🔗 https://example.com/api/v1/token.

⚙️ ⚖ 📛 ⚠ ⚒ 💭 👆 🈸 🚧 👷 🏧 ⚙️ 💼 💖 ⛅ 🗳.

👉 🔢 🚫 ✍ 👈 🔗 / ➡ 🛠️, ✋️ 📣 👈 📛 /token 🔜 1️⃣ 👈 👩‍💻 🔜 ⚙️ 🤚 🤝. 👈 ℹ ⚙️ 🗄, & ⤴️ 🎓 🛠️ 🧾 ⚙️.

👥 🔜 🔜 ✍ ☑ ➡ 🛠️.

Info

🚥 👆 📶 ⚠ "✍" 👆 💪 👎 👗 🔢 📛 tokenUrl ↩️ token_url.

👈 ↩️ ⚫️ ⚙️ 🎏 📛 🗄 🔌. 👈 🚥 👆 💪 🔬 🌅 🔃 🙆 👫 💂‍♂ ⚖ 👆 💪 📁 & 📋 ⚫️ 🔎 🌖 ℹ 🔃 ⚫️.

oauth2_scheme 🔢 👐 OAuth2PasswordBearer, ✋️ ⚫️ "🇧🇲".

⚫️ 💪 🤙:

oauth2_scheme(some, parameters)

, ⚫️ 💪 ⚙️ ⏮️ Depends.

⚙️ ⚫️

🔜 👆 💪 🚶‍♀️ 👈 oauth2_scheme 🔗 ⏮️ Depends.

from fastapi import Depends, FastAPI
from fastapi.security import OAuth2PasswordBearer

app = FastAPI()

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")


@app.get("/items/")
async def read_items(token: str = Depends(oauth2_scheme)):
    return {"token": token}

👉 🔗 🔜 🚚 str 👈 🛠️ 🔢 token ➡ 🛠️ 🔢.

FastAPI 🔜 💭 👈 ⚫️ 💪 ⚙️ 👉 🔗 🔬 "💂‍♂ ⚖" 🗄 🔗 (& 🏧 🛠️ 🩺).

📡 ℹ

FastAPI 🔜 💭 👈 ⚫️ 💪 ⚙️ 🎓 OAuth2PasswordBearer (📣 🔗) 🔬 💂‍♂ ⚖ 🗄 ↩️ ⚫️ 😖 ⚪️➡️ fastapi.security.oauth2.OAuth2, ❔ 🔄 😖 ⚪️➡️ fastapi.security.base.SecurityBase.

🌐 💂‍♂ 🚙 👈 🛠️ ⏮️ 🗄 (& 🏧 🛠️ 🩺) 😖 ⚪️➡️ SecurityBase, 👈 ❔ FastAPI 💪 💭 ❔ 🛠️ 👫 🗄.

⚫️❔ ⚫️ 🔨

⚫️ 🔜 🚶 & 👀 📨 👈 Authorization 🎚, ✅ 🚥 💲 Bearer ➕ 🤝, & 🔜 📨 🤝 str.

🚥 ⚫️ 🚫 👀 Authorization 🎚, ⚖️ 💲 🚫 ✔️ Bearer 🤝, ⚫️ 🔜 📨 ⏮️ 4️⃣0️⃣1️⃣ 👔 📟 ❌ (UNAUTHORIZED) 🔗.

👆 🚫 ✔️ ✅ 🚥 🤝 🔀 📨 ❌. 👆 💪 💭 👈 🚥 👆 🔢 🛠️, ⚫️ 🔜 ✔️ str 👈 🤝.

👆 💪 🔄 ⚫️ ⏪ 🎓 🩺:

👥 🚫 ✔ 🔬 🤝, ✋️ 👈 ▶️ ⏪.

🌃

, 3️⃣ ⚖️ 4️⃣ ➕ ⏸, 👆 ⏪ ✔️ 🐒 📨 💂‍♂.