如何在 http Google Cloud Function 中获取 `self`
How to get `self` in http Google Cloud Function
我正在尝试在 Python HTTP Google 云函数中使用 Firebase 身份验证。
但是函数 verify_id_token()
需要 self
作为参数。如何在 HTTP Google 云函数中获取 self
?这是我目前的方法:
def main(request):
print(self)
# Handle CORS
if request.method == 'OPTIONS':
# Allows GET requests from any origin with the Content-Type
# header and caches preflight response for an 3600s
headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': '*',
'Access-Control-Allow-Headers': '*',
'Access-Control-Max-Age': '3600'
}
return '', 204, headers
headers = {
'Access-Control-Allow-Origin': '*'
}
# Validate Firebase session
if 'authorization' not in request.headers:
return f'Unauthorized', 401, headers
authorization = str(request.headers['authorization'])
if not authorization.startswith('Bearer '):
return f'Unauthorized', 401, headers
print(authorization)
id_token = authorization.split('Bearer ')[1]
print(id_token)
decoded_token = auths.verify_id_token(id_token)
uid = str(decoded_token['uid'])
if uid is None or len(uid) == 0:
return f'Unauthorized', 401, headers
我已经尝试将 self
作为参数添加到 main
函数,但这不起作用,因为 request
必须是第一个参数并且没有设置第二个参数,所以 def main(self, request)
和 def main(request, self)
都不起作用。
main 是一种方法而不是 class。不是 class 成员的方法没有 self.
self
是对对象本身的引用。假设您有一个带有属性(方法、属性)的 class。如果你想访问 class 本身的任何一个属性,你需要 self
(有些语言称之为 this
。例如 JavaScript)。
如果您从该 class 创建一个对象并想要访问任何一个属性,您将使用该对象名称。
示例:
class MyClass:
def __init__(self):
pass
def method1(self):
print("Method 1 is called")
def method2(self):
print("I'll call method 1")
self.method1()
看,如果有人想从 method1
调用 method2
,他们将需要 self
。
但是如果您从 MyClass
创建一个对象,您可以使用变量名访问任何 属性:
mc = MyClass()
mc.method1()
TL;DR
您不能(也不需要)在 class.
范围之外访问 self
我正在尝试在 Python HTTP Google 云函数中使用 Firebase 身份验证。
但是函数 verify_id_token()
需要 self
作为参数。如何在 HTTP Google 云函数中获取 self
?这是我目前的方法:
def main(request):
print(self)
# Handle CORS
if request.method == 'OPTIONS':
# Allows GET requests from any origin with the Content-Type
# header and caches preflight response for an 3600s
headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': '*',
'Access-Control-Allow-Headers': '*',
'Access-Control-Max-Age': '3600'
}
return '', 204, headers
headers = {
'Access-Control-Allow-Origin': '*'
}
# Validate Firebase session
if 'authorization' not in request.headers:
return f'Unauthorized', 401, headers
authorization = str(request.headers['authorization'])
if not authorization.startswith('Bearer '):
return f'Unauthorized', 401, headers
print(authorization)
id_token = authorization.split('Bearer ')[1]
print(id_token)
decoded_token = auths.verify_id_token(id_token)
uid = str(decoded_token['uid'])
if uid is None or len(uid) == 0:
return f'Unauthorized', 401, headers
我已经尝试将 self
作为参数添加到 main
函数,但这不起作用,因为 request
必须是第一个参数并且没有设置第二个参数,所以 def main(self, request)
和 def main(request, self)
都不起作用。
main 是一种方法而不是 class。不是 class 成员的方法没有 self.
self
是对对象本身的引用。假设您有一个带有属性(方法、属性)的 class。如果你想访问 class 本身的任何一个属性,你需要 self
(有些语言称之为 this
。例如 JavaScript)。
如果您从该 class 创建一个对象并想要访问任何一个属性,您将使用该对象名称。
示例:
class MyClass:
def __init__(self):
pass
def method1(self):
print("Method 1 is called")
def method2(self):
print("I'll call method 1")
self.method1()
看,如果有人想从 method1
调用 method2
,他们将需要 self
。
但是如果您从 MyClass
创建一个对象,您可以使用变量名访问任何 属性:
mc = MyClass()
mc.method1()
TL;DR
您不能(也不需要)在 class.
范围之外访问self