python-3.x - 基于 Google Discovery 的 API :: 无法创建 API 客户端(SCOPES 错误)
问题描述
我正在尝试构建自定义挂钩,以便连接到仅受Google API 客户端库支持的 Google API (不幸的是,谷歌云客户端库不支持!)
这是我对服务的身份验证部分的代码:
class MyGoogleDiscoveryServiceHook(GoogleCloudBaseHook):
client = None
serviceName = "analyticsreporting"
version = "v4"
def __init__(self, conn_id = None, delegate_to = None):
super().__init__(conn_id,delegate_to)
self.conn_id = self.get_connection("google_analytics")
self.extras = self.conn_id.extra_dejson
self.KEY_FILE_LOC = self.extras.get("extra__google_cloud_platform__key_path")
self.SCOPES = self.extras.get("extra__google_cloud_platform__scope")
def get_conn(self):
if not self.client:
from google.oauth2 import service_account
credentials = service_account.Credentials.from_service_account_file(filename = self.KEY_FILE_LOC,
scopes = self.SCOPES)
from googleapiclient.errors import Error
self.client = build(self.serviceName, self.version,
#http=http_authorized,
#credentials = None,
http=None,
credentials = credentials,
cache_discovery=False,
)
return self.client
但是,当我在自定义运算符中使用此钩子时,出现错误:
[2019-12-10 21:43:41,577] {taskinstance.py:1058} 错误 - ('invalid_scope: h 不是有效的受众字符串。', '{\n "error": "invalid_scope",\n " error_description": "h 不是有效的受众字符串。"\n}')
我很确定,在 Airflow Connections 设置中,我输入了正确https://www.googleapis.com/auth/analytics.readonly
的范围,并且 DAG 正在从 Airflow Connection 字符串中正确读取范围。
谁能建议这里有什么问题?
提前致谢!
解决方案
我的猜测是scopes
您从 extras 中获得的值是纯字符串,而不是元组/列表。GoogleCloudBaseHook
提示预期格式的默认范围值( https://github.com/apache/airflow/blob/1.10.5/airflow/contrib/hooks/gcp_api_base_hook.py#L37):
_DEFAULT_SCOPES = ('https://www.googleapis.com/auth/cloud-platform',)
错误消息进一步强化了这一点,因为它似乎试图访问您传入的索引零,在您的字符串的情况下,这是h
错误所抱怨的。
推荐阅读
- docker - wsl.exe -l -v 系统找不到指定的路径
- javascript - Webpack devServer 不尊重 webpack.config.js 中的 devServer 选项
- java - 触发单选按钮时显示信息
- php - 如何使用 PHP 函数从文件、样式表或 javascript 中去除注释?
- javascript - 尝试在 oncompleted 方法中访问状态
- google-api - 停止在谷歌日历上推送通知
- c# - EF Core 在 OnModelCreating 时创建影子 FK 索引并导致运行时错误
- mongoose - mongoose 中的匹配验证器让我发疯
- pandas - Pandas 仅将第一页抓取的数据保存到 CSV
- unity3d - 在统一中,您如何在给定的世界空间位置找到体素信息?