I have a script from Google drive and wordks fine when i executing by browser, but i want to execute it by Wget, but that doesn't work. Why does this not work with wget?
$client = new Google_Client();
$client->setAuthConfig($oauth_credentials);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/drive");
$service = new Google_Service_Drive($client);
if (!empty($_SESSION['upload_token'])) {
$client->setAccessToken($_SESSION['upload_token']);
if ($client->isAccessTokenExpired()) {
unset($_SESSION['upload_token']);
}
}
else {
$authUrl = $client->createAuthUrl();
}
DEFINE("TESTFILE", 'testfile-small.txt');
if (!file_exists(TESTFILE)) {
$fh = fopen(TESTFILE, 'w');
fseek($fh, 1024 * 1024);
fwrite($fh, "!", 1);
fclose($fh);
}
// This is uploading a file directly, with no metadata associated.
$file = new Google_Service_Drive_DriveFile();
$result = $service->files->insert(
$file,
array(
'data' => file_get_contents(TESTFILE),
'mimeType' => 'application/octet-stream',
'uploadType' => 'media'
)
);
wget
pulls what you'd receive if you navigated to a website yourself. That means that you'd receive whatever you'd normally receive were you to put the link into the browser yourself without any session information.
You can try wget
with --save-cookies
. Based on how OAuth works, you'll probably also need --header
, and quite possibly --referer
(for details see here. For an example see this SO post.)
This is complicated and is essentially creating a client wrapper for a PHP script. There is a better way.
You can execute a PHP file on a command line by using php [your php script]
(as per @Oldskool's comment). Note that this may not work.
You can also download a client for bash or PowerShell.