Bigquery API php授权 [英] Bigquery API php authorization

查看:152
本文介绍了Bigquery API php授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用PHP库访问bigquery api,但我总是遇到这样的错误:
$ b

I'm trying to get access bigquery api using PHP library, but I always have this error:


致命错误:未捕获的异常'Google_Service_Exception'消息'Error calling GET https: //www.googleapis.com/bigquery/v2/projects/primeval-shadow-571/datasets/Test/tables :(401)需要登录'in ...

Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling GET https://www.googleapis.com/bigquery/v2/projects/primeval-shadow-571/datasets/Test/tables: (401) Login Required' in ...

我有一个web应用程序,需要从bigquery中的表中获取一些数据。远程帐户不是我的,但它是由一些人创建的(我没有登录并传递登录),谁给了我< .json 文件包括thoose参数:

I have a web application, that needs to get some data from table in bigquery. The Remote account is not mine, but it was created by some guy (and I don't have login and pass to log in), who gave me the .json file which included thoose parameters:

auth_uri, 
client_secret, 
token_uri, 
client_email, 
client_x509_cert_url, 
client_id and 
auth_provider_x509_cert_url.

这是我的php代码:

Here is my php code:

<?php

require_once 'Google/Client.php';
require_once 'Google/Service/Bigquery.php';

$client = new Google_Client();
$client->setAuthConfigFile('google-config.json');

$service = new Google_Service_Bigquery($client);

$service->tables->listTables('primeval-shadow-571', 'Test');

但是最后我得到了上面的错误。

But finally I got error that's above.

有人可以告诉我我的错在哪?

Can anybody tell me where am I wrong?

PS两天前我刚刚开始使用google api,所以我刚开始学习它。

P.S. I just started work with google api two days ago, so I'm just beginning learn it.

非常感谢。

推荐答案

为我们工作:

session_start();

define('PROJECT_ID', 'edited');
define('DATASET_ID', 'edited');
define('API_KEY', 'edited');

$client_id = 'edited';
$service_account_name = 'edited';
$key_file_location = '.ssh/privatekey-bigquery.p12';
$service_token_file_location = 'bigquery_current_service_token.json';

set_include_path("google-api-php/src/" . PATH_SEPARATOR . get_include_path());
require_once 'google-api-php/src/Google/Client.php';
require_once 'google-api-php/src/Google/Service/Bigquery.php';

$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
//$client->setDeveloperKey(API_KEY);

if (!is_file($service_token_file_location)) {
    if (!is_writable($service_token_file_location)) {
        @chmod($service_token_file_location, 0777);
        if (!is_writable($service_token_file_location)) {
            die('Service token file is not writable: ' . $service_token_file_location);
        }
    }
    file_put_contents($service_token_file_location, '');
} else {
    if (!is_writable($service_token_file_location)) {
        @chmod($service_token_file_location, 0777);
        if (!is_writable($service_token_file_location)) {
            die('Service token file is not writable: ' . $service_token_file_location);
        }
    }
}
$service_token = @file_get_contents($service_token_file_location);
if (!empty($service_token)) {
    $client->setAccessToken($service_token);
}
if (!file_exists($key_file_location)) {
    die('Key file is missing: ' . $key_file_location);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
        $service_account_name, array(
    'https://www.googleapis.com/auth/bigquery',
        ), $key
);
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
    $client->getAuth()->refreshTokenWithAssertion($cred);
}
$service_token = $client->getAccessToken();
file_put_contents($service_token_file_location, $service_token);

// start using $client

这篇关于Bigquery API php授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆