【问题标题】:How Can I make flask API to access GraphAPI for get excel file content如何使烧瓶 API 访问 GraphAPI 以获取 excel 文件内容
【发布时间】:2021-09-30 20:13:51
【问题描述】:

我想制作一个 API 来访问图形 API,但没有登录页面和用户身份验证。

token_url = "https://login.microsoftonline.com/<tenantID>/oauth2/v2.0/token"
token_data = {
 'grant_type': 'client_credentials',
 'client_id': app_id,
 'client_secret': client_secret,
 'scope':'https://graph.microsoft.com/.default',

}
token_r = requests.post(token_url, data=token_data)

token = token_r.json().get('access_token')
print(token)

users_url = **"https://graph.microsoft.com/v1.0/drive/items/<FileID>/workbook/worksheets('Sheet1')/range(address='a1:v500')/UsedRange(valuesOnly=true)?$format=atom,$select=values"**

headers = {
 'Authorization': 'Bearer {}'.format(token)
}

authHeader = {
    'Content-Type':'application/json',
    'Authorization':"Bearer " + token,
    'ExpiresOn':str(token_r.json().get('expires_in'))
}

k = requests.get(users_url, headers=authHeader)
print(k.text)

现在它返回 {'error': {'code': 'AccessDenied', 'message': '无论是 scp 还是角色声明都需要存在于令牌中。',... }}强>

我知道,我需要从 Azure 提供授予管理员许可,但我们无权访问管理员门户。那么有没有其他方法可以在没有登录或管理员同意的情况下读取 XLSX 文件?

【问题讨论】:

    标签: python flask azure-ad-graph-api


    【解决方案1】:

    这里有几点需要注意-

    1. 在客户端凭据流中 - 令牌将具有应用程序声明,即在应用程序的上下文中运行。因此,您不能在此处使用 /me 端点。在这种情况下尝试使用 /users 端点或使用其他 Oauth 2.0 流(如授权代码流)来获取承载令牌。

    2. 要获取 driveitem 资源,需要以下权限。确保在获取不记名令牌时将它们添加到范围。

    【讨论】:

    • 感谢 Shweta 的快速回复。我用新场景编辑了问题。请检查并指导我。
    • 也许您可以尝试以编程方式同意。在这种情况下,您还可以利用 PowerShell。看看这是否有帮助 - stackoverflow.com/questions/65302583/…
    猜你喜欢
    • 2013-03-07
    • 2018-02-18
    • 2013-04-09
    • 2022-06-20
    • 2021-11-12
    • 2019-12-29
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    相关资源
    最近更新 更多