2016-12-19 2 views
2

내 Google 드라이브에 액세스하고 거기에 파일을 복사 할 서버 PHP 스크립트를 작성하고 싶습니다. 서버가 Google 드라이브의 자격증 명을 저장하고 승인을 요청하지 않아야합니다. 내가 본 모든 예제는 다양한 사용자가 드라이브에서 작업을 수행 할 수있는 웹 응용 프로그램을 설명합니다. 예를 들어 https://developers.google.com/drive/v3/web/quickstart/php 서버에 필요한 모든 자격 증명을 저장하려면 어떻게해야합니까?수동 승인없이 PHP 스크립트에서 Google 드라이브에 액세스하는 방법

답변

3

서비스 계정을 사용하는 것이 좋습니다. 서비스 계정은 미리 인증 된 더미 사용자와 같습니다. 즉, Google 드라이브 계정의 폴더를 서비스 계정과 공유하면 서비스 계정이 해당 계정에 업로드 할 수 있습니다. 서비스 계정 작동 방식에 대한 기사가 있습니다. Google Developers console Service account

서비스 계정으로 작업 할 때 기억해야 할 몇 가지 사항이 있습니다. 주로 서비스 계정이 파일을 기본적으로 업로드 할 때 사용 권한으로 파일을 소유하므로 업로드 후 파일에 개인 계정 권한을 부여해야합니다. 그것의 추가 단계.

PHP 클라이언트 라이브러리에는 서비스 계정 인증 사용 샘플이 있지만 드라이브가 없으므로 도서 부분을 google 드라이브로 변경해야합니다. Service-account.php

require_once __DIR__ . '/vendor/autoload.php'; 
// Use the developers console and download your service account 
// credentials in JSON format. Place the file in this directory or 
// change the key file location if necessary. 
putenv('GOOGLE_APPLICATION_CREDENTIALS='.__DIR__.'/service-account.json'); 
/** 
* Gets the Google client refreshing auth if needed. 
* Documentation: https://developers.google.com/identity/protocols/OAuth2ServiceAccount 
* Initializes a client object. 
* @return A google client object. 
*/ 
function getGoogleClient() { 
    return getServiceAccountClient(); 
} 
/** 
* Builds the Google client object. 
* Documentation: https://developers.google.com/api-client-library/php/auth/service-accounts 
* Scopes will need to be changed depending upon the API's being accessed. 
* array(Google_Service_Analytics::ANALYTICS_READONLY, Google_Service_Analytics::ANALYTICS) 
* List of Google Scopes: https://developers.google.com/identity/protocols/googlescopes 
* @return A google client object. 
*/ 
function getServiceAccountClient() { 
    try { 
     // Create and configure a new client object.   
     $client = new Google_Client(); 
     $client->useApplicationDefaultCredentials(); 
     $client->addScope([YOUR SCOPES HERE]); 
     return $client; 
    } catch (Exception $e) { 
     print "An error occurred: " . $e->getMessage(); 
    } 
} 

이 또한 도움이 될 수 있습니다 내 오랜 연구와 독서 구글 문서와 예제 내가 나를 위해 작동 방법을 발견 google drive samples

4

후.

  1. 서비스 계정이 필요합니다. 이 계정은 Google 드라이브 데이터에 액세스합니다.
  2. Google 도메인에서 작업하기 때문에이 서비스 계정에 도메인 전체 권한을 부여해야했습니다.
  3. Here you can find how to create a service account and grant it domain wide authority 그러나이 링크는 서비스 계정을 만들 때 JSON 형식 키 파일이 필요합니다주의하십시오 PHP 예를
  4. Here you can find the same instructions with PHP examples
  5. 을 가지고하지 않습니다.
  6. 나는 Google Application Default Credentials을 사용했습니다.

마지막으로이 노력 코드 :

<?php 
require_once '/path/to/google-api-php-client/vendor/autoload.php'; 

putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json'); 

$client = new Google_Client(); 
$client->useApplicationDefaultCredentials(); 
$client->setScopes(['https://www.googleapis.com/auth/drive']); 
$client->setSubject('[email protected]_want_to_work.for'); 

$service = new Google_Service_Drive($client); 

//Create a new folder 
$fileMetadata = new Google_Service_Drive_DriveFile(
    array('name' => 'Invoices', 
      'mimeType' => 'application/vnd.google-apps.folder')); 
$file = $service->files->create($fileMetadata, array('fields' => 'id')); 
echo $file->id; 
?>