1

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.

Community
  • 1
  • 1
xharmas
  • 43
  • 2
  • 8
  • can you share the authentication method you are using? the problem is a missing OAuth2 scope for Google Drive, but I'd like to answer in context – Felipe Hoffa Jan 18 '17 at 02:40
  • Sorry, how do I do this "missing OAuth2 scope for Google Drive" in PHP? I might miss this, sorry and thanks – xharmas Jan 20 '17 at 04:46
  • "can you share the authentication method you are using?" -> how are you running authentication right now? – Felipe Hoffa Jan 20 '17 at 08:12
  • What I did right now basically just following the answer of http://stackoverflow.com/questions/40731823/encountered-an-error-while-globbing-file-pattern-error-when-using-bigquery-api question @FelipeHoffa. And how do I add scope is $client->setScopes(array('https:/...... Am I wrong or missing anything? Really thanks for the help – xharmas Jan 23 '17 at 05:04
  • @FelipeHoffa And also, the code is almost the same as the answer of this question http://stackoverflow.com/questions/41671769/bigquery-php-call-to-undefined-method-google-clientsetassertioncredentials – xharmas Jan 23 '17 at 05:09
  • Could you add the exact code you're using to the question and the error you're seeing? This is improve the question's clarity by avoiding piecing it together from several sources and further help the community diagnose exactly what's happening so we can provide and more appropriate/specific answer. – Nicholas Jan 26 '17 at 14:42
  • @Nicholas http://stackoverflow.com/questions/41843956/accessdenied-when-querying-google-drive-sheet-through-bigquery-api-php?noredirect=1#comment70880635_41843956 I already add the scopes but it still says Access Denied – xharmas Jan 27 '17 at 05:25
  • Please add the exact code your application is using to the question so we can attempt to reproduce this for ourselves and better identify where and what access is denied. – Nicholas Jan 27 '17 at 17:56
  • @Nicholas Hello, sorry.. I added the exact code, could you please take a look at this and help? Thanks – xharmas Jan 31 '17 at 04:00
  • Possible duplicate of ["Encountered an error while globbing file pattern" error when using BigQuery API w/ Google Sheets](http://stackoverflow.com/questions/40731823/encountered-an-error-while-globbing-file-pattern-error-when-using-bigquery-api) – Adam Feb 26 '17 at 19:35
  • 2
    You missed a step, but there's not enough information for the community to divine which step you missed. Opening multiple duplicate questions with the same information isn't going to help. My advice is to re-check [Graham Polley's answer](http://stackoverflow.com/a/40731962/3953357) from the related question, and start over from the beginning if needed, tracing each step in the documentation carefully. – Adam Feb 26 '17 at 19:38

0 Answers0