2014-11-13 6 views
0

기본적으로 Google 개발자의 설명서는 API의 새 버전과 일치하도록 업데이트되지 않습니다. 여기 Google 드라이브 PHP api, 드라이브에 파일 삽입

내 코드입니다 :

<?php 
require_once 'autoload.php'; 

$client = new Google_Client(); 
// Get your credentials from the console 
$client->setClientId ('CLIENT_ID'); 
$client->setClientSecret ('CLIENT_SECRET'); 
$client->setRedirectUri ('urn:ietf:wg:oauth:2.0:oob'); 
$client->setScopes (array (
     'https://www.googleapis.com/auth/drive' 
)); 

$service = new Google_Service ($client); 

$authUrl = $client->createAuthUrl(); 

// Request authorization 
print "Please visit:\n$authUrl\n\n"; 
print "Please enter the auth code:\n"; 
$authCode = trim (fgets (STDIN)); 

// Exchange authorization code for access token 
$accessToken = $client->authenticate ($authCode); 
$client->setAccessToken ($accessToken); 

// Insert a file 
$file = new Google_Service_Drive_DriveFile(); 
$file->setTitle ('My document'); 
$file->setDescription ('A test document'); 
$file->setMimeType ('text/plain'); 

$data = file_get_contents ('document.txt'); 

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

echo $createdFile; 
?> 

그리고 이것은 내가 오류입니다 :

PHP Notice: Undefined property: Google_Service::$files in D:\DATA\java\php workspace\Google Drive api test\quickstart.php on line 34 
PHP Fatal error: Call to a member function insert() on a non-object in D:\DATA\java\php workspace\Google Drive api test\quickstart.php on line 34 

이 코드의 문제가있는 라인 : 당신은 어떤이있는 경우

$createdFile = $service->files->insert 

만약 내가 다른 수업을 들여와야한다고 생각하거나 조언을 해주십시오. 고맙습니다.

답변

1

새로운 문서가없는 것과 관련하여 고민하고있었습니다. 그 결과를 모두 파악한 후 Google OAuth 사용법, Google 드라이브 SDK에서 폴더 생성 및 다른 사람들을 돕기위한 간단한 예를 만들었습니다. 당신은 GitHub에서 포크 수 있습니다 : https://github.com/numsu/google-drive-sdk-api-php-insert-file-parent-example

행운을 빕니다!

0

당신이 말할 수있는 $ files라는 이름의 개체가 없다고 생각합니다. 수행하려는 작업에 부합하는 $ 파일의 새 인스턴스를 만든 다음 삽입을 수행하십시오. 도움이 되길 바랍니다.

0

v3 인 경우 create를 사용해야합니다. v3에 삽입 할 수 없습니다.

$createdFile = $service->files->create($file, array (
    'data' => $data, 
    'mimeType' => 'text/plain', 
    'uploadType' => 'media' 
)); 
관련 문제