使用Python进行GCP BigQuery的Google OAuth 2.0 [英] Google OAuth 2.0 using Python for GCP BigQuery

查看:38
本文介绍了使用Python进行GCP BigQuery的Google OAuth 2.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个代码段,以使用python连接到GCP Big Query服务来实现oAuth 2.0身份验证.

I am seeking a code snippet for implementing oAuth 2.0 authentication using python to connect to GCP Big Query service.

我正在使用Google云外壳编写python代码.但是访问令牌我收到了错误的请求.

I am using Google cloud shell to write the python code. But the access token I am receiving bad request.

access_token = google.fetch_token(token_url=token_url,client_id=client_id,client_secret=client_secret,authorization_response=redirect_response).

我还需要使该过程自动化,因此需要避免手动粘贴redirect_response.

Also I need to automate this process so manually pasting the redirect_response needs to be avoided.

推荐答案

您将需要用于导出到json的serviceaccount的凭据.GCP-> IAM和管理->服务帐户,在三个小点下,您会找到帐户的创建密钥.

You will need credentials for a serviceaccount exported to a json. GCP -> IAM and Admin -> Service Accounts and under the three little dots you will find create key for your account.

如之前的答案中所述,您还需要 BigQuery库

As mentioned in previous answers you will also need the BigQuery library

那么类似的事情就可以了

Then something like this would work

from google.cloud import bigquery
from google.oauth2 import service_account

def BigQuery():
  try:
    credentials = service_account.Credentials.from_service_account_file(
      '/Credentials.json')
    project_id = '[project_id]
    client = bigquery.Client(credentials= credentials,project=project_id)

    query = ('SELECT Column1, Column2 FROM `{}.{}.{}` limit 20'.format('[project_id]','[dataset]','[table]'))
    query_job = client.query(query)
    results = query_job.result()
    for row in results:
      print('Column1 1 : {}, Column 2: {}'.format(row.Column1, row.Column2))
  except:
    print('Error!')



if __name__ == '__main__':
  BigQuery()

这篇关于使用Python进行GCP BigQuery的Google OAuth 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆