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!