Following the steps in this tutorial, I was able to upload a file to my Google Drive via API, the priority was to upload it without the user's consent.
However, I noticed that the Refresh Token has to be always updated and for this reason, I looked for another solution to send the files without the consent screen being sent to the user.
In the first hours everything worked out, however, now it does not return TOKEN and consequently no longer uploads.
<?php
function get_access_token( $force_refresh=false ) {
global $client_id, $client_secret, $refresh_token, $verbose ;
if( $verbose ) { echo "> retrieving access token<br />" ; }
$token_filename = "/tmp/access_token_" . md5( $client_id . $client_secret . $refresh_token ) ;
$access_token = "" ;
if( !file_exists($token_filename) || $force_refresh===true ) {
// no previous access token, let's get one
if( $verbose ) { echo "> getting new one<br />" ; }
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, "https://accounts.google.com/o/oauth2/token" ) ;
curl_setopt( $ch, CURLOPT_PORT , 443 ) ;
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ) ;
curl_setopt( $ch, CURLOPT_POST, 1 ) ;
curl_setopt( $ch, CURLOPT_POSTFIELDS, "client_id={$client_id}&client_secret={$client_secret}&refresh_token={$refresh_token}&grant_type=refresh_token" ) ;
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ) ;
curl_setopt( $ch, CURLOPT_HEADER, true ) ;
curl_setopt( $ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded") ) ;
echo "Curl error: " . curl_error($ch) . "<br />";
$response = curl_exec( $ch ) ;
$response = parse_response( $response ) ;
// todo: make sure that we got a valid response before retrieving the access token from it
$access_token = json_decode( $response["body"] ) ;
$access_token = $access_token->access_token ;
file_put_contents( $token_filename, $access_token ) ;
} else {
// we already have something cached, with some luck it's still valid
$access_token = file_get_contents( $token_filename ) ;
if( $verbose ) { echo "> from cache<br />" ; }
}
if( $access_token=="" ) {
echo "ERROR: problems getting an access token<br />" ;
exit( 1 ) ;
}
return $access_token ;
}
?>
Is it possible to integrate my application with Google Drive without the consent screen and without the need to always renew this TOKEN?