我正在尝试使用 Python 脚本向 Google Apps 脚本网络应用程序发送 POST 请求。虽然在 Web 浏览器中使用 GET 请求访问 Web 应用程序的 URL 可以正常工作,但使用 Python 发送 POST 请求会导致 401 未经授权的错误。
认证流程如下:
from google_auth_oauthlib.flow import InstalledAppFlow
SCOPES = ['openid', 'https://www.googleapis.com/auth/script.projects', 'https://www.googleapis.com/auth/script.webapp.deploy']
CLIENT_SECRETS_FILE = 'client_secret.json'
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES, redirect_uri='http://localhost:80/')
credentials = flow.run_local_server(port=80)
headers = {
'Authorization': f'Bearer {credentials.token}',
'Content-Type': 'application/json',
}
# Checking token validity
if credentials.valid:
print("Token is valid.")
else:
print("Token is not valid.")
# Checking if the token has expired
if credentials.expired:
print("Token has expired.")
else:
print("Token is still valid.")
print("Scopes granted to the token:", credentials.scopes)
print("Access token:", credentials.token)
try:
response = requests.post(url, headers=headers1, json={"text": "hello from python with OAuth"})
response.text
response.raise_for_status() # 400 or 500 Error
except requests.exceptions.HTTPError as err:
print(f"HTTP error occurred: {err}") # HTTP Error
except Exception as err:
print(f"An error occurred: {err}") # Other Error
Apps Script Web 应用程序的 appsscript.json 设置如下:
{
"timeZone": "Asia/Seoul",
"dependencies": {},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8",
"webapp": {
"executeAs": "USER_ACCESSING",
"access": "DOMAIN"
}
}
令牌有效,并且授予的范围没有问题。根据Google Cloud Console,用户具有所有者权限,并且Apps Script API已启用。
尽管有这些设置和条件,我无法理解为什么 POST 请求失败。 Web 应用程序的 doPost 函数配置为处理 JSON 类型的 POST 数据。对于我可能缺少的任何建议,我将不胜感激。
谢谢你。
我尝试过的:
我使用 Python 脚本向 Google Apps 脚本网络应用程序发送了 POST 请求。我使用 OAuth 2.0 进行身份验证,在标头中包含访问令牌,然后发送请求。 Google Apps 脚本中的 doPost 函数被设置为接收和处理 JSON 数据。
我的期望:
成功完成身份验证过程并获得有效的访问令牌后,我希望 Web 应用程序能够接收 POST 请求并做出适当的响应。
实际发生的事情:
虽然通过 Web 浏览器使用 GET 请求访问 Web 应用程序工作正常,但通过 Python 脚本发送 POST 请求会导致 401 未经授权错误。因此,网络应用程序不会处理该请求。
关于状态码
401
,意思是The HyperText Transfer Protocol (HTTP) 401 Unauthorized response status code indicates that the client request has not been completed because it lacks valid authentication credentials for the requested resource.
。 参考
为了使用访问令牌请求Web应用程序,在当前阶段,需要包含Drive API的范围。当我看到你的展示脚本时,我猜想这可能是由于你当前问题的原因。那么,下面的修改怎么样?
SCOPES = ['openid', 'https://www.googleapis.com/auth/script.projects', 'https://www.googleapis.com/auth/script.webapp.deploy']
SCOPES = ["https://www.googleapis.com/auth/drive"]
或
SCOPES = ["https://www.googleapis.com/auth/drive.readonly"]
或者,如果您需要使用
'openid', 'https://www.googleapis.com/auth/script.projects', 'https://www.googleapis.com/auth/script.webapp.deploy'
的范围,请修改如下。
SCOPES = ["https://www.googleapis.com/auth/drive", 'openid', 'https://www.googleapis.com/auth/script.projects', 'https://www.googleapis.com/auth/script.webapp.deploy']
或
SCOPES = ["https://www.googleapis.com/auth/drive.readonly", 'openid', 'https://www.googleapis.com/auth/script.projects', 'https://www.googleapis.com/auth/script.webapp.deploy']