dousa1630 2014-02-28 11:37
浏览 82
已采纳

无法让google-api工作

I want to manage my Fusion Tables from php cron. I can't get it to work:

<?php
//include_once "templates/base.php";
error_reporting(E_ALL-E_WARNING-E_NOTICE-E_DEPRECATED);
ini_set('display_errors',1);
session_start();

set_include_path("src/" . PATH_SEPARATOR . get_include_path());

require_once 'Google/Client.php';
require_once 'Google/Service/Fusiontables.php';
$client_id = 'given';
$service_account_name = 'given';
$key_file_location = 'given';
$tableid='given';

$client = new Google_Client();
$client->addScope("https://www.googleapis.com/auth/fusiontables");
$service=new Google_Service_Fusiontables($client);

unset($_SESSION['service_token']);
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/fusiontables'),
    $key
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
  $client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();

$result=$service->table->listTable();

$insert_statement = "SELECT tytul FROM $tableid ";
$result = $service->query->sql($insert_statement);
?>

Here are results:

object(Google_Service_Fusiontables_TableList)#14 (7) {
  ["itemsType":protected]=>
  string(33) "Google_Service_Fusiontables_Table"
  ["itemsDataType":protected]=>
  string(5) "array"
  ["kind"]=>
  string(22) "fusiontables#tableList"
  ["nextPageToken"]=>
  NULL
  ["collection_key":protected]=>
  string(5) "items"
  ["data":protected]=>
  array(0) {
  }
  ["processed":protected]=>
  array(0) {
  }
}

Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling POST https://www.googleapis.com/fusiontables/v1/query: (403) Forbidden' in /funkcje/google-api/src/Google/Http/REST.php:80 Stack trace: #0 /funkcje/google-api/src/Google/Http/REST.php(44): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request)) #1 /funkcje/google-api/src/Google/Client.php(499): Google_Http_REST::execute(Object(Google_Client), Object(Google_Http_Request)) #2 /funkcje/google-api/src/Google/Service/Resource.php(195): Google_Client->execute(Object(Google_Http_Request)) #3 /funkcje/google-api/src/Google/Service/Fusiontables.php(682): Google_Service_Resource->call('sql', Array, 'Google_Service_...') #4 /funkcje/google-api/fusion-tables.php(79): Google_Service_Fusiontables_Query_Resource->sql('SELECT tytul FR...') #5 {main} thrown in /funkcje/google-api/src/Google/Http/REST.php on line 80

As You can see I'm connected with API (first result) but it gives back no tables (in dev console it return two tables) - as if I was connected to different account...

I'm connecting as Service Account. If I connect as Client ID for web application all works.

  • 写回答

1条回答 默认 最新

  • drbuowqe02101 2014-02-28 14:19
    关注

    Found anwser: https://groups.google.com/d/msg/google-api-php-client/9d2lQAppTvg/o5QGhsXiAQIJ

    You have to add new user to document - user from google api service

    • Select the document (FT) in Google Docs
    • Click "Share"
    • Select "Add People"
    • Enter your service account email address, e.g. myservice...@developer.gserviceaccount.com
    • Ensure "Can Edit" is selected.
    • Un-check "notify people via email"
    • Click "Share and Save"
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度