从嵌入式设备访问bigquery API



我打算通过REST API将数据从嵌入式设备加载到BigQuery。

  1. 我的BigQuery初始化已经完成,我正在使用python库访问相同的内容
  2. 我的嵌入式设备使用这些参数创建JWT,这些参数与我的服务帐户JSON哈希
{
"iss": "*account*@*project*.iam.gserviceaccount.com",
"iat": "*thistime*",
"exp": "*time +20mins*",
"aud": "https://bigquery.googleapis.com",
"sub": "*account*@*project*.iam.gserviceaccount.com"
}
  1. 我的HTTP请求
curl -X GET -H 'Authorization: Bearer *my JWT*' -H 'content-type: application/json' -v -i 'https://bigquery.googleapis.com/bigquery/v2/projects/*project*/datasets/*dataset*?key=*API Key*'

我尝试使用TCP终端应用程序进行测试如果配方有什么问题,请向我解释。

  1. 我首先需要知道BigQuery是否允许这样的访问
  2. 请告诉我JSON字符串中的应该是什么
  3. 如果我做错了,请告诉我正确的程序

我参考了这篇文章,但在我的场景中不能这样做:

带有JWT代币的Google云存储JSON API

[已编辑]查看此链接以配置嵌入式设备的身份验证模型-https://developers.google.com/identity/protocols/oauth2/service-account#jwt-授权

  1. 创建一个服务帐户并下载json密钥文件
  2. 创建JWT令牌。我使用PyJWT库创建了一个。根据文档,在这个python文件中填写JWT字段,然后运行它:
import time, jwt
iat = time.time()
exp = iat + 3600
PRIVATE_KEY_ID_FROM_JSON = "<private_key_id from json key file>"
PRIVATE_KEY_FROM_JSON = "<private_key from json file>"
payload = {'iss': '<client_email field from json key file>',
'sub': '<client_email field from json key file>',
'aud': 'https://bigquery.googleapis.com/',
'iat': iat,
'exp': exp}
additional_headers = {'kid': PRIVATE_KEY_ID_FROM_JSON}
signed_jwt = jwt.encode(payload, PRIVATE_KEY_FROM_JSON, headers=additional_headers,algorithm='RS256')
print(signed_jwt)
  1. 测试BigQuery API
curl -H "Authorization: Bearer <generated JWT>" https://bigquery.googleapis.
com/bigquery/v2/projects/<GCP project name>/datasets/<test dataset name>

最新更新