【问题标题】:BigQuery PHP Call to undefined method Google_Client::setAssertionCredentials()BigQuery PHP 调用未定义的方法 Google_Client::setAssertionCredentials()
【发布时间】:2017-01-16 07:57:06
【问题描述】:

所以基本上我从第二个答案中尝试了this,但它带有一个错误

致命错误:调用未定义的方法 Google_Client::setAssertionCredentials() 在

这是我的代码

require_once 'xxx/vendor/autoload.php';
public function createGClient() {
    define("CLIENT_ID", "xxx.apps.googleusercontent.com");
    define("SERVICE_ACCOUNT_NAME","xxx@xxx.ccc");
    define("KEY_FILE",'xxx');

    define("PROJECT_ID","xxx");
    define("DATASET_ID","xxx");
    define("TABLE_ID","xxx");

    $this->client = new Google_Client();
    $this->client->setApplicationName("Test");  

    $key = file_get_contents(KEY_FILE);
    $this->client->setAssertionCredentials(
    Google_Auth_AssertionCredentials(SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/bigquery'), $key, "notasecret"));
    $this->client->setClientId(CLIENT_ID);
    $this->service = new Google_Service_Bigquery($this->client);
}

public function runQuery() {
    // To see the a list of tables  
    print_r($this->service->tables->listTables(PROJECT_ID, DATASET_ID));

    // To see details of a table
    print_r($this->service->tables->get(PROJECT_ID, DATASET_ID, TABLE_ID));

    // To query a table
    $jobs = $this->service->jobs;
    $query = new Google_Service_Bigquery_QueryRequest();
    $query->setQuery("SELECT * FROM wherever;");
    $response = $jobs->query(PROJECT_ID, $query);
    print_r($response);
}

我已经安装了指南/文档中的所有内容。谁能帮帮我,因为我什么都试过了,还是不行,非常感谢。

【问题讨论】:

    标签: php api google-bigquery


    【解决方案1】:

    不再使用那个版本的 PHP 库,因为它已经过时并且缺少文档。

    下面有一个较新的链接,它适用于设置服务帐户默认凭据的需要,请参阅putenvuseApplicationDefaultCredentials() 行。这是我使用库 https://github.com/google/google-api-php-client 的工作代码您需要从控制台获取您的服务帐户密钥文件:https://console.cloud.google.com/iam-admin/serviceaccounts/

    composer.json

    {
        "require": {
            "google/cloud": "^0.13.0",
            "google/apiclient": "^2.0"
        }
    }
    

    php 文件

    # Imports the Google Cloud client library
    use Google\Cloud\BigQuery\BigQueryClient;
    use Google\Cloud\ServiceBuilder;
    
    $query="SELECT repository_url, 
           repository_has_downloads 
    FROM   [publicdata:samples.github_timeline]
    LIMIT  10";
    $client = new Google_Client();
    putenv('GOOGLE_APPLICATION_CREDENTIALS='.dirname(__FILE__) . '/.ssh/dummyname-7f0004z148e1.json');//this can be created with other ENV mode server side
    $client->useApplicationDefaultCredentials();
    
    $builder = new ServiceBuilder([
                    'projectId' => 'edited',
            ]);
    
            $bigQuery = $builder->bigQuery();
    
            $job = $bigQuery->runQueryAsJob($query);
            $info=$job->info();
    //      print_r($info);
    //      exit;
            $queryResults = $job->queryResults();
    
            /*$queryResults = $bigQuery->runQuery(
                $query,
                ['useLegacySql' => true]);*/
    
            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');
            }
    
            print_r($result);
    

    【讨论】:

    • “致命错误:未在”中找到类 'Google\Cloud\ServiceBuilder' 我错过了什么吗?
    • 确保两个作曲家库都已正确安装。当然,您需要导入 vendor/autoload.php 才能让作曲家工作。
    • 非常感谢,我重新安装了所有东西,效果很好。
    • default credentials 不应该意味着它使用自动生成的 GAE 和 GCE service accounts' keys?所以不需要生成一个并指定为GOOGLE_APPLICATION_CREDENTIALS中的密钥文件?
    • 您需要使用一个并指定它可以是您面板中的第一个。默认情况下,PHP 库意味着如果作为 ENV 变量加载到服务器上,它将尝试找出它。但通常,如果我们处理多个项目,我们会在 lib 中指定每个项目。
    猜你喜欢
    • 2014-04-17
    • 2012-07-27
    • 2018-04-11
    • 2020-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多