【问题标题】:Google Drive Files Upload via API not showing通过 API 上传的 Google Drive 文件未显示
【发布时间】:2016-10-12 13:47:57
【问题描述】:

大家好,我已经从 Google 官方文档中编写了一个文件上传脚本。它是一种服务器到服务器的身份验证,无需任何用户交互。我的意思是这些文件旨在上传到一个帐户,因此不需要用户进行身份验证,并且使用服务帐户私钥完成身份验证。我的文件似乎可以正常上传,因为它没有给我任何错误,并且给出了完整的对象,如下所示。

$client_email = '';
$json_str = file_get_contents('private_key.json');
$json = json_decode($json_str);
$scopes = array('https://www.googleapis.com/auth/drive');
$credentials = new \Google_Auth_AssertionCredentials(
             $client_email,
             $scopes,
             $json->private_key
);


$client = new \Google_Client();
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
   $client->getAuth()->refreshTokenWithAssertion();
}
$service = new \Google_Service_Drive($client);
$file = new \Google_Service_Drive_DriveFile();
$file->setTitle($quote->title);
$file->setDescription($quote->description);
//$file->setMimeType($mimeType);

$result = $service->files->insert($file,array(
                        'data' => file_get_contents($filePath),
                        'mimeType' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
                        'uploadType' => 'multipart',
                        'convert' => true

                      )
                    );


Google_Service_Drive_DriveFile Object

( [collection_key:protected] => 空格 [internal_gapi_mappings:protected] => 数组 ( )

[alternateLink] => https://docs.google.com/document/d/1p406Vcb_cI5lgPDx4YsNmwu-mA7S0ovIPsE94PuGo6w/edit?usp=drivesdk
[appDataContents] => 
[canComment] => 
[copyable] => 1
[createdDate] => 2016-05-22T22:13:46.332Z
[defaultOpenWithLink] => 
[description] => google doc quote test
[downloadUrl] => 
[editable] => 1
[embedLink] => https://docs.google.com/document/d/1p406Vcb_cI5lgPDx4YsNmwu-mA7S0ovIPsE94PuGo6w/preview
[etag] => "An1NegRH_Q1WfUh5qt1ofDI9qPI/MTQ2Mzk1NTIyNjMzMg"
[explicitlyTrashed] => 
[exportLinks] => Array
    (
        [application/rtf] => https://docs.google.com/feeds/download/documents/export/Export?id=1p406Vcb_cI5lgPDx4YsNmwu-mA7S0ovIPsE94PuGo6w&exportFormat=rtf
        [application/vnd.oasis.opendocument.text] => https://docs.google.com/feeds/download/documents/export/Export?id=1p406Vcb_cI5lgPDx4YsNmwu-mA7S0ovIPsE94PuGo6w&exportFormat=odt
        [text/html] => https://docs.google.com/feeds/download/documents/export/Export?id=1p406Vcb_cI5lgPDx4YsNmwu-mA7S0ovIPsE94PuGo6w&exportFormat=html
        [application/pdf] => https://docs.google.com/feeds/download/documents/export/Export?id=1p406Vcb_cI5lgPDx4YsNmwu-mA7S0ovIPsE94PuGo6w&exportFormat=pdf
        [application/zip] => https://docs.google.com/feeds/download/documents/export/Export?id=1p406Vcb_cI5lgPDx4YsNmwu-mA7S0ovIPsE94PuGo6w&exportFormat=zip
        [application/vnd.openxmlformats-officedocument.wordprocessingml.document] => https://docs.google.com/feeds/download/documents/export/Export?id=1p406Vcb_cI5lgPDx4YsNmwu-mA7S0ovIPsE94PuGo6w&exportFormat=docx
        [text/plain] => https://docs.google.com/feeds/download/documents/export/Export?id=1p406Vcb_cI5lgPDx4YsNmwu-mA7S0ovIPsE94PuGo6w&exportFormat=txt
    )

[fileExtension] => 
[fileSize] => 
[folderColorRgb] => 
[fullFileExtension] => 
[headRevisionId] => 
[iconLink] => https://ssl.gstatic.com/docs/doclist/images/icon_11_document_list.png
[id] => 1p406Vcb_cI5lgPDx4YsNmwu-mA7S0ovIPsE94PuGo6w
[imageMediaMetadataType:protected] => Google_Service_Drive_DriveFileImageMediaMetadata
[imageMediaMetadataDataType:protected] => 
[indexableTextType:protected] => Google_Service_Drive_DriveFileIndexableText
[indexableTextDataType:protected] => 
[kind] => drive#file
[labelsType:protected] => Google_Service_Drive_DriveFileLabels
[labelsDataType:protected] => 
[lastModifyingUserType:protected] => Google_Service_Drive_User
[lastModifyingUserDataType:protected] => 
[lastModifyingUserName] => email@gserviceaccount.com
[lastViewedByMeDate] => 2016-05-22T22:13:46.332Z
[markedViewedByMeDate] => 1970-01-01T00:00:00.000Z
[md5Checksum] => 
[mimeType] => application/vnd.google-apps.document
[modifiedByMeDate] => 2016-05-22T22:13:46.332Z
[modifiedDate] => 2016-05-22T22:13:46.332Z
[openWithLinks] => 
[originalFilename] => 
[ownedByMe] => 
[ownerNames] => Array
    (
        [0] => email@gserviceaccount.com
    )

[ownersType:protected] => Google_Service_Drive_User
[ownersDataType:protected] => array
[parentsType:protected] => Google_Service_Drive_ParentReference
[parentsDataType:protected] => array
[permissionsType:protected] => Google_Service_Drive_Permission
[permissionsDataType:protected] => array
[propertiesType:protected] => Google_Service_Drive_Property
[propertiesDataType:protected] => array
[quotaBytesUsed] => 0
[selfLink] => https://www.googleapis.com/drive/v2/files/1p406Vcb_cI5lgPDx4YsNmwu-mA7S0ovIPsE94PuGo6w
[shareable] => 
[shared] => 
[sharedWithMeDate] => 
[sharingUserType:protected] => Google_Service_Drive_User
[sharingUserDataType:protected] => 
[spaces] => Array
    (
        [0] => drive
    )

[thumbnailType:protected] => Google_Service_Drive_DriveFileThumbnail
[thumbnailDataType:protected] => 
[thumbnailLink] => 
[title] => Google doc quote2
[userPermissionType:protected] => Google_Service_Drive_Permission
[userPermissionDataType:protected] => 
[version] => 19
[videoMediaMetadataType:protected] => Google_Service_Drive_DriveFileVideoMediaMetadata
[videoMediaMetadataDataType:protected] => 
[webContentLink] => 
[webViewLink] => 
[writersCanShare] => 1
[modelData:protected] => Array
    (
        [labels] => Array
            (
                [starred] => 
                [hidden] => 
                [trashed] => 
                [restricted] => 
                [viewed] => 1
            )

        [parents] => Array
            (
                [0] => Array
                    (
                        [kind] => drive#parentReference
                        [id] => 0AOX081rNVUe2Uk9PVA
                        [selfLink] => https://www.googleapis.com/drive/v2/files/1p406Vcb_cI5lgPDx4YsNmwu-mA7S0ovIPsE94PuGo6w/parents/0AOX081rNVUe2Uk9PVA
                        [parentLink] => https://www.googleapis.com/drive/v2/files/0AOX081rNVUe2Uk9PVA
                        [isRoot] => 1
                    )

            )

        [userPermission] => Array
            (
                [kind] => drive#permission
                [etag] => "An1NegRH_Q1WfUh5qt1ofDI9qPI/pA5gH9Z7iyxg_Vu6dNFNiyQpzOE"
                [id] => me
                [selfLink] => https://www.googleapis.com/drive/v2/files/1p406Vcb_cI5lgPDx4YsNmwu-mA7S0ovIPsE94PuGo6w/permissions/me
                [role] => owner
                [type] => user
            )

        [owners] => Array
            (
                [0] => Array
                    (
                        [kind] => drive#user
                        [displayName] => email@gserviceaccount.com
                        [isAuthenticatedUser] => 1
                        [permissionId] => 16050536308259210811
                        [emailAddress] => email@gserviceaccount.com
                    )

            )

        [lastModifyingUser] => Array
            (
                [kind] => drive#user
                [displayName] => email@gserviceaccount.com
                [isAuthenticatedUser] => 1
                [permissionId] => 16050536308259210811
                [emailAddress] => 
            )

    )

[processed:protected] => Array
    (
    )

)

出于我的帐户安全原因,我在这里用 email@gserviceaccount.com 替换了我原来的 client_email。我的问题是我在驱动器上的任何地方都找不到我的文件。我的驱动器上有旧文件,但没有显示新文件。可能是什么问题?从 API 的响应中可以看出文件上传成功了吗?

非常感谢任何指导。谢谢!

【问题讨论】:

    标签: php rest google-drive-api


    【解决方案1】:

    这是一个常见问题,开发人员错误地认为服务帐户与他们的用户帐户有某种关联。它不是。如果您想上传到用户帐户,例如。你自己的,见How do I authorise an app (web or installed) without user intervention? (canonical ?)

    【讨论】:

    • 感谢您的回复。我对此表示怀疑,但不确定。这个答案有帮助。
    【解决方案2】:

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

    由于不属于您而未在您的 Google Drive 列表中列出的文件属于服务帐户,请注意您的应用程序发送到 Drive API 的每个请求都必须包含授权令牌。您的应用程序必须使用OAuth 2.0 来授权请求​​。

    如果请求成功,服务器会返回 HTTP 200 OK 状态码以及任何元数据:

    HTTP/1.1 200
    Content-Type: application/json
    
    {
    "id": "0Bx0vC9FG-2AmVnRic0FvSkpFNE0",
    "name": "My File"
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-30
      • 1970-01-01
      • 2021-02-11
      • 1970-01-01
      • 2015-10-13
      • 1970-01-01
      相关资源
      最近更新 更多