使用Python脚本触发Power Automate流程失败(401错误)求助
调用Power Automate触发Teams消息时返回401未授权错误
我尝试通过Python脚本借助Power Automate向Teams发送消息,编写的代码如下:
import requests # Import requests library import datetime # Get current time. now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") # Triggering Power Automate Flow flow_url='https://***/triggers/manual/paths/invoke?api-version=2016-06-01' # Replace with actual URL from step 2 response=requests.post(flow_url,json={"status":"Script Completed","timestamp":now}) if response.status_code==200: print('PowerAutomate Flow triggered successfully') else: print(f'Failed to trigger PowerAutomate Flow: {response.status_code}')
执行后出现错误:
Failed to trigger PowerAutomate Flow: 401
Power Automate流程截图:
完整流程截图:
恳请帮忙排查触发失败的原因。
错误排查方向(针对401未授权)
触发器权限配置错误
检查手动触发器的「谁可以触发此流程」设置:如果当前设为「仅我」,外部脚本调用会被拒绝。需要根据需求修改为「组织中的任何人」或「任何人(包括外部用户)」,注意平衡权限开放程度和安全性。触发URL不完整
你使用的URL仅包含api-version参数,缺少Power Automate手动触发器必需的授权参数(如sp、sv、sig、code等)。正确的触发URL应该从Power Automate的「获取触发器URL」功能完整复制,格式类似:https://xxx/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=xxx缺失这些参数会导致身份验证失败,返回401。
请求缺少身份验证头
如果你的组织要求通过Azure AD身份验证触发流程,Python请求需要添加Authorization头。示例:headers = {"Authorization": "Bearer <你的Azure AD访问令牌>"} response = requests.post(flow_url, json={"status":"Script Completed","timestamp":now}, headers=headers)可通过Azure AD应用注册获取对应的访问令牌。
组织安全策略限制
部分企业会设置Power Platform安全策略,比如限制特定IP范围调用流程、禁止外部匿名调用等。如果上述配置都正确仍报错,需联系企业管理员确认是否有相关策略限制。
内容的提问来源于stack exchange,提问作者Salman
相关产品推荐
相关产品推荐

