如何使用PHP在Google文档API中检索正文内容

时间:2019-02-20 06:39:20

标签: php google-docs

在google文档中,我仅获取标题,但是在获取正文/整个文档时显示错误。

getTitle()更改为GetBody()时出现错误-

  

致命错误:未捕获的Google_Service_Exception:{     “错误”:{       “代码”:403,       “ message”:“请求的验证范围不足。”,       “错误”:[         {           “ message”:“请求的验证范围不足。”,           “ domain”:“ global”,           “原因”:“禁止”         }       ],       “状态”:“ PERMISSION_DENIED”     }

这是我当前仅用于标题的工作代码-

<?PHP
    require __DIR__ . '/vendor/autoload.php';
    /**
     * Returns an authorized API client.
     * @return Google_Client the authorized client object
     */
    function getClient()
    { 
        $client = new Google_Client();
        $client->setApplicationName('Google Docs API PHP Quickstart');
        $client->setScopes(Google_Service_Docs::DOCUMENTS_READONLY);
        $client->setAuthConfig('credentials.json');
        $client->setAccessType('offline');
        // Load previously authorized credentials from a file.
        $credentialsPath = expandHomeDirectory('token.json');
        if (file_exists($credentialsPath)) {
            $accessToken = json_decode(file_get_contents($credentialsPath), true);
        } else {
            // Request authorization from the user.
            $authUrl = $client->createAuthUrl();
            printf("Open the following link in your browser:\n%s\n", $authUrl);
            print 'Enter verification code: ';
            $authCode = trim(fgets(STDIN));
            // Exchange authorization code for an access token.
            $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
             // Store the credentials to disk.
            if (!file_exists(dirname($credentialsPath))) {
                mkdir(dirname($credentialsPath), 0700, true);
            }
            file_put_contents($credentialsPath,json_encode($accessToken));
            printf("Credentials saved to %s\n", $credentialsPath);
        }
        $client->setAccessToken($accessToken);
        // Refresh the token if it's expired.
        if ($client->isAccessTokenExpired()) {        $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
            file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
        }
        return $client;
    }
    /**
     * Expands the home directory alias '~' to the full path.
     * @param string $path the path to expand.
     * @return string the expanded path.
     */
    function expandHomeDirectory($path)
    {
        $homeDirectory = getenv('HOME');
        if (empty($homeDirectory)) {
            $homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH');
        }
        return str_replace('~', realpath($homeDirectory), $path);
    }

    // Get the API client and construct the service object.
    $client = getClient();
    $service = new Google_Service_Docs($client);
    $documentId = '10v9xARnPkQEYH_fYoB2kDbU7656848Ap6uUff6565RbbAW0_jFyMhg';
    $doc = $service->documents->get($documentId);
    printf("The document title is: %s\n", $doc->getTitle());
?>

标题正在工作,但如何获取所有文档?

0 个答案:

没有答案