Google App Script UnauthorizedException:您需要获得许可

时间:2017-10-28 20:49:55

标签: php heroku google-sheets google-drive-api google-spreadsheet-api

我正在关注示例here。它运作良好。但是在客户端,我试图在将数据发布到Google工作表之后立即从我的PHP服务器获得响应。如果我不删除下面的代码块,它运行良好,我得到print_r作为返回的数据(响应)。如果我删除下面的代码块,我会在Chrome DevTools上获得500(内部服务器错误)。我不知道是什么问题。谢谢你的帮助!

$listFeed = $worksheet->getListFeed();
foreach ($listFeed->getEntries() as $entry) {
           $representative = $entry->getValues(); 
           print_r($representative);  
} 

当我打印heroku日志时,我收到错误:

 2017-10-28T20:20:48.585812+00:00 app[web.1]: [2017-10-28 20:20:48] name.ERROR: Bar [] []
    2017-10-28T20:20:49.648709+00:00 heroku[router]: at=info method=POST path="/controller.php" host=a.herokuapp.com request_id=c08b2d5
    f-8473-44c0-8780-529d29a1fb40 fwd="88.231.130.210" dyno=web.1 connect=0ms service=1092ms status=500 bytes=227 protocol=https
    2017-10-28T20:20:49.639249+00:00 app[web.1]: [28-Oct-2017 20:20:49 UTC] PHP Fatal error:  Uncaught Google\Spreadsheet\Exception\UnauthorizedExcepti
    on: You need permission in /app/vendor/asimlqt/php-google-spreadsheet-client/src/Google/Spreadsheet/DefaultServiceRequest.php:356 
    2017-10-28T20:20:49.639276+00:00 app[web.1]: Stack trace:
    2017-10-28T20:20:49.639337+00:00 app[web.1]: #0 /app/vendor/asimlqt/php-google-spreadsheet-client/src/Google/Spreadsheet/DefaultServiceRequest.php(
    261): Google\Spreadsheet\DefaultServiceRequest->execute(Resource id #89)
    2017-10-28T20:20:49.639438+00:00 app[web.1]: #1 /app/vendor/asimlqt/php-google-spreadsheet-client/src/Google/Spreadsheet/ListFeed.php(96): Google\S
    preadsheet\DefaultServiceRequest->post('https://spreads...', '<?xml version="...')
    2017-10-28T20:20:49.639480+00:00 app[web.1]: #2 /app/controller.php(73): Google\Spreadsheet\ListFeed->insert(Array)
    2017-10-28T20:20:49.639482+00:00 app[web.1]: #3 {main}
    2017-10-28T20:20:49.639556+00:00 app[web.1]:   thrown in /app/vendor/asimlqt/php-google-spreadsheet-client/src/Google/Spreadsheet/DefaultServiceReq
    uest.php on line 356
    2017-10-28T20:20:49.645305+00:00 app[web.1]: 10.65.88.66 - - [28/Oct/2017:20:20:48 +0000] "POST /controller.php HTTP/1.1" 500 7 "https://a.git
    hub.io/test-form" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36

完整代码:

<?php
header("Access-Control-Allow-Origin: *");
require __DIR__ . '/vendor/autoload.php';


use Google\Spreadsheet\DefaultServiceRequest;
use Google\Spreadsheet\ServiceRequestFactory;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$log = new Logger('name');
$log->pushHandler(new StreamHandler('php://stdout', Logger::WARNING));

$log->addWarning('Foo');
$log->addError('Bar');

putenv('GOOGLE_APPLICATION_CREDENTIALS=' . __DIR__ . '/client_secret.json');
$client = new Google_Client;
$client->useApplicationDefaultCredentials();

$client->setApplicationName("Something to do with my representatives");
$client->setScopes(['https://www.googleapis.com/auth/drive','https://spreadsheets.google.com/feeds']);

if ($client->isAccessTokenExpired()) {
    $client->refreshTokenWithAssertion();
}

$accessToken = $client->fetchAccessTokenWithAssertion()["access_token"];
ServiceRequestFactory::setInstance(
    new DefaultServiceRequest($accessToken)
);


// Get our spreadsheet
$spreadsheet = (new Google\Spreadsheet\SpreadsheetService)
->getSpreadsheetFeed()
->getByTitle('From PHP To Sheet');


// Get the first worksheet (tab)
$worksheets = $spreadsheet->getWorksheetFeed()->getEntries();
$worksheet = $worksheets[0];

//When I remove below, and echo instead, doesnt work. 
foreach ($listFeed->getEntries() as $entry) {
               $representative = $entry->getValues(); 
               print_r($representative);  
    } 

// Check if the form is submitted and get the form data
if ( isset( $_POST['submit'] ) ) {  
    $name = $_REQUEST['name'];
    $email = $_REQUEST['email'];
    $subject = $_REQUEST['subject'];
    $message = $_REQUEST['message'];
}

// Get the listFeed to insert form data       
        $listFeed = $worksheet->getListFeed();
        $listFeed->insert([
          'name' =>  $name,
          'email' => $email,
          'subject' => $subject,
          'message' => $message,
        ]);       
?>

1 个答案:

答案 0 :(得分:0)

您需要与服务帐户共享文档:enter image description here