简体   繁体   中英

Uploading the files to same google drive using php

I was wondering is there any method to upload the files directly to our own Google drive by using php , Currently i am using a method like this.

            $drive = new Google_Client();
            $drive->setClientId($client_id);
            $drive->setClientSecret($client_secret);
            $drive->setRedirectUri($redirect_uri);
            $drive->setScopes(array('https://www.googleapis.com/auth/drive'));

            $gdrive = new Google_DriveService($drive);

            $_GET['code'] = 'ya2-------------------------OZXiA';

            //file_put_contents('token.json', $drive->authenticate());

            $drive->setAccessToken(file_get_contents('token.json'));

            $doc = new Google_DriveFile();

            $doc->setTitle('Test Document');
            $doc->setDescription('Test description');
            $doc->setMimeType('text/plain');

            $content = file_get_contents('/assets/new--detils.txt');

            $output = $gdrive->files->insert($doc, array(
                'data' => $content,
                'mimeType' => 'text/plain',
            ));
            print_r($output);

But this shows an error as

Fatal error: Uncaught exception Google_AuthException with message 'Error fetching OAuth2 access token, message: 'invalid_grant'' in /var/www/path/src/auth/Google_OAuth2.php:115

Here i am using $_GET['code'] = 'ya2-------------------------OZXiA' generated for that app,

Can anyone please suggest a method to do this, thanks in advance.

Not sure if it helps but I found this looking through Google Code...

"You'll get the invalid_grant error when you try to use the same authorization code."

I found it here: https://code.google.com/p/google-api-php-client/issues/detail?id=94

Not sure if that will help you or not...

Also found this tutorial: http://25labs.com/tutorial-implementing-google-api-using-oauth-2-0-in-php/

 $drive = new Google_Client();
 $drive->setClientId($client_id);
 $drive->setClientSecret($client_secret);
 $drive->setRedirectUri($redirect_uri); 
 **$drive->setAccessType('offline');**
 $authUrl = $client->createAuthUrl();
 echo $authUrl;

Now go the auth url and get the refresh token and other things. Make sure this is the first time you are asking for permission, if you aren't then you wont get the refreshtoken which is necessary to make authtokens in the future. If you have already give permissions just go to your google account and revoke the permissions of the app. This is what you should have in the file which you are redirecting too.

$tokeninfo = $drive->getAccessToken();
print_r($tokeninfo);

Now save the tokeninfo in a file like "token.json".

Now go back to the original file which will contain the code for upload.

$drive = new Google_Client();
$drive->setClientId($client_id);
$drive->setClientSecret($client_secret);
$drive->setRedirectUri($redirect_uri);
$drive->setScopes(array('https://www.googleapis.com/auth/drive'));
$drive->setAccessType('offline');
$service = new Google_Service_Drive($drive);
$refreshToken = "Your refresh token";
/* get it from token.json and it will look some like this 1**\/**asasfasfsfsd
remove the \ since this is actually json encoded.*/

$tokens = file_get_contents(TOKEN); /*TOKEN = the token.json file*/

$drive->setAccessToken($tokens);

if ($drive->isAccessTokenExpired()) {
   $drive->refreshToken($refreshToken);
   file_put_contents(TOKEN,$drive->getAccessToken());
}
Now you have done the authentication and just need to upload the file.
if ($drive->getAccessToken()) {
$file = new Google_Service_Drive_DriveFile();
$file->title ="the file name";
/*the upload code can be found in the examples*/
}

Note The file you upload cannot be downloaded by other ppl, you need to set googlepermissions to make the file shared. Just drop a comment if you need to code to add permissions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM