Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么谷歌云函数返回错误:无法处理请求?_Python_Http_Google Cloud Platform_Python Requests_Google Cloud Functions - Fatal编程技术网

Python 为什么谷歌云函数返回错误:无法处理请求?

Python 为什么谷歌云函数返回错误:无法处理请求?,python,http,google-cloud-platform,python-requests,google-cloud-functions,Python,Http,Google Cloud Platform,Python Requests,Google Cloud Functions,我有一个Python中的GCF,每9小时刷新一个访问令牌。 当我测试这个函数时,我得到一个200的响应,成功 但是,当我发送POST请求或试图通过Google Scheduler调用该函数时,我遇到了一个错误错误:在前3次或更长时间内无法处理该请求。发送两个请求后,将执行该函数。为什么? 我的职能如下: from os import getenv import requests import pymysql import json from pymysql.err import Operati

我有一个Python中的GCF,每9小时刷新一个访问令牌。 当我测试这个函数时,我得到一个200的响应,成功

但是,当我发送POST请求或试图通过Google Scheduler调用该函数时,我遇到了一个错误
错误:在前3次或更长时间内无法处理该请求。发送两个请求后,将执行该函数。为什么?

我的职能如下:

from os import getenv
import requests
import pymysql
import json
from pymysql.err import OperationalError


CONNECTION_NAME = getenv('INSTANCE_CONNECTION_NAME', '')
DB_USER = getenv('MYSQL_USER', '')
DB_PASSWORD = getenv('MYSQL_PASSWORD', '')
DB_NAME = getenv('MYSQL_DATABASE', '')

mysql_config = {
  'user': DB_USER,
  'password': DB_PASSWORD,
  'db': DB_NAME,
  'charset': 'utf8mb4',
  'cursorclass': pymysql.cursors.DictCursor,
  'autocommit': True
}

mysql_conn = None


def __get_cursor():
    try:
        return mysql_conn.cursor()
    except OperationalError:
        mysql_conn.ping(reconnect=True)
        return mysql_conn.cursor()


def authMonzo(request):
    global mysql_conn
    global endpoint

    endpoint = ''

    if not mysql_conn:
        try:
            mysql_conn = pymysql.connect(**mysql_config)
        except OperationalError:
            mysql_config['unix_socket'] = f'/cloudsql/{CONNECTION_NAME}'
            mysql_conn = pymysql.connect(**mysql_config)

    with __get_cursor() as cursor:   
        cursor.execute("SELECT * FROM TOKENS ORDER BY ID DESC LIMIT 1")
        result = cursor.fetchall()

        clientSecret = result[...]
        clientId = result[...]
        refreshToken = result[...]

        # Change the last refresh token for a new access token
        tokenRequest = requests.post(endpoint, data={'grant_type': 'refresh_token', 'client_id': clientId, 'client_secret': clientSecret, 'refresh_token': refreshToken})
        tokenJson = json.loads(tokenRequest.content)

        print(tokenJson)

        # Assing the values to variables
        accessToken = tokenJson['access_token']
        refreshToken = tokenJson['refresh_token']
        scope = tokenJson['scope']
        clientId = tokenJson['client_id']
        expiresIn = tokenJson['expires_in']
        userId = tokenJson['user_id']
        tokenType = tokenJson['token_type']

        # Insert the new token on the table tokens
        SQLToken = ("INSERT INTO TOKENS (CLIENT_SECRET, ACCESS_TOKEN, REFRESH_TOKEN, TOKEN_TYPE, USER_ID, CLIENT_ID, EXPIRES_IN, SCOPE) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)")
        Values = (clientSecret, accessToken, refreshToken, tokenType, userId, clientId, expiresIn, scope)

        cursor.execute(SQLToken, Values)
        mysql_conn.close()

    return str(accessToken)

您在哪里看到错误?GCP的哪个组件正在记录错误?Stackdriver日志记录中是否有其他诊断功能?@Kolban当我通过调度程序或HTTP(例如Postman)调用函数时,会发生错误@Marcelo Gazzola-如何验证Postman调用?这意味着在您发出请求之前,rest调用客户端应该通过GCP项目的身份验证。您在哪里看到错误?GCP的哪个组件正在记录错误?Stackdriver日志记录中是否有其他诊断功能?@Kolban当我通过调度程序或HTTP(例如Postman)调用函数时,会发生错误@Marcelo Gazzola-如何验证Postman调用?这意味着在您发出请求之前,rest调用客户端应该通过GCP项目的身份验证。