尽管成功获取了身份验证令牌,但在尝试访问 Microsoft Forms URL 时遇到 403 错误

问题描述 投票:0回答:1

我目前正在开发一个项目,需要从我的 Python 后端访问与 Microsoft Forms 相关的各种 URL。但是,我在尝试这样做时遇到了 403 错误。以下是我想要实现的目标以及迄今为止已采取的步骤的详细说明:

访问网址: 检索 Microsoft 365 组的所有表单:

https://forms.office.com/formapi/api/{tenantId}/groups/{groupId}/forms

获取团体表格的详细信息:

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

从小组表格中获取问题:

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

检索对小组表单的回复:

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

目标:从后端访问 Microsoft Forms 数据的 URL。

方法:

首先,我登录 MS Forms 网站。

然后,我将必要的参数(tenantId 和 groupId)附加到我想要访问的 URL,并且我收到 JSON 格式的浏览器响应。

现在使用后端请求,我尝试通过传递所需的身份验证令牌来访问这些 URL。

问题:尽管使用提供的代码片段成功获取了身份验证令牌,但当我尝试使用生成的令牌访问 Microsoft Forms URL 时,收到了 403 错误。

这是我用来获取令牌的代码片段:


token_url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token" 

data = { 

    "grant_type": "client_credentials", 

    "client_id": client_id, 

    "client_secret": client_secret, 

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

} 

response = requests.post(token_url, data=data) 

response.raise_for_status()  # Raise an exception for non-200 status codes 

这是我用来使用获得的令牌访问 Microsoft Forms URL 的代码片段:


headers = { 

    'Authorization': f'Bearer {response.json()["access_token"]}', 

    'Content-Type': 'application/json', 

} 

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

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

我不确定为什么尽管我拥有有效的令牌,但仍收到 403 错误。我按照这篇文章中提到的说明进行操作,但没有成功。

任何有关解决此问题的见解或帮助将不胜感激。

提前谢谢您!

microsoft-graph-api azure-authentication ms-forms django-microsoft-authentication
1个回答
0
投票

最初,当我使用客户端凭据流生成的令牌来调用 MS Forms API 时,我也遇到了同样的错误

import requests

tenant_id = "tenantId"
client_id = "appID"
client_secret = "secret"

token_url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token"

data = {
    "grant_type": "client_credentials",
    "client_id": client_id,
    "client_secret": client_secret,
    "scope": "https://forms.office.com/.default"
}

response = requests.post(token_url, data=data)

headers = {
    'Authorization': f'Bearer {response.json()["access_token"]}',
    'Content-Type': 'application/json',
}

base_url = "https://forms.office.com/formapi/api/tenantId/groups/groupId/forms"

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

回复:

enter image description here

要解决该错误,您需要切换到委托流程来生成不记名令牌,该令牌要求用户至少登录一次。

就我而言,我在终端中运行以下 CLI 命令来连接 Azure 帐户最初,如下所示:

az login --tenant tenantID

enter image description here

现在,我运行了下面的示例 Python 代码,该代码使用

azure-identity
模块,通过使用 AzureCliCredential() 方法生成令牌,并成功在 response 中获取表单详细信息:

import requests

from azure.identity import DefaultAzureCredential, InteractiveBrowserCredential, AzureCliCredential

# Use one of these credential objects to get an access token
cred = AzureCliCredential() # i.e. `az login`
# cred = InteractiveBrowserCredential()
# cred = DefaultAzureCredential()

# Request an access token with the following scope
scope = "https://forms.office.com/.default"

token = cred.get_token(scope)

tenantId = "tenantId"
groupId = "groupId"

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

# Provide the access token in the request header
headers = {"Authorization": f"Bearer {token.token}"}

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

回复:

enter image description here

参考:

如何使用代码访问在线 MS Form 回复? - 9nut

的堆栈溢出
© www.soinside.com 2019 - 2024. All rights reserved.