Unable to save a query as a view table

喜欢而已 提交于 2020-01-15 03:33:12

问题


I have a query that runs and can see the results. But while trying to save the query as a view table, I get error message saying

Failed to save view. No suitable credentials found to access Google Drive. Contact the table owner for assistance.

I think the problem is caused by a table used in the query. The table is uploaded from a google sheet (with source URI), own by me. I have tried to enable Google Drive API from the project but no luck. Not sure how I can give BigQuery access to Google Drive.


回答1:


I suspect the problem you are hitting is one of OAuth Scopes. In order to talk to the Google Drive API to read data, you need to use credentials that were granted access to that API.

If you are using the BigQuery web UI and have not explicitly granted access to Drive, it won't work. For example, the first time I tried to "Save to Google Sheets", the BigQuery UI popped up an OAuth prompt asking me to grant access to my Google Drive. After this it could save the results. Try doing this to make sure your credentials have the Drive scope and then "Save View" again.

If you are using your own code to do this, you should request scope 'https://www.googleapis.com/auth/drive' in addition to the 'https://www.googleapis.com/auth/bigquery' scope you are already using to talk to BigQuery.

If you are using the bq client, it has been updated to request this scope, but you may need to re-initialize your authentication credentials. You can do this with bq init --delete_credentials to remove the credentials, then your next action we re-request credentials.




回答2:


Using Google App Script this worked for me:

function saveQueryToTable() {
  var projectId = '...yourprojectid goes here...';
  var datasetId = '...yourdatesetid goes here...';
  var sourceTable = '...your table or view goes here...';
  var destTable = '...destination table goes here...';
var myQuery;
  
  //just a random call to activate the Drive API scope
  var test = Drive.Properties.list('...drive file id goes here...')
  
  //list all tables for the particular dataset
  var tableList = BigQuery.Tables.list(projectId, datasetId).getTables();
  
  //if the table exist, delete it
  for (var i = 0; i < tableList.length; i++) {
    if (tableList[i].tableReference.tableId == destTable) { 
      BigQuery.Tables.remove(projectId, datasetId, destTable);
      Logger.log("DELETED: " + destTable);
    }
  };
 
 myQuery =  'SELECT * FROM [PROJECTID:DATASETID.TABLEID];'
 .replace('PROJECTID',projectId)
 .replace('DATASETID',datasetId)
 .replace('TABLEID',sourceTable)
  
 var job = {
    configuration: {
      query: {
        query: myQuery,
        destinationTable: {
          projectId: projectId,
          datasetId: datasetId,
          tableId: destTable
        }
      }
    }
  };

  var queryResults = BigQuery.Jobs.insert(job, projectId);
  Logger.log(queryResults.status);
}

The 'trick' was a random call to the Drive API to ensure both the BigQuery and Drive scopes are included.

Google Apps Script Project Properties



来源:https://stackoverflow.com/questions/36619324/unable-to-save-a-query-as-a-view-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!