<aside> 💡

Dùng mail sinh viên tạo tài khoản Azure và truy cập portal

image.png

</aside>

<aside> 💡

Tìm Azure Active Directory → chọn Microsoft Entra ID

image.png

</aside>

<aside> 💡

Chọn Add application registration

image.png

</aside>

<aside> 💡

Nhập tên app → chọn Accounts in this organizational directory only (VNU-HCMUS only - Single tenant) → Register

image.png

</aside>

<aside> 💡

Copy lại Application(client) ID và Directory(tenant) ID → Add a certificate or secret

image.png

</aside>

<aside> 💡

New client secret → nhập description → copy lại value

image.png

image.png

</aside>

<aside> 💡

Code mẫu để test lấy access token

import requests

def get_access_token(tenant_id, client_id, client_secret):
    url = f"<https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token>"

    payload = {
        'client_id': client_id,
        'scope': '<https://analysis.windows.net/powerbi/api/.default>',
        'client_secret': client_secret,
        'grant_type': 'client_credentials'
    }

    headers = {
        'Content-Type': 'application/x-www-form-urlencoded'
    }

    response = requests.post(url, data=payload, headers=headers)
    if response.status_code == 200:
        access_token = response.json().get('access_token')
        return access_token
    else:
        print(f"Error getting token: {response.status_code} {response.text}")
        return None

# Thay các giá trị này bằng thông tin thực tế của bạn
tenant_id = 'YOUR_TENANT_ID'
client_id = 'YOUR_CLIENT_ID'
client_secret = 'YOUR_CLIENT_SECRET'

token = get_access_token(tenant_id, client_id, client_secret)
if token:
    print("Access token:")
    print(token)
else:
    print("Failed to get access token")

</aside>