我試圖通過 PHP API 客戶端進入 Google 電子表格,但我收到 404->您需要許可。我發現我使用的是舊的關閉表 API 版本 v3,這是什么原因導致此錯誤 404->“您需要許可”!現在我需要將我的電子表格 API 從 v3 遷移到 v4。
這是我的 gdata 代碼,我在其中呼叫 class getspreadsheets(); :
if(!$accessToken){
$auth_url = $this->client->createAuthUrl();
} else {
try{
$this->client->setAccessToken($accessToken);
$token = json_decode($accessToken);
if ($this->client->isAccessTokenExpired()) {
$this->client->refreshToken($token->refresh_token);
$tok = json_encode($this->client->getAccessToken());
$token = json_decode($tok);
$db->setQuery("Update #__breezingforms_addons_gdata set password = " . $db->quote($tok) . " Where form_id = " . intval($form_id));
$db->execute();
}
$serviceRequest = new DefaultServiceRequest($token->access_token, $token->token_type);
ServiceRequestFactory::setInstance($serviceRequest);
$spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
$spreadsheetFeed = $spreadsheetService->getSpreadsheets();
}catch(Exception $ee){
//$accessToken = null;
//$auth_url = $this->client->createAuthUrl();
$error=$ee->getMessage();
}
}
if($spreadsheetFeed !== null){
foreach($spreadsheetFeed As $sheet){
$gdata_spreadsheets[$sheet->getId()] = $sheet->getTitle();
}
}
if($gdata->spreadsheet_id != '' && isset( $gdata_spreadsheets[$gdata->spreadsheet_id] ) && $spreadsheetFeed !== null){
$spreadsheet = $spreadsheetFeed->getByTitle($gdata_spreadsheets[$gdata->spreadsheet_id]);
$worksheetFeed = $spreadsheet->getWorksheets();
foreach ( $worksheetFeed as $sheet ){
$gdata_worksheets[$sheet->getId()] = $sheet->getTitle();
}
if($gdata->worksheet_id != '' && isset( $gdata_worksheets[$gdata->worksheet_id] )){
$worksheet = $worksheetFeed->getByTitle($gdata_worksheets[$gdata->worksheet_id]);
$cellFeed = $worksheet->getCellFeed();
foreach($cellFeed->getEntries() as $cellEntry) {
$row = $cellEntry->getRow();
$col = $cellEntry->getColumn();
if( $row > 1 ){
break;
}
$gdata_columns[] = $cellFeed->getCell($row, $col)->getContent();
}
}
}
} catch(Exception $e){
$error = $e->getMessage();
}
用于獲取電子表格的 Sheets API v3 代碼是:
namespace Google\Spreadsheet;
使用 SimpleXMLElement;
/**
電子表格服務。
@包谷歌
@subpackage 電子表格
@author Asim Liaquat [email protected] / class SpreadsheetService { / *
- 從谷歌驅動器獲取電子表格電子表格串列。
- @return \Google\Spreadsheet\SpreadsheetFeed */ public function getSpreadsheets() { return new SpreadsheetFeed( ServiceRequestFactory::getInstance()->get('feeds/spreadsheets/private/full') ); }
/**
- 如果您決定,根據 id 從谷歌驅動器獲取單個電子表格
- 將 id 存盤在本地。這有助于減少 api 呼叫。
- @param string $id 電子表格的網址
- @return \Google\Spreadsheet\Spreadsheet */ public function getSpreadsheetById($id) { return new Spreadsheet( new SimpleXMLElement( ServiceRequestFactory::getInstance()->get('feeds/spreadsheets/private/full/'. $id) ) ); }
/**
- 回傳指定作業表的串列提要。
- @see \Google\Spreadsheet\Worksheet::getWorksheetId()
- @param 字串 $worksheetId
- @return \Google\Spreadsheet\ListFeed */ public function getListFeed($worksheetId) { return new ListFeed( ServiceRequestFactory::getInstance()->get("feeds/list/{$worksheetId}/od6/private/full") ) ; }
/**
- 回傳指定作業表的單元格提要。
- @see \Google\Spreadsheet\Worksheet::getWorksheetId()
- @param 字串 $worksheetId
- @return \Google\Spreadsheet\CellFeed */ public function getCellFeed($worksheetId) { return new CellFeed( ServiceRequestFactory::getInstance()->get("feeds/cells/{$worksheetId}/od6/private/full") ) ; } }
如何在 v4 上進行遷移?有什么幫助嗎?
uj5u.com熱心網友回復:
A
和
composer require google/apiclient:^2.0
檔案:
- 驅動 API PHP
- 表格 API PHP
- PHP 谷歌 API 客戶端
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/365576.html
標籤:谷歌表格
上一篇:如何使用form2Json決議通過get請求收到的字串?
下一篇:如何將多列中的字串拆分為多行
