【问题标题】:Google drive api-File uploaded but not listedGoogle drive api-文件已上传但未列出
【发布时间】:2014-01-24 12:06:00
【问题描述】:

我是 google drive api 的新手。我正在将文件从我的服务器上传到 google drive。

在我的 gmail 帐户中,我有

  1. 创建服务帐号,下载密钥并上传到服务器
  2. 提供对 Drive Api 和 SDK Api 的访问权限。
  3. 添加了一个控制器来与谷歌通信:

        set_include_path(get_include_path() . PATH_SEPARATOR . ROOT .DS. "vendors");
        require_once('Google/Client/Google_Client.php');
        require_once('Googl
    

e/Client/contrib/Google_DriveService.php');

    define('CLIENT_ID','my_client_here....');
    define('SERVICE_ACCOUNT_NAME','my_account_name here');
    define('KEY_FILE',VENDORS.'Google/Client/key/privatekey.p12');  // what i was availed by google.       

    if(is_file(KEY_FILE)){
        $client = new Google_Client();
        $key = file_get_contents(KEY_FILE);
        $client->setClientId(CLIENT_ID);
        $client->setAssertionCredentials(new Google_AssertionCredentials(
          SERVICE_ACCOUNT_NAME,
          array('https://www.googleapis.com/auth/drive'),
          $key)
     );


     $service = new Google_DriveService($client);


            $file = new Google_DriveFile();
            $file->setTitle('Any sample file title');
            $file->setDescription('An experiment to upload text file via server to server');
            $file->setMimeType('text/plain');

            $data = "Lorem ipsum is a dummy text";

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


            print_r($createdFile);

现在这个输出如下:

[kind] => drive#file
[id] => 0B-NwgyCI2GQENkswOHVNcVl2aDQ
[etag] => "11QSOvUX6KZKZVB6nCB3GloLTJQ/MTM5MDU2MzUwNTU2OQ"
[selfLink] => https://www.googleapis.com/drive/v2/files/0B-NwgyCI2GQENkswOHVNcVl2aDQ
[webContentLink] => https://docs.google.com/uc?id=0B-NwgyCI2GQENkswOHVNcVl2aDQ&export=download
[alternateLink] => https://docs.google.com/file/d/0B-NwgyCI2GQENkswOHVNcVl2aDQ/edit?usp=drivesdk
[iconLink] => https://ssl.gstatic.com/docs/doclist/images/icon_10_text_list.png
[title] => Any sample file title
[mimeType] => text/plain
[description] => An experiment to upload text file via server to server
[labels] => Array
    (
        [starred] => 
        [hidden] => 
        [trashed] => 
        [restricted] => 
        [viewed] => 1
    )

[createdDate] => 2014-01-24T11:38:25.679Z
[modifiedDate] => 2014-01-24T11:38:25.569Z
[modifiedByMeDate] => 2014-01-24T11:38:25.569Z
[lastViewedByMeDate] => 2014-01-24T11:38:25.569Z
[parents] => Array
    (
        [0] => Array
            (
                [kind] => drive#parentReference
                [id] => 0AONwgyCI2GQEUk9PVA
                [selfLink] => https://www.googleapis.com/drive/v2/files/0B-NwgyCI2GQENkswOHVNcVl2aDQ/parents/0AONwgyCI2GQEUk9PVA
                [parentLink] => https://www.googleapis.com/drive/v2/files/0AONwgyCI2GQEUk9PVA
                [isRoot] => 1
            )

    )
[downloadUrl] => https://doc-04-60-docs.googleusercontent.com/docs/securesc/lptjog45u94o44tbsu1586vcuinvv74h/mlvse3i90c59eb4q6ggg7d4c6v6q4hfd/1390557600000/03476129820556261866/03476129820556261866/0B-NwgyCI2GQENkswOHVNcVl2aDQ?h=16653014193614665626&e=download&gd=true
    [userPermission] => Array
        (
            [kind] => drive#permission
            [etag] => "11QSOvUX6KZKZVB6nCB3GloLTJQ/-HVXFUAxHplEpHWK6MSWEcIX7d8"
            [id] => me
            [selfLink] => https://www.googleapis.com/drive/v2/files/0B-NwgyCI2GQENkswOHVNcVl2aDQ/permissions/me
            [role] => owner
            [type] => user
        )

    [originalFilename] => Any sample file title
    [fileExtension] => 
    [md5Checksum] => 2c06987d6ae9cfcd7060c68f4356231a
    [fileSize] => 31
    [quotaBytesUsed] => 31
    [ownerNames] => Array
        (
            [0] => sdsdsdswrwew@developer.myemail.com
        )

    [owners] => Array
        (
            [0] => Array
                (
                    [kind] => drive#user
                    [displayName] => dsdadasd3@developer.myemail.com
                    [isAuthenticatedUser] => 1
                    [permissionId] => 03476129820556261866
                )

        )

    [lastModifyingUserName] => 89989989889@developer.myemailid.com
    [lastModifyingUser] => Array
        (
            [kind] => drive#user
            [displayName] => my_display_name
            [isAuthenticatedUser] => 1
            [permissionId] => 03476129820556261866
        )

    [editable] => 1
    [copyable] => 1
    [writersCanShare] => 1
    [shared] => 
    [appDataContents] => 

.......

现在的主要问题是通过此上传的文件未在我的谷歌驱动器文件列表中列出,但如果我在我的帐户中查询可用文件,我只会得到通过此 api 上传的文件列表,而不是我创建的文件列表在 google drive 界面中手动操作。

查看我的代码以获取文件列表..

   .....................
            $client = new Google_Client();
            $key = file_get_contents(KEY_FILE);
            $client->setClientId(CLIENT_ID);
            $client->setAssertionCredentials(new Google_AssertionCredentials(
              SERVICE_ACCOUNT_NAME,
              array('https://www.googleapis.com/auth/drive'),
              $key)
            );

            $service = new Google_DriveService($client);
    ....................

            $files = $service->files->listFiles();

现在的问题:

  1. 为什么我的谷歌驱动器列表中没有列出文件。
  2. 为什么我在 google 控制台中创建的服务帐户被视为不同的用户。例如,文件列表与原始用户不同,原始用户文件未在 api 列出的文件中列出。

请让我知道我在哪里做错了,也让我知道我想要实现的目标是否可行。我创建了一个服务帐户而不是Client ID for web application,因为该脚本将在没有任何用户界面的情况下自动运行。

请推荐....

【问题讨论】:

    标签: php web-services google-api google-drive-api


    【解决方案1】:

    为什么我的 Google 云端硬盘列表中没有列出这些文件?

    因为它们不属于你。它们属于服务帐号。

    为什么我在 google 控制台中创建的服务帐户被视为不同的用户。

    因为那是一个服务帐户,即。它是自己的用户。

    如果您希望上传的文件包含在您自己的云端硬盘内容中,那么您需要使用自己的帐户凭据访问云端硬盘 SDK。这不需要涉及用户交互。您只需获取一次刷新令牌,然后使用该令牌为 Drive 生成​​访问令牌。

    【讨论】:

    • 嗨 pinoyyid ...有什么方法可以通过任何 api 将这些文件共享或传输到其他用户帐户...我在哪里可以查看应用程序文件
    • 是的,它们可以像任何其他文件一样共享。 “应用程序文件”是什么意思?
    • 表示我在 google 控制台中创建的应用 id。谢谢
    • 现在我按照你所说的那样关注这个......developers.google.com/drive/web/auth/web-server......你认为这对我有用吗
    • 可以。请注意,文档始终假定用户将验证自己的云端硬盘帐户。您的情况是您使用的是自己的帐户,因此用户无权执行。您当然需要以某种方式对用户进行身份验证。
    猜你喜欢
    • 1970-01-01
    • 2015-10-13
    • 2017-10-10
    • 1970-01-01
    • 2020-03-10
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多