【发布时间】:2021-11-23 23:05:18
【问题描述】:
这是我的问题: 我有 3 名成员的 365 Family OneDrive 订阅,我的帐户是管理员。 我正在尝试构建一个 python 应用程序来根据特定标准读取/提取我在这个 onedrive 空间上的文件内容。我想将它构建为命令行应用程序,在我的 PC 上本地运行。我知道为此可能存在一些工具,但我想编写自己的解决方案。
在浏览了大量不同的文档后,我最终做了以下事情
- 在 Azure 门户上注册了我的应用程序
- 授予了对 Microsoft Graph API(User.read、Files.Read 和 Files.ReadAll)的一些权限
- 创建了一个秘密
- 抓取了微软提供的示例代码
- 用我的 Client_Id 和 Secret 替换一些变量
- 运行代码
代码返回访问令牌,但授权请求失败并显示401 - 未经授权:由于凭据无效而拒绝访问。
这是我正在使用的 Python 代码。
import msal
config = {
"authority": "https://login.microsoftonline.com/consumers",
"client_id": "<my client ID>",
"scope": ["https://graph.microsoft.com/.default"],
"secret": "<My secret stuff>",
"endpoint": "https://graph.microsoft.com/v1.0/users"
}
# Create a preferably long-lived app instance which maintains a token cache.
app = msal.ConfidentialClientApplication(
config["client_id"], authority=config["authority"],
client_credential=config["secret"],
)
result = None
result = app.acquire_token_silent(config["scope"], account=None)
if not result:
result = app.acquire_token_for_client(scopes=config["scope"])
if "access_token" in result:
# Calling graph using the access token
graph_data = requests.get( # Use token to call downstream service
config["endpoint"],
headers={'Authorization': 'Bearer ' + result['access_token']}, ).json()
print("Graph API call result: ")
print(json.dumps(graph_data, indent=2))
else:
print(result.get("error"))
print(result.get("error_description"))
print(result.get("correlation_id")) # You may need this when reporting a bug
根据错误信息,我显然在授权过程中遗漏了一些东西,但又不知道是什么。我什至不确定我应该使用的权威和端点。我的帐户是个人帐户,我没有租户。 我需要在某处设置/配置一些 URI 吗?
欢迎任何帮助。 提前谢谢你。
【问题讨论】:
标签: python authentication onedrive msal