谷歌驱动器API - 文件上传但未列出 [英] Google drive api-File uploaded but not listed

查看:119
本文介绍了谷歌驱动器API - 文件上传但未列出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我的gmail帐户中,我有


  1. 创建服务帐户,下载密钥并上传到服务器
  2. 提供对Drive Api和SDK Api的访问

  3. 添加了一个Controller来与谷歌沟通:

      set_include_path(get_include_path( )。PATH_SEPARATOR。ROOT .DS。供应商); 
    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'); //我通过google获得了什么。

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


$ service = new Google_DriveService($ client);


$ file = new Google_DriveFile();
$ file-> setTitle('任何示例文件标题');
$ file-> setDescription('通过服务器上传文本文件到服务器的实验');
$ file-> setMimeType('text / plain');

$ data =Lorem ipsum是一个虚拟文本;
$ b $ createdFile = $ service-> files-> insert($ file,array(
'data'=> $ data,
'mimeType'=> 'text / plain',
));


print_r($ createdFile);

现在输出如下:

  [kind] =>驱动器#文件
[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] =>任何示例文件标题
[mimeType] => text / plain
[description] =>通过服务器将文本文件上传到服务器的实验
[labels] =>数组

[starred] =>
[hidden] =>
[trashed] =>
[restricted] =>
[查看] => 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] =>阵列

[种类] =>驱动#许可
[ETAG] => 中11QSOvUX6KZKZVB6nCB3GloLTJQ / -HVXFUAxHplEpHWK6MSWEcIX7d8
[ID] =>我
[selfLink] => https://www.googleapis.com/drive/v2/files/0B-NwgyCI2GQENkswOHVNcVl2aDQ/permissions/me
[角色] =>拥有者
[类型] =>用户


[originalFilename] =>任何示例文件标题
[fileExtension] =>
[md5Checksum] => 2c06987d6ae9cfcd7060c68f4356231a
[fileSize] => 31
[quotaBytesUsed] => 31
[ownerNames] =>数组

[0] => sdsdsdswrwew@developer.myemail.com


[所有者] => 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上传的文件列表,不是我在谷歌驱动界面手动创建的。



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

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

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

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

现在提问:


  1. 为什么文件没有列在我的谷歌驱动器列表中。

  2. 为什么我在Google控制台中创建的服务帐户被视为不同的用户。例如,文件列表与原始用户不同,并且orignal用户文件未列在api列出的文件中。

请让我知道我在哪里做错了,也让我知道,如果我试图实现是否可能。我创建了一个服务帐户,而不是 Web应用程序的客户端ID ,因为脚本将自动运行而无需任何用户界面。



请建议....

为什么文件没有列在我的谷歌驱动列表中?



因为他们不属于你。他们属于服务帐户。



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



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



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


I am new to google drive api.. I am working on uploading files from my server to google drive.

In my gmail account I have

  1. created a Service Account, download keys and uploaded to server
  2. Provided access to Drive Api and SDK Api.
  3. Added a Controller to communicate with google as:

        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);

Now the output of this is as follows:

[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] => 

..............

Now the main problem is that the files upload via this is not listing in my google drive files list but if I query for the availale files in my account I get only the file list uploaded via this api and not the one I created manually in the google drive interface.

See my code to get file list..

   .....................
            $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();

Now Questions:

  1. Why do the files not listed in my google drive list.
  2. Why the service account I created in google console is treated as like a different user. eg file list are different than original users and orignal user file are not listed in api listed files.

Please let me know where I am doing wrong, also let me know if what I am trying to achieve is possible or not. I have created a Service Account rather than Client ID for web application because the script will run automatically without any user interface.

Please suggest....

解决方案

Why do the files not listed in my google drive list?

Because they don't belong to you. They belong to the service account.

Why the service account I created in google console is treated as like a different user.

Because that is what a service account, ie. it's own user.

If you want uploaded files to be in your own Drive contents, then you need to use your own account credentials to the Drive SDK. This does not need to involve user interaction. You simply need to acquire a refresh token one time, then use that subsequently to generate the access token for Drive.

这篇关于谷歌驱动器API - 文件上传但未列出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆