[英]Google Drive Download API IN php
從 Google Drive API 儀表板下載client_secret.json
。 一旦你把它存儲在你可以訪問的地方。 然后使用composer從谷歌官方github存儲庫為谷歌驅動器Api安裝谷歌客戶端PHP庫。 它仍處於測試階段,但有效。 我自己測試過。
在composer.json
有以下代碼片段,然后運行composer update
。
"require": {
"php": ">=7",
"google/apiclient": "^2.0"
}
在供應商目錄中擁有 google/apiclient 后,將其包含在您的工作流程中。
下面是我最近在我的一個項目中使用的示例代碼片段。 您可以調整它以滿足您的需要。 我從 google 官方 github repo 借用了這段代碼片段。 Google API PHP 客戶端示例
<?php
session_start();
error_reporting(E_ALL);
require_once __DIR__ . '/vendor/autoload.php';
$credentialPath = 'creds/accesstoken.json';
$authCode = $_GET['code'] ?: null ;
$client = new Google_Client();
$client->setApplicationName('Drive API PHP Quickstart');
$client->setScopes(implode(' ', [Google_Service_Drive::DRIVE, Google_Service_Drive::DRIVE_FILE]));
$client->setAuthConfig(__DIR__ . '/client_secret.json');
$client->setAccessType('offline');
if (is_null($authCode)) {
$authUrl = $client->createAuthUrl();
header('Location: ' . $authUrl);
exit;
} else {
if (file_exists($credentialPath)) {
$accessToken = json_decode(file_get_contents($credentialPath), true);
} else {
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$_SESSION['access_token'] = json_encode($accessToken);
file_put_contents($credentialPath, json_encode($accessToken));
print 'Token stored successfully';
}
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
print $client->getRefreshToken();
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
$accessToken = $client->getAccessToken();
file_put_contents($credentialPath, json_encode($accessToken));
$client->setAccessToken($accessToken);
print_r($accessToken);
}
}
嘗試研究給出的代碼,一旦您了解了該腳本的生命周期,您就可以輕松地將Google Drive API
集成到任何框架、腳本中。 您不必依賴第三方服務。 雖然有時這是想做的事情。 讓我們知道此腳本是否適合您。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.