1

我正在使用 PHP 测试 Google Drive API,但没有设法通过命令行在我的驱动器上获取我的文档文件。我还尝试使用提供的 PHP 脚本创建一个文件夹。

我的 PHP 脚本运行并请求我从它请求我访问的 url 获得的身份验证代码。在按下回车键时,它看起来像是工作了一段时间,然后它没有错误地终止。当我转到“我的 Google 云端硬盘”时,我看不到任何新文件或文件夹

(使用 OSX MAMP 和 PHP)

谢谢

使用的 PHP 代码

<?php
    require_once 'google-api-php-client/src/Google_Client.php';
    require_once 'google-api-php-client/src/contrib/Google_DriveService.php';

    $client = new Google_Client();
    // Get your credentials from the APIs Console
    $client->setClientId('clien_id');
    $client->setClientSecret('Secret');
    $client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
    $client->setScopes(array('https://www.googleapis.com/auth/drive'));

    $service = new Google_DriveService($client);

    $authUrl = $client->createAuthUrl();

    //Request authorization
    print "Please visit:\n$authUrl\n\n";
    print "Please enter the auth code:\n";
    $authCode = trim(fgets(STDIN));

    // Exchange authorization code for access token
    $accessToken = $client->authenticate($authCode);
    $client->setAccessToken($accessToken);

    //Insert a file
    $file = new Google_DriveFile();
    $file->setTitle('My document');
    $file->setDescription('A test document');
    $file->setMimeType('text/plain');

    $data = file_get_contents('document.txt');

    $createdFile = $service->files->insert($file, array(
      'data' => $data,
      'mimeType' => 'text/plain',
    ));

    print_r($createdFile);
    ?>
4

0 回答 0