2016-05-22 22 views
0

こんにちはすべてGoogleの公式ドキュメントからファイルアップロードスクリプトを作成しました。これは、ユーザーとのやりとりなしに、サーバーとサーバーの認証です。私はファイルが1つのアカウントにアップロードされることを意図しているので、ユーザーは認証する必要はなく、認証はサービスアカウントの秘密鍵を使用して行われます。私のファイルは私にエラーを与えず、完全なオブジェクトをコードの下に示すように与えるので、ファイルはうまくアップロードされているようです。GoogleドライブファイルAPI経由でのアップロードが表示されない

$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:保護] =>スペース [internal_gapi_mappings:保護] =>アレイ ( )

[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 protected] 
[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 protected] 
    ) 

[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 protected] 
         [isAuthenticatedUser] => 1 
         [permissionId] => 16050536308259210811 
         [emailAddress] => [email protected] 
        ) 

      ) 

     [lastModifyingUser] => Array 
      (
       [kind] => drive#user 
       [displayName] => [email protected] 
       [isAuthenticatedUser] => 1 
       [permissionId] => 16050536308259210811 
       [emailAddress] => 
      ) 

    ) 

[processed:protected] => Array 
    (
    ) 

私は、電子メールで私のオリジナルclient_emailを交換しました@ gserviceaccount.comはアカウントのセキュリティ上の理由からです私の問題は、ドライブ上のどこにでもファイルを見つけることができないことです。ドライブに古いファイルがあり、新しいファイルが表示されません。何が問題なのでしょうか?ファイルが正常にアップロードされたというAPIの応答から聞こえますか?

ご指摘いただきありがとうございます。ありがとう!

答えて

1

これは、サービスアカウントが何らかの形でユーザーアカウントに関連していると誤って信じているという一般的な問題です。そうではありません。ユーザーアカウントにアップロードする場合は、あなた自身、あなたはuploaded files独自のドライブの内容になりたい場合はHow do I authorise an app (web or installed) without user intervention? (canonical ?)

+0

感謝を。私はそれを疑ったが、確かではなかった。この回答は役に立ちます。 –

0

を参照してください、あなたはDrive SDKに自分のアカウントの資格情報を使用する必要があります。これは、ユーザーのやりとりを伴う必要はありません。リフレッシュトークンを1回取得するだけで、後でそれを使用してドライブのアクセストークンを生成するだけです。

ファイルはあなたのものではないため、あなたのGoogleドライブのリストには表示されません。これらはサービスアカウントに属しており、アプリケーションがドライブAPIに送信するすべてのリクエストには認証トークンが必要です。アプリケーションでは、要求を承認するためにOAuth 2.0を使用する必要があります。

要求が成功すると、サーバーは、任意のメタデータと一緒にHTTP 200 OKステータスコードを返す:あなたの応答のための

HTTP/1.1 200 
Content-Type: application/json 

{ 
"id": "0Bx0vC9FG-2AmVnRic0FvSkpFNE0", 
"name": "My File" 
} 
関連する問題