php - access drive with access token -


struggling understand oauth2 token , refresh token processes

ive got code

    $url = 'https://www.googleapis.com/oauth2/v3/token'; $data = array('client_id' => 'clientid', 'client_secret' => 'secret','refresh_token' => 'token','grant_type' => 'refresh_token'); $options = array(     'http' => array(         'header'  => "content-type: application/x-www-form-urlencoded",         'method'  => 'post',         'approval_prompt'=>'force',         'access_type'=>'offline',         'content' => http_build_query($data),     ), ); $context  = stream_context_create($options); $result = file_get_contents($url, false, $context); 

that code above gives me access token , , followed link suggested 1 fellow stackoverflower, pinoyyid, , im confunsed on how correctly use resulting access token access drive , copy file...

all process ive seen involves $client = new google_client() , im not sure on how use whole post http://..... thing, need figure out if use access token got code above in new instance of google client, or post url necesary info ( im not clear on ) help/clarification appreciated guys really

edit #1 want achieve allow end user access drive via webpage, let them copy spreadsheet in drive , , access via website, store data on spreadsheet,the spreadsheet on drive, never on end user

edit #2

code per posts follows, using service account,,,,the files inside gmail account created on api console service account

<?php require 'google/autoload.php'; $client = new google_client(); // replace application name. $client->setapplicationname("test"); // replace service using. $service = new google_service_drive($client);  // file location should point private key file. $key = file_get_contents($_server['document_root'] . '/number-privatekey.p12'); $user_to_impersonate = 'admin@testpr.com'; $cred = new google_auth_assertioncredentials(   'number@developer.gserviceaccount.com',   array('https://www.googleapis.com/auth/drive'),  ****//this here has drive not drive.file   $key,   'notasecret',   'http://oauth.net/grant_type/jwt/1.0/bearer',   $user_to_impersonate ); $client->setassertioncredentials($cred); if ($client->getauth()->isaccesstokenexpired()) {   $client->getauth()->refreshtokenwithassertion(); } $originfileid = "longnumber"; $copytitle = 'copied'; $newfile = copyfile($service, $originfileid, $copytitle); print_r($newfile); function copyfile($service, $originfileid, $copytitle)  {   $copiedfile = new google_service_drive_drivefile();   $copiedfile->settitle($copytitle);   try {     return $service->files->copy($originfileid, $copiedfile);   } catch (exception $e) {     print "an error occurred: " . $e->getmessage();   }   return null; } ?> 

so got working now, ty time guys , edited post reflect dang thing

if want give end user access drive, have give application authority make api calls on behalf of user (in case you) in domain. have set service account , generate p12 key in google developers console. have enter https://www.googleapis.com/auth/drive api scope in admin console well.

full explanation , examples can found here: https://developers.google.com/api-client-library/php/auth/service-accounts.

to achieve need google api's client library: https://github.com/google/google-api-php-client (also mentioned in google manual).

code example let users make api calls on behalf of 1 of accounts: https://developers.google.com/api-client-library/php/guide/aaa_oauth2_service


Comments