我正在嘗試在 Python HTTP Google Cloud 函式中使用 Firebase 身份驗證。但是該函式verify_id_token()需要self作為引數。如何進入selfHTTP Google Cloud 函式?這是我目前的方法:
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)作業。
uj5u.com熱心網友回復:
main是一個方法而不是一個類。不是類成員的方法沒有self。
uj5u.com熱心網友回復:
self是對物件本身的參考。假設您有一個具有屬性(方法、屬性)的類。如果您想訪問類本身中的任何一個屬性,您將需要self(某些語言稱它為this. 例如 JavaScript)。
如果您從該類創建一個物件并想要訪問任何一個屬性,您將使用物件名稱。
例子:
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()
看,如果一個人想從他們那里打電話method2,method1他們將需要self.
但是,如果您從中創建物件,MyClass則可以使用變數名訪問任何屬性:
mc = MyClass()
mc.method1()
TL;博士
您不能(也不需要)訪問self類范圍之外的內容。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/446114.html
