【问题标题】:I want to query the Google Big Query Dataset from PHP script我想从 PHP 脚本中查询 Google Big Query Dataset
【发布时间】:2014-03-11 07:30:44
【问题描述】:

我已经下载了用于访问 google api 的 google-api-php-client。 我的代码是 include_once "templates/base.php"; echo pageHeader("Simple API Access"); set_include_path("../src/" . PATH_SEPARATOR . get_include_path()); require_once 'Google/Client.php'; require_once 'Google/Service/Bigquery.php'; $client = new Google_Client(); $client->setApplicationName("my application name"); $apiKey = "--------my api key----"; $client->setDeveloperKey($apiKey); define("PROJECT_ID", my project ID); define("DATASET_ID", "my data set ID"); $service = new Google_Service_Bigquery($client); $results = $service->tables->listTables(PROJECT_ID, DATASET_ID); print_r($results); 如果我运行上面的文件,它会给出错误:致命错误:未捕获异常 'Google_Service_Exception' 并在 C:\xampp\htdocs\test\google-api- 中显示消息 'Error calling GET https://www.googleapis.com/bigquery/v2/projects/projectID/datasets/datasetname/tables?key=API key: (401) Login Required' php-client-master (2)\google-api-php-client-master\src\Google\Http\REST.php:80 堆栈跟踪:#0 C:\xampp\htdocs\test\google-api-php-客户端主 (2)\google-api-php-client-master\src\Google\Http\REST.php(44): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request)) #1 C:\xampp\htdocs\test \google-api-php-client-master (2)\google-api-php-client-master\src\Google\Client.php(499): Google_Http_REST::execute(Object(Google_Client), Object(Google_Http_Request)) #2 C:\xampp\htdocs\test\google-api-php-client-master (2)\google-api-php-client-master\src\Google\Service\Resource.php(195): Google_Client->执行(对象(Google_Http_Request))#3 C:\xampp\htdocs\test\google-api-php-client-master (2)\google-api-php-client-master\src\Google\Service\B in C :\xampp\htdocs\test\google-api-php-cl ient-master (2)\google-api-php-client-master\src\Google\Http\REST.php 在第 80 行

【问题讨论】:

  • 我在安装了 XAMPP 服务器的 localhost 上运行文件,上面的错误提示 Login Required 这是什么意思?
  • 这意味着您忘记使用 OAUTH2.0 对您的应用程序进行身份验证,请参阅链接示例
  • 我通过设置所需参数对我的应用程序进行了身份验证,但出现错误:致命错误:未捕获异常 'Google_Auth_Exception' 并带有消息'刷新 OAuth2 令牌时出错,消息:'{“error”:“invalid_grant” }'' 在 OAuth2.php:327 , OAuth2.php(289): Google_Auth_OAuth2->refreshTokenRequest(Array)
  • 感谢帮助 Oath2.0 问题现已解决

标签: google-bigquery


【解决方案1】:

您需要使用 OAUTH2.0 对您的应用进行身份验证

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

if (isset($_SESSION['service_token'])) {
    $client->setAccessToken($_SESSION['service_token']);
}
$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);
}
$_SESSION['service_token'] = $client->getAccessToken();
var_dump($_SESSION);
$bq = new Google_Service_Bigquery($client);

【讨论】:

  • 通过尝试这个我得到了错误 - 致命错误:未捕获的异常 'Google_Auth_Exception' 带有消息'刷新 OAuth2 令牌时出错,消息:'{ "error" : "invalid_grant" }'' in OAuth2 .php:327 , OAuth2.php(289): Google_Auth_OAuth2->refreshTokenRequest(Array)
  • 面临同样的问题致命错误:未捕获的异常 'Google_Auth_Exception' 带有消息'刷新 OAuth2 令牌时出错,消息:'{"error" : "invalid_grant" }'' 在 OAuth2.php:327 中请帮助任何人
  • 如果您仍然必须使用 OAuth,那么开发人员密钥的意义何在?
猜你喜欢
  • 2021-04-16
  • 2014-01-15
  • 2012-10-21
  • 2021-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-15
相关资源
最近更新 更多