Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Google计算引擎上验证BigQuery?_Python_Bash_Curl_Google Bigquery_Google Compute Engine - Fatal编程技术网

Python 如何在Google计算引擎上验证BigQuery?

Python 如何在Google计算引擎上验证BigQuery?,python,bash,curl,google-bigquery,google-compute-engine,Python,Bash,Curl,Google Bigquery,Google Compute Engine,在Google计算引擎实例上进行Google BigQuery身份验证的最简单方法是什么?在Python中: AppAssertionCredentials是一个python类,它允许计算引擎实例向Google和其他OAuth 2.0服务器标识自己,而我们需要一个流 项目id可以从元数据服务器读取,因此不需要将其设置为变量 以下代码使用AppAssertionCredentials和元数据服务器中的项目id获取令牌,并使用此数据实例化BigqueryClient: import bigque

在Google计算引擎实例上进行Google BigQuery身份验证的最简单方法是什么?

在Python中:

AppAssertionCredentials
是一个python类,它允许计算引擎实例向Google和其他OAuth 2.0服务器标识自己,而我们需要一个流

项目id可以从元数据服务器读取,因此不需要将其设置为变量

以下代码使用AppAssertionCredentials和元数据服务器中的项目id获取令牌,并使用此数据实例化BigqueryClient:

import bigquery_client
import urllib2
from oauth2client import gce

def GetMetadata(path):
  return urllib2.urlopen(
      'http://metadata/computeMetadata/v1/%s' % path,
      headers={'Metadata-Flavor': 'Google'}
      ).read()

credentials = gce.AppAssertionCredentials(
    scope='https://www.googleapis.com/auth/bigquery')

client = bigquery_client.BigqueryClient(
    credentials=credentials,
    api='https://www.googleapis.com',
    api_version='v2',
    project_id=GetMetadata('project/project-id'))
要使其工作,您需要在创建时为GCE实例提供对BigQuery API的访问权限:

gcloud compute instances create <your_instance_name> --scopes storage-ro bigquery 
gcloud计算实例创建——范围存储查询

首先确保您的实例具有访问BigQuery的范围-您只能在创建时决定这一点

在bash脚本中,通过调用以下命令获取oauth令牌:

ACCESSTOKEN=`curl -s "http://metadata/computeMetadata/v1/instance/service-accounts/default/token" -H "X-Google-Metadata-Request: True" | jq ".access_token" | sed 's/"//g'`
echo "retrieved access token $ACCESSTOKEN"
现在,假设您想要项目中的数据集列表:

CURL_URL="https://www.googleapis.com/bigquery/v2/projects/YOURPROJECTID/datasets"
CURL_OPTIONS="-s --header 'Content-Type: application/json' --header 'Authorization: OAuth $ACCESSTOKEN' --header 'x-goog-project-id:YOURPROJECTID' --header 'x-goog-api-version:1'"
CURL_COMMAND="curl --request GET $CURL_URL $CURL_OPTIONS"    
CURL_RESPONSE=`eval $CURL_COMMAND`
JSON格式的响应可以在变量CURL\u response中找到


PS:我现在意识到这个问题被标记为Python,但同样的原则也适用。

谢谢你的回答,我也添加了bash和curl标记以确保适当性:)