douduan3203 2017-01-17 05:12
浏览 78

BigQuery:在文件模式下使用时权限被拒绝

I have the error saying that permission denied. I already tried this (giving view access to the google credential), and this. And everything doesnt work. I already enable the API for the google drive and also did this :

gcloud components update
gcloud auth login --enable-gdrive-access

I'm using PHP.

Here's the Code

<?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;
}

Does anyone have any idea what did I miss? Thanks.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 乘性高斯噪声在深度学习网络中的应用
    • ¥15 运筹学排序问题中的在线排序
    • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
    • ¥30 求一段fortran代码用IVF编译运行的结果
    • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥30 python代码,帮调试,帮帮忙吧