簡體   English   中英

通過API上傳的Google雲端硬盤文件未顯示

[英]Google Drive Files Upload via API not showing

大家好,我已經從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] => Array()

[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
    (
    )

我出於帳戶安全原因將原來的client_email替換為email@gserviceaccount.com。 我的問題是我無法在驅動器上的任何位置找到文件。 我的驅動器上有舊文件,沒有新文件顯示。可能是什么問題? 從API的響應中得知文件已成功上傳嗎?

將高度贊賞任何指導。 謝謝!

這是一個常見問題,開發人員錯誤地認為服務帳戶與他們的用戶帳戶相關。 不是。 如果您要上傳到用戶帳戶,例如 您自己,請參閱如何在沒有用戶干預的情況下授權應用程序(網絡或已安裝)? (規范?)

如果您希望上傳的文件包含在自己的雲端硬盤內容中,則需要使用自己的帳戶憑據訪問雲端硬盤SDK 這不需要涉及用戶交互。 您只需要一次性獲取刷新令牌,然后使用該令牌來生成Drive的訪問令牌。

未列在Google雲端硬盤列表中的文件,因為它們不屬於您,它們屬於服務帳戶,請注意,您的應用程序發送到Drive API的每個請求都必須包含授權令牌。 您的應用程序必須使用OAuth 2.0來授權請求​​。

如果請求成功,服務器將返回HTTP 200 OK狀態代碼以及任何元數據:

HTTP/1.1 200
Content-Type: application/json

{
"id": "0Bx0vC9FG-2AmVnRic0FvSkpFNE0",
"name": "My File"
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM