简体   繁体   English

谷歌 OAuth 2.0 使用 Python 用于 GCP BigQuery

[英]Google OAuth 2.0 using Python for GCP BigQuery

I am seeking a code snippet for implementing oAuth 2.0 authentication using python to connect to GCP Big Query service.我正在寻找一个代码片段,用于使用 python 连接到 GCP 大查询服务来实现 oAuth 2.0 身份验证。

I am using Google cloud shell to write the python code.我正在使用谷歌云 shell 编写 python 代码。 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).

Also I need to automate this process so manually pasting the redirect_response needs to be avoided.另外我需要自动化这个过程,所以需要避免手动粘贴redirect_response。

It is recommended that you use the BigQuery Python client library.建议您使用 BigQuery Python 客户端库。 Pip package google-cloud-bigquery provides this. Pip package google-cloud-bigquery提供了这个。 You will also need to setup GOOGLE_APPLICATION_CREDENTIALS with the service account json file.您还需要使用服务帐户 json 文件设置 GOOGLE_APPLICATION_CREDENTIALS。

Using this process you wont need to deal with token generation and renewal as this process is taken care by the client libraries in the background.使用此过程,您无需处理令牌生成和更新,因为此过程由后台的客户端库处理。

Please see BigQuery Client Libraries Python section for detailed instructions.有关详细说明,请参阅BigQuery 客户端库Python 部分。

In the BigQuery Client Libraries it is documented how to set up the authentication both from the GCP console and the Command Line .BigQuery 客户端库中,记录了如何从 GCP 控制台和命令行设置身份验证。

To employ the BigQuery API library you need to authenticate your service account.要使用BigQuery API 库,您需要对服务帐户进行身份验证。 The gcloud command gcloud iam service-accounts keys create [FILE_NAME].json --iam-account [NAME]@[PROJECT_ID].iam.gserviceaccount.com generates a JSON key file with the necessary private information (like your project_id, private key, etc) to do so. The gcloud command gcloud iam service-accounts keys create [FILE_NAME].json --iam-account [NAME]@[PROJECT_ID].iam.gserviceaccount.com generates a JSON key file with the necessary private information (like your project_id, private key等)这样做。

When making BigQuery API calls, you need to provide such credentials to your application code.在进行 BigQuery API 调用时,您需要向应用程序代码提供此类凭据。 It can be done by setting the environment variable GOOGLE_APPLICATION_CREDENTIALS pointing to the path of the service account JSON file可以通过设置环境变量GOOGLE_APPLICATION_CREDENTIALS指向服务账户 JSON 文件的路径来完成

export GOOGLE_APPLICATION_CREDENTIALS="PATH/TO/SERVICE_ACCOUNT.json"

However, this will work only during your current shell session, so if this one expires or you open a new one you will need to set this variable again.但是,这仅在您当前的 shell session 期间有效,因此如果此变量过期或您打开一个新变量,则需要再次设置此变量。 Another way to authenticate the credentials is to employ the method验证凭据的另一种方法是使用该方法
google.oauth2.Credentials.from_service_account_file inside of your Python script. google.oauth2.Credentials.from_service_account_file在您的 Python 脚本中。

In the following Python code the service account is authenticated with the method google.oauth2.Credentials.from_service_account_file , a new BigQuery table is generated from a CSV file located in Google Cloud Storage and new data is inserted into such table.在以下 Python 代码中,服务帐户使用google.oauth2.Credentials.from_service_account_file方法进行身份验证,从位于 Google Cloud Storage 中的 CSV 文件生成新的 BigQuery 表,并将新数据插入到该表中。

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

# Path to the service account credentials
key_path = "/PATH/TO/SERVICE-ACCOUNT.json"
credentials = service_account.Credentials.from_service_account_file(
    key_path,
    scopes=["https://www.googleapis.com/auth/cloud-platform"],
)

# Instantiation of the BigQuery client
bigquery_client = bigquery.Client()

GCS_URI    = "gs://MY_BUCKET/MY_CSV_FILE"
DATASET_ID = "MY_DATASET"
TABLE_ID   = "MY_TABLE"

def bq_insert_from_gcs(target_uri = GCS_URI, dataset_id = DATASET_ID, table_id = TABLE_ID):
    """This method inserts a CSV file stored in GCS into a BigQuery Table."""

    dataset_ref = bigquery_client.dataset(dataset_id)

    job_config = bigquery.LoadJobConfig()
    # Schema autodetection enabled
    job_config.autodetect = True
    # Skipping first row which correspnds to the field names
    job_config.skip_leading_rows = 1
    # Format of the data in GCS
    job_config.source_format = bigquery.SourceFormat.CSV
    load_job = bigquery_client.load_table_from_uri(target_uri,\
                                                   dataset_ref.table(table_id),\
                                                   job_config=job_config)\

    print('Starting job {}'.format(load_job.job_id))
    print('Loading file {} into the Bigquery table {}'.format(target_uri, table_id))

    load_job.result()
    return 'Job finished.\n'


def bq_insert_to_table(rows_to_insert, dataset_id = DATASET_ID, table_id= TABLE_ID):
    """This method inserts rows into a BigQuery table"""

    # Prepares a reference to the dataset and table
    dataset_ref = bigquery_client.dataset(dataset_id)
    table_ref = dataset_ref.table(table_id)
    # API request to get table call
    table = bigquery_client.get_table(table_ref)

    # API request to insert the rows_to_insert
    print("Inserting rows into BigQuery table {}".format(table_id))
    errors = bigquery_client.insert_rows(table, rows_to_insert)
    assert errors == []


bq_insert_from_gcs()

rows_to_insert = [( u'Alice', u'cat'),\
                  (u'John', u'dog')]
bq_insert_to_table(rows_to_insert)

Also, I would strongly recommend to implement your script using Python 3, since Python 2 will no longer be supported by the google-cloud-bigquery from 01/01/2020.另外,我强烈建议您使用 Python 3 来实现您的脚本,因为从 2020 年 1 月 1 日起, google-cloud-bigquery将不再支持 Python 2。

You will need credentials for a serviceaccount exported to a json.您将需要导出到 json 的服务帐户的凭据。 GCP -> IAM and Admin -> Service Accounts and under the three little dots you will find create key for your account. GCP -> IAM 和 Admin -> 服务帐户,在三个小点下,您将找到您帐户的创建密钥。

As mentioned in previous answers you will also need the BigQuery library如前面的答案所述,您还需要BigQuery 库

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM