我有错误说许可被拒绝。我已尝试this(提供对Google凭据的查看权限)和this。一切都行不通。我已经为google驱动器启用了API,并且还执行了此操作:
gcloud components update
gcloud auth login --enable-gdrive-access
我正在使用PHP。
这是代码
<?php
ini_set("memory_limit", "2048M");
ini_set('max_execution_time', 7200); //3600 seconds = 60 minutes
if (!defined('BASEPATH'))
exit('No direct script access allowed');
use Google\Cloud\BigQuery\BigQueryClient;
use Google\Cloud\ServiceBuilder;
use Google\Cloud\ExponentialBackoff;
require 'assets/SDK/vendor/autoload.php';
//require 'assets/library/vendor/autoload.php';
require_once 'assets/library/google-api-php-client/vendor/autoload.php';
define('APPLICATION_NAME', 'asd');
define('CLIENT_SECRET_PATH', 'assets/library/auth/asd.json');
define('PROJECT_ID','qwe');
define('SCOPES', implode(' ', array(
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/drive.metadata',
'https://www.googleapis.com/auth/drive.readonly')
));
public function index(){
$myFile = "assets/library/auth/session_bq2.txt";
if(file_exists($myFile)){
@$fh = fopen($myFile, 'r');
// echo 'filesize '.filesize($myFile);
$session = fread($fh, filesize($myFile));
fclose($fh);
//echo '--refreshToken index '.$session;
}
$client = new Google_Client();
$client->setAccessType('offline');
$client->setClientId('asd.apps.googleusercontent.com');
$client->setClientSecret('asds');
$client->setState('asd');
$client->setApplicationName(APPLICATION_NAME);
$client->setScopes(SCOPES);
if (isset($session) && $session) {
//Set the new access token after authentication
$client->setAccessToken($session);
//json decode the session token and save it in a variable as object
$sessionToken = json_decode($session);
//Save the refresh token (object->refresh_token) into a cookie called 'token' and make last for 1 month
setcookie('access_token', $sessionToken->refresh_token , time()+2678400);
$cookie = isset($_COOKIE['access_token']) ? $_COOKIE['access_token'] : "";
if(!empty($cookie)){
$client->refreshToken($cookie);
}
$client->setAccessToken($session);
}
else{
$tes = $client->getRefreshToken();
$client = new Google_Client();
$client->setAccessType('offline');
$client->setClientId('asd.apps.googleusercontent.com');
$client->setRedirectUri('qwe');
$client->setClientSecret('asd');
$client->setState('asd');
$client->setApplicationName(APPLICATION_NAME);
$client->setScopes(SCOPES);
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$myfile = fopen("assets/library/auth/session_bq2.txt", "w") or die("Unable to open file!");
$txt = json_encode($client->getAccessToken());
fwrite($myfile, $txt);
fclose($myfile);
header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
}
else{
$oauth2_client_id = 'asd.apps.googleusercontent.com';
// $oauth2_redirect = 'asd';
$oauth2_redirect = 'http://' . $_SERVER['HTTP_HOST'] . 'asd';
$oauth2_server_url = 'https://accounts.google.com/o/oauth2/auth';
$query_params = array(
'response_type' => 'code',
'access_type' => 'offline',
'client_id' => $oauth2_client_id,
'scope' => SCOPES,
'redirect_uri' => $oauth2_redirect,
'state' => 'asd',
'approval_prompt' => 'force'
);
$forward_url = $oauth2_server_url . '?' . http_build_query($query_params);
header('Location: ' . $forward_url);
}
}
$service = new Google_Service_Bigquery($client);
$query="SELECT * FROM [queryspreadsheet] LIMIT 10";
putenv('GOOGLE_APPLICATION_CREDENTIALS='.dirname(__FILE__) . '/assets/library/auth/MyProject-f0b4007b2f57.json');//this can be created with other ENV mode server side
$client->useApplicationDefaultCredentials();
$builder = new ServiceBuilder([
'projectId' => 'asd',
]);
$bigQuery = $builder->bigQuery();
$job = $bigQuery->runQueryAsJob($query);
$info=$job->info();
$queryResults = $job->queryResults();
/*$queryResults = $bigQuery->runQuery(
$query,
['useLegacySql' => true]);*/
// var_dump($queryResults);die;
if ($queryResults->isComplete())
{
$i = 0;
$rows = $queryResults->rows();
foreach ($rows as $row)
{
$i++;
$result[$i] = $row;
}
}
else
{
throw new Exception('The query failed to complete');
}
$i = 0;
$i++;
var_dump($result);die;
return $result;
}
有谁知道我错过了什么?感谢。