php - Unable to see the uploaded file on Google drive -
i using php , service account connect , upload test file google drive. not give me error when login using account google drive , check file. dont find it. why? please help.
i've used following code:
<!doctype html> <html> <head> </head> <body> <?php set_include_path( get_include_path() . path_separator . 'google-api-php-client-master/src' ); require_once 'google-api-php-client-master/src/google/client.php'; require_once 'google-api-php-client-master/src/google/service/drive.php'; require_once "google-api-php-client-master/src/google/service/oauth2.php"; session_start(); $drive_scope = 'https://www.googleapis.com/auth/drive'; $service_account_email = '<xyz>@developer.gserviceaccount.com'; $service_account_pkcs12_file_path = '<abc>-privatekey.p12'; $key = file_get_contents($service_account_pkcs12_file_path); $auth = new google_auth_assertioncredentials( $service_account_email, array($drive_scope), $key); $client = new google_client(); //$client->setuseobjects(true); $client->setassertioncredentials($auth); $service = new google_service_drive($client); //insert file $file = new google_service_drive_drivefile(); $file->settitle('my document'); $file->setdescription('a test document'); $file->setmimetype('text/plain'); $data = file_get_contents('my document.txt'); $createdfile = $service->files->insert($file, array( 'data' => $data, 'mimetype' => 'text/plain', 'uploadtype' => 'media' )); print_r($createdfile); ?> </body> </html>
the document you've created tied service account. can't login google drive website service account. explanation of service accounts kind of explains this. have 2 options:
if it's important document visible service account , personal account, create document using personal google account , share service account (via email address).
understand document exist, available service account. in $createdfile variable code sample, see "downloadurl" property. use url in code below.
$request = new google_http_request();
$requestsigned = self::$client->getauth()->sign($request);
$response = self::$client->getio()->makerequest($requestsigned);
$requestsigned->getresponsebody();
using either get method or list method, can retrieve "downloadurl" @ later time files.
Comments
Post a Comment