2016-10-30 115 views
0

我正在构建从BigQuery获取数据到另一台服务器的应用程序。并尝试使用服务器以服务帐户的生成密钥进行服务器身份验证。 但收到followig错误。谷歌bigquery服务器到服务器认证失败 - python

import json 

from httplib2 import Http 
import argparse 
from oauth2client.client import GoogleCredentials 
from googleapiclient.errors import HttpError 
from oauth2client.service_account import ServiceAccountCredentials 
from apiclient.discovery import build 
from oauth2client.service_account import ServiceAccountCredentials 

scopes = ['https://www.googleapis.com/auth/sqlservice.admin'] 
credentials = ServiceAccountCredentials.from_json_keyfile_name(
    'credentials_bq.json', scopes) 
bigquery_service = build('bigquery', 'v1', credentials=credentials) 


query_request = bigquery_service.jobs() 
query_data = { 
    'query': (
     'select count(distinct meta_id) from [..table.name] ' 
     'where MSEC_TO_TIMESTAMP(meta_serverTs) >= \'2016-10-28 10:00:00 UTC\'') 
} 



query_response = query_request.query(
    projectId='my_project_id', 
    body=query_data).execute() 

返回错误:

Traceback (most recent call last): 
    File "test_connection_bq.py", line 35, in <module> 
    body=query_data).execute() 
    File "/usr/local/lib/python2.7/dist-packages/oauth2client/_helpers.py", line 133, in positional_wrapper 
    return wrapped(*args, **kwargs) 
    File "/usr/local/lib/python2.7/dist-packages/googleapiclient/http.py", line 838, in execute 
    raise HttpError(resp, content, uri=self.uri) 
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/bigquery/v2/projects/my_project_id/queries?alt=json returned "Insufficient Permission"> 
+0

我使用v2不是v1抱歉。 bigquery_service = build('bigquery','v2',credentials = credentials) – colly434

回答

1

不知道如果你看过access control document?如果不是,请通过它并授予服务帐户适当的权限。由于您正在运行查询,因此应该授予bigquery.user。