Python Google应用程序引擎-401在请求BigQuery API时

Python Google应用程序引擎-401在请求BigQuery API时,python,google-app-engine,google-bigquery,Python,Google App Engine,Google Bigquery,我看到了一些类似的问题,但没有一个真正给出了这个问题的具体解决方案 我得到的信息是: HttpError 401 when requesting https://www.googleapis.com/bigquery/v2/projects/project/queries?alt=json returned "Invalid Credentials" 连接到BigQuery的代码: appname = get_application_id()

我看到了一些类似的问题,但没有一个真正给出了这个问题的具体解决方案

我得到的信息是:

HttpError 401 when requesting https://www.googleapis.com/bigquery/v2/projects/project/queries?alt=json returned "Invalid Credentials"
连接到BigQuery的代码:

   appname = get_application_id()                                                                                                                                                  
       project_id = appname                                                                                                                                                            
       # Grab the application's default credentials from the environment.                                                                                                              
       credentials = GoogleCredentials.get_application_default()                                                                                                                       
       # Construct the service object for interacting with the BigQuery API.                                                                                                           
       bigquery_service = build('bigquery', 'v2', credentials=credentials)                                                                                                             
       query_request = bigquery_service.jobs()     



       query_data = {                                                                                                                                                                  
              'query': query,                                                                                                                                                             
              'useLegacySql': use_legacy_sql                                                                                                                                              
          }                                                                                                                                                                               

       query_response = query_request.query(                                                                                                                                           
              projectId=project_id,                                                                                                                                                       
              body=query_data).execute()  
此问题的原因可能是什么?

请确保:

  • 您已经创建了一个具有正确IAM角色的服务帐户
  • 导出环境变量
    GOOGLE\u APPLICATION\u CREDENTIALS
    以指向在步骤1中创建的文件
  • 那就行了。可以找到更多细节