我想使用Tensorflow的BigQuery阅读器访问GCP中其他人项目的数据。我做错了什么?
为此,我使用Flask应用程序,用户可以以OAuth2Credentials的形式提供权限。在BigQuery API上测试时,此权限可以正常工作。当使用BigQuery阅读器获取我自己的数据时,它也可以正常工作。但是,当初始化BigQery读取器以获取其他人的数据时,我会收到拒绝权限错误。
这是获取权限的代码
from oauth2client import client
app = flask.Flask(__name__)
from apiclient import discovery
@app.route('/')
@app.route('/index')
def index():
global service
if 'credentials' not in flask.session:
return flask.redirect(flask.url_for('oauth2callback'))
credentials = client.OAuth2Credentials.from_json(flask.session['credentials'])
if credentials.access_token_expired:
return flask.redirect(flask.url_for('oauth2callback'))
else:
http_auth = credentials.authorize(httplib2.Http())
service = discovery.build('bigquery', 'v2', http_auth)
return 'ok'
@app.route('/oauth2callback')
def oauth2callback():
flow = client.flow_from_clientsecrets(
'client_secret.json',
scope='https://www.googleapis.com/auth/bigquery '
+ 'https://www.googleapis.com/auth/bigquery.insertdata '
+ 'https://www.googleapis.com/auth/cloud-platform',
redirect_uri=flask.url_for('oauth2callback', _external=True))
if 'code' not in flask.request.args:
auth_uri = flow.step1_get_authorize_url()
return flask.redirect(auth_uri)
else:
auth_code = flask.request.args.get('code')
credentials = flow.step2_exchange(auth_code)
flask.session['credentials'] = credentials.to_json()
return flask.redirect(flask.url_for('index'))
如果我运行代码来访问bigquery API,这可以正常工作,所以我不希望权限有任何问题:
response = service.projects().list().execute()
但是在使用此代码创建BigQuery阅读器时:
from tensorflow.contrib.cloud.python.ops.bigquery_reader_ops import BigQueryReader
import tensorflow as tf
reader = BigQueryReader(project_id=project_id,
dataset_id=dataset_id,
table_id=table_id,
timestamp_millis=millis,
num_partitions=10,
features=features_dict)
queue = tf.train.string_input_producer(reader.partitions())
我收到了拒绝权限错误:
Permission denied: Error executing an HTTP request (HTTP response code 403, error code 0, error message '')
when reading schema for project_id:dataset_id.table_id
答案 0 :(得分:0)
TF BigQuery连接器的身份验证没有很好的记录,我似乎无法找到相关的代码片段,但我的猜测是它使用application default credentials。
这与原始BigQuery调用不同,后者在构建service
时显式传递它们。
您可以尝试执行credentials.to_json()
时将结果转储到文件中,并将GOOGLE_APPLICATION_CREDENTIALS
环境变量指向它。不确定是否会修复它,但它可能值得一试。