2014-09-07 124 views
15

Google Drive PHP API kullanarak yerel bir dosyayı Google Drive'a yüklemek için küçük bir komut dosyası yazmaya çalışıyorum. dokümantasyon çok kötü korunur, ama şu ana kadar kod böyle görünümlü olmalıdır eminim: Kimlik doğrulama doğru yapmak mümkün değilim olduğunuGoogle Drive PHP API - Basit Dosya Yükleme

<?php 

include_once 'Google/Client.php'; 
include_once 'Google/Service/Drive.php'; 
include_once 'Google/Auth/OAuth2.php'; 

$client = new Google_Client(); 

$client->setScopes(array('https://www.googleapis.com/auth/drive.file')); 
$client->setClientId('dfgdfgdg'); 
$client->setClientSecret('dfgdfgdf'); 
$client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob'); 


$service = new Google_Service_Drive($client); 

$data = file_get_contents("a.jpg"); 

// create and upload a new Google Drive file, including the data 
try 
{ 
//Insert a file 
$file = new Google_Service_Drive_DriveFile($client); 

$file->setTitle(uniqid().'.jpg'); 
$file->setMimeType('image/jpeg'); 

$createdFile = $service->files->insert($file, array(
    'data' => $data, 
    'mimeType' => 'image/jpeg', 
    'uploadType' => 'media', 
)); 
} 
catch (Exception $e) 
{ 
    print $e->getMessage(); 
} 

print_r($createdFile); 

?> 

sorun (veya başka yanlış bir şey yapıyorum?). Aldığım hatadır:

HTTP Error: Unable to connect: 'fopen(compress.zlib://https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart) [function.fopen]: failed to open stream: operation failed' 

bu hatanın Ardından: Neyi yanlış yapıyorum

Notice: Undefined variable: createdFile in C:\wamp\www\GoogleAPI\index.php on line 39 

? Google Drive PHP API kullanarak Google Drive'a bir dosya yüklemek için basit bir çalışma komut dosyası sağlayabilir misiniz? Şimdiden teşekkür ederim!

cevap

15

kullanın bu kod kimlik doğrulaması ve bir test dosyası yüklemek için. Kimlik doğrulaması yapmak için bu belgenin kendisine <YOUR_REGISTERED_REDIRECT_URI> (ve ayrıca konsolda) ayarlamalısınız.

require_once 'Google/Client.php'; 
require_once 'Google/Service/Drive.php'; 

$client = new Google_Client(); 
// Get your credentials from the console 
$client->setClientId('<YOUR_CLIENT_ID>'); 
$client->setClientSecret('<YOUR_CLIENT_SECRET>'); 
$client->setRedirectUri('<YOUR_REGISTERED_REDIRECT_URI>'); 
$client->setScopes(array('https://www.googleapis.com/auth/drive.file')); 

session_start(); 

if (isset($_GET['code']) || (isset($_SESSION['access_token']) && $_SESSION['access_token'])) { 
    if (isset($_GET['code'])) { 
     $client->authenticate($_GET['code']); 
     $_SESSION['access_token'] = $client->getAccessToken(); 
    } else 
     $client->setAccessToken($_SESSION['access_token']); 

    $service = new Google_Service_Drive($client); 

    //Insert a file 
    $file = new Google_Service_Drive_DriveFile(); 
    $file->setName(uniqid().'.jpg'); 
    $file->setDescription('A test document'); 
    $file->setMimeType('image/jpeg'); 

    $data = file_get_contents('a.jpg'); 

    $createdFile = $service->files->create($file, array(
      'data' => $data, 
      'mimeType' => 'image/jpeg', 
      'uploadType' => 'multipart' 
     )); 

    print_r($createdFile); 

} else { 
    $authUrl = $client->createAuthUrl(); 
    header('Location: ' . $authUrl); 
    exit(); 
} 
+0

Cevabınız için teşekkürler! Sağladığın senaryonun ihtiyacım olan şeye çok yakın olduğunu düşünüyorum. Önceki versiyonumda $ _GET ['code'] değerini başarıyla almayı başardım. Senaryonuzun versiyonu ile de sorun yok. http:? Kod Tamam demektir //localhost/googleApi/index.php kodu = 4/T1PUKqjURysd_pzhnu7sfG_SPXxc.YsHYHi9UwVyjz_MlCJoi2I5xqokAI My URL olarak değiştirilir. Ne yazık ki, bundan hemen sonra şu hataları alıyorum: – mirosoft

+0

Önemli hata: 'HTTP_ hatası:' ile bağlanamayan istisna 'Google_IO_Exception': Bağlantı kurulamıyor: 'fopen (https://accounts.google.com/o/oauth2/token) [function.fopen ]: akışı açmak için başarısız oldu: '' C: \ wamp \ www \ GoogleAPI \ Google \ IO \ Stream.php satırında geçersiz argüman '' 112 – mirosoft

+0

sunucunuzdaki bazı yapılandırmalar sorunu çözebilir, OS'nizde hatayı aradım ve bulundu Bu: http://stackoverflow.com/q/25193378/3477084 – Hafez

1

bunu kullanın

<?php 
    require_once 'google-api-php-client/src/Google_Client.php'; 
    require_once 'google-api-php-client/src/contrib/Google_DriveService.php'; 

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

    $service = new Google_DriveService($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_DriveFile(); 
    $localfile = 'a.jpg'; 
    $title = basename($localfile); 
    $file->setTitle($title); 
    $file->setDescription('My File'); 
    $file->setMimeType('image/jpeg'); 

    $data = file_get_contents($localfile); 

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

    print_r($createdFile); 
    ?> 
+1

Cevabınız için teşekkürler, maalesef bu eski bir örnektir, Google'dan zaten kullanımdan kaldırılmıştır. Dahil olan dosyalar bile değiştirilir. – mirosoft