Oauth 2.0 通过令牌(或用户id)检查或获取用户对图纸的访问,以导出图纸内容。谷歌api php客户端库

Oauth 2.0 通过令牌(或用户id)检查或获取用户对图纸的访问,以导出图纸内容。谷歌api php客户端库,oauth-2.0,export-to-csv,google-spreadsheet-api,google-api-php-client,Oauth 2.0,Export To Csv,Google Spreadsheet Api,Google Api Php Client,嗨,很抱歉我的英语不好。我添加到网站与谷歌api php客户端 谢谢卡尔的帮助 ,如何使用google api php客户端访问工作表。 我的代码: index.php: <?php define('ROOT',$_SERVER['DOCUMENT_ROOT']); require_once ROOT.'/lib/google-api-php-client/vendor/autoload.php'; include_once ROOT.'/lib/google-api-php-client

嗨,很抱歉我的英语不好。我添加到网站与谷歌api php客户端 谢谢卡尔的帮助 ,如何使用google api php客户端访问工作表。 我的代码: index.php:

<?php
define('ROOT',$_SERVER['DOCUMENT_ROOT']);
require_once ROOT.'/lib/google-api-php-client/vendor/autoload.php';
include_once ROOT.'/lib/google-api-php-client/examples/templates/base.php';

$client = new Google_Client();

putenv("GOOGLE_APPLICATION_CREDENTIALS=service-account-credentials.json");
if ($credentials_file = checkServiceAccountCredentialsFile()) {
    // set the location manually
    $client->setAuthConfig($credentials_file);
} elseif (getenv('GOOGLE_APPLICATION_CREDENTIALS')) {
    // use the application default credentials
    $client->useApplicationDefaultCredentials();
} else {
    echo missingServiceAccountDetailsWarning();
    exit;
}
$client->setApplicationName(APPLICATION_NAME);
$client->setClientId(CLIENT_ID);
$client->setClientSecret(CLIENT_SECRET);
$client->setAuthConfigFile('client_secrets.json');
$client->setScopes(['https://www.googleapis.com/auth/drive','https://spreadsheets.google.com/feeds']);

if (isset($_SESSION['access_token']) && $_SESSION['access_token'])
{
    $fileId = 'FILE_ID_HERE';
    $tokenArray = $client->fetchAccessTokenWithAssertion();
    $accessToken = $tokenArray["access_token"];
    $service = new Google_Service_Drive($client);
    $results = $service->files->get($fileId);
    $url = "https://spreadsheets.google.com/feeds/list/$fileId/od6/private/full";
    $method = 'GET';
    $headers = ["Authorization" => "Bearer {$accessToken}", "GData-Version" => "3.0"];
    $httpClient = new GuzzleHttp\Client(['headers' => $headers]);
    $resp = $httpClient->request($method, $url);
    $body = $resp->getBody()->getContents();
    $code = $resp->getStatusCode();
    $reason = $resp->getReasonPhrase();
    echo "$code : $reason\n\n";
    echo "$body\n";
}
else 
{
    $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php';
    header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}

DriveFiles
可以共享,因此检查用户的访问令牌实际上是不必要的

文档的页面详细说明了如何实现(使用特定于PHP的代码片段)。共享后,用户应该能够查看和导出文件

提供的错误消息表明,这可能是由于用户无权访问该文件或该文件不存在(错误的文件ID)。它建议用户与文件所有者交谈,以获得文件的权限

最后,如果您已经有权访问该文件,则可以。有三种实现方法。但是,如果希望将其导出为
.csv
文件,可以使用
导出
方法并指定MIME类型(
文本/csv

文档中的PHP代码片段如下所示:

$fileId = '1ZdR3L3qP4Bkq8noWLJHSr_iBau0DNT4Kli4SxNc2YEo';
$content = $driveService->files->export($fileId, 'application/pdf', array('alt' => 'media' ));
请注意,此
text/csv
仅适用于图纸。其他用于导出的MIME类型可以在文档中看到

$fileId = '1ZdR3L3qP4Bkq8noWLJHSr_iBau0DNT4Kli4SxNc2YEo';
$content = $driveService->files->export($fileId, 'application/pdf', array('alt' => 'media' ));