我将云功能与 python 用作我项目的无服务器 我通过触发Cloud Function将用户添加到我的BigQuery项目中,这样他就可以访问某些表。
我需要从gsutil
获取访问令牌,才能使用API授予用户访问权限。
如何赋予IAM角色或获得对项目的访问令牌,以便可以从Cloud Function中使用它来向用户(通过电子邮件)授予对BigQuery的访问权限。
我正在使用这些API端点:
ENDPOING_GETIAMPOLICY = 'https://cloudresourcemanager.googleapis.com/v1/projects/{resource}:getIamPolicy'
ENDPOING_SETIAMPOLICY = 'https://cloudresourcemanager.googleapis.com/v1/projects/{resource}:setIamPolicy'
要使用此ENDPOING_GETIAMPOLICY
端点,我需要ACCESS_TOKEN
# Preparing get all the current iam users
params = {
'access_token': ACCESS_TOKEN
}
resp = requests.post(ENDPOING_GETIAMPOLICY.format(resource=resource), params=params)
我愿意接受其他建议。
答案 0 :(得分:2)
为了使用Python获取令牌,您可以执行以下操作:
将此添加到requirements.txt:
oauth2client>=4.1.2
像这样在Cloud Function中检索令牌:
def getAccessToken():
from oauth2client.client import GoogleCredentials
credentials = GoogleCredentials.get_application_default()
credentials.get_access_token()
token = credentials.access_token
return verifyToken(token)
def verifyToken(token):
import requests
response = requests.get('https://www.googleapis.com/bigquery/v2/projects/[PROJECT_ID]/datasets', headers={'Authorization': 'Bearer ' + token})
return (response.content)
这将以字符串格式返回访问令牌,然后根据需要将其添加到JSON。