Integrating Microsoft Forms Authentication in Python Django: Troubleshooting Terminal Error

To integrate the Microsoft Forms authentication flow into my Python Django project for accessing various forms URLs and storing form details and responses, I’m employing the provided MS Forms authentication code within my project’s backend. Additionally, I’ve configured my project to run within a Docker container.

The MS Forms authentication code snippet, enclosed below, outlines the process:


import json 

import os 

import django.core.management.base 

import requests 

from azure.identity import DefaultAzureCredential, InteractiveBrowserCredential, AzureCliCredential 

  

# Custom Django command definition 

class Command(django.core.management.base.BaseCommand): 

    def handle(self, *args, **options): 

        # Select one of the credential objects to obtain an access token 

        # cred = AzureCliCredential()  # e.g., via `az login` 

        cred = InteractiveBrowserCredential() 

        # cred = DefaultAzureCredential() 

  

        # Request an access token with the specified scope 

        scope = "https://forms.office.com/.default" 

        token = cred.get_token(scope) 

        print("===============================") 

        print(f"{token.expires_on = }") 

        print("===============================") 

  

        tenantId = "tenant id" 

        groupId = "group id" 

        formId = "form id" 

  

        # Provide the access token in the request header 

        headers = {"Authorization": f"Bearer {token.token}"} 

  

        # Retrieve all Forms for a Microsoft 365 Group 

        url = f"https://forms.office.com/formapi/api/{tenantId}/groups/{groupId}/forms" 

        list_response = requests.get(url, headers=headers) 

        print(f"All Forms: {list_response.json()}") 

  

        # Retrieve details for a specific group form 

        url = f"https://forms.office.com/formapi/api/{tenantId}/groups/{groupId}/forms('{formId}')" 

        list_response = requests.get(url, headers=headers) 

        print(f"Form Detail: {list_response.json()}") 

  

        # Retrieve questions from a group form 

        url = f"https://forms.office.com/formapi/api/{tenantId}/groups/{groupId}/forms('{formId}')/questions" 

        list_response = requests.get(url, headers=headers) 

        print(f"Form questions: {list_response.json()}") 

  

        # Retrieve responses to a group form 

        url = f"https://forms.office.com/formapi/api/{tenantId}/groups/{groupId}/forms('{formId}')/responses" 

        list_response = requests.get(url, headers=headers) 

        print(f"Form responses: {list_response.json()}") 

 

However, during execution, I encountered the following error in my terminal:


[2024-05-01 07:15:41,963] {_universal.py:475} INFO - Request URL: 'https://login.microsoftonline.com:443/organizations/v2.0/.well-known/openid-configuration'/nRequest method: 'GET'/nRequest headers:/n    'User-Agent': 'azsdk-python-identity/1.14.0 Python/3.8.7 (Linux-6.5.0-1020-oem-x86_64-with-glibc2.2.5)'/nNo body was attached to the request 

[2024-05-01 07:15:42,211] {_universal.py:503} INFO - Response status: 200/nResponse headers:/n    'Cache-Control': 'max-age=86400, private'/n    'Content-Type': 'application/json; charset=utf-8'/n    'Strict-Transport-Security': 'REDACTED'/n    'X-Content-Type-Options': 'REDACTED'/n    'Access-Control-Allow-Origin': 'REDACTED'/n    'Access-Control-Allow-Methods': 'REDACTED'/n    'P3P': 'REDACTED'/n    'x-ms-request-id': 'e1684310-96f3-41e4-a6eb-65211103ff00'/n    'x-ms-ests-server': 'REDACTED'/n    'x-ms-srs': 'REDACTED'/n    'X-XSS-Protection': 'REDACTED'/n    'Set-Cookie': 'REDACTED'/n    'Date': 'Wed, 01 May 2024 11:15:41 GMT'/n    'Content-Length': '1589' 

[2024-05-01 07:15:42,219] {authcode.py:254} INFO - Open a browser on this device to visit: https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize?client_id=04b07795-8ddb-461a-bbee-02f9e1bf7b46&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A42687&scope=https%3A%2F%2Fforms.office.com%2F.default+offline_access+openid+profile&state=ZWiINUHPhfSVnRKA&code_challenge=yMrfoYd9HzvR90L03To-Dq0We8DZX5PJTjZB0mwARsw&code_challenge_method=S256&nonce=b2541aafcf591b21e143423d36feea131abae1cab38f82e7e107112886f2b68c&client_info=1&prompt=select_account 

[2024-05-01 07:15:42,221] {authcode.py:264} WARNING - Found no browser in current environment. If this program is being run inside a container which has access to host network (i.e. started by `docker run --net=host -it ...`), you can use browser on host to visit the following link. Otherwise, this auth attempt would either timeout (current timeout setting is None) or be aborted by CTRL+C. Auth URI: https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize?client_id=04b07795-8ddb-461a-bbee-02f9e1bf7b46&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A42687&scope=https%3A%2F%2Fforms.office.com%2F.default+offline_access+openid+profile&state=ZWiINUHPhfSVnRKA&code_challenge=yMrfoYd9HzvR90L03To-Dq0We8DZX5PJTjZB0mwARsw&code_challenge_method=S256&nonce=b2541aafcf591b21e143423d36feea131abae1cab38f82e7e107112886f2b68c&client_info=1&prompt=select_account 

 

Your insights or assistance in resolving this issue would be greatly appreciated.

Thank you in advance!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật