-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is it possible to upload files to my own Google Drive using this SDK? #801
Comments
Yes you could use this library to accomplish this - just download Service Account Credentials and set the file path with However, Wouldn't you be better off using the Drive desktop client for that? |
@bshaffer Thank you for your reply. Yes, I tried with Service account, however, its not giving me the same results as I have in my Google Drive. You can check the results here And here is what I actually have in my Google Drive Would you please explain why this happens? |
@bshaffer Here is the source code I've written: <?php
include_once __DIR__ . '/vendor/autoload.php';
$client = new Google_Client();
$credentialsFile = __DIR__ . '/credentials/service_account.json';
if (!file_exists($credentialsFile)) {
throw new RuntimeException('Service account credentials Not Found!');
}
$client->setAuthConfig($credentialsFile);
$client->setApplicationName("Service Account Example");
$client->setScopes(Google_Service_Drive::DRIVE);
$service = new Google_Service_Drive($client);
$files = $service->files->listFiles();
print '<pre>';
foreach ($files as $file) {
print_r($file);
print PHP_EOL . '--------------' . PHP_EOL;
}
print '</pre>'; |
What results are you getting? It's probably because you need to set the subject. Try doing the following: $client->setSubject('yourdriveemail@gmail.com'); I am surprised the API isn't just throwing an error. Is the response coming back with an empty array for files? |
Not an empty array... It contains only one file. |
@bshaffer I got an exception when I do that. |
@bshaffer it looks like using the |
@bshaffer After getting rid of the |
@bshaffer As long as I am using a |
I'm not sure why you're seeing different files - I was under the impression it was impossible to upload to drive without a user context. We are doing something here that might be useful. We set accessType to // create an authorized client
$client = new Google_Client();
$client->setClientId($clientId);
$client->setClientSecret($clientSecret);
// create file cache for storing the access token and refresh token
$cache = new Google_Cache_File(sys_get_temp_dir().'/google-api-php-client-tests');
// make sure we have a token
if (!$token = $cache->get('access_token')) {
$client->setRedirectUri("urn:ietf:wg:oauth:2.0:oob");
$client->setConfig('access_type', 'offline');
$authUrl = $client->createAuthUrl();
echo "\nPlease enter the auth code:\n";
ob_flush();
`open '$authUrl'`;
$authCode = trim(fgets(STDIN));
$token = $client->fetchAccessTokenWithAuthCode($authCode);
if (!isset($token['access_token'])) {
exit('failed!');
}
$cache->set('access_token', $token);
}
$client->setAccessToken($token); |
@bshaffer I actually referred to this example that exists in the SDK.
For oAuth server-to-server interaction, I referred to this:
Probably the example is not for the right case? |
The second link there is referring to version 1 of this library, and has changed according to the Upgrading document in version 2. I suggest you stick with the first example. We'll be moving the docs to version 2 very soon. |
@bshaffer I know that, but the example should work for my case. Can you give me sample code that do the job for me? I need it done ASAP... I know this is out of your interest/service but I'd like to ask you to for me :) I was not able to get the API to work for my case or to find any examples on the Internet that works with Google Drive. I have to use the service account because it shouldn't be asking me to log in. |
I have already provided several code samples that will accomplish this task. In addition, there is this example that exists as part of the repository which accomplishes what you are trying to do. |
@bshaffer This is not using service accounts. I wrote the code that combines the 2 examples (service account and file uploading) as you can see. As this reference says, I thought service accounts should work against my own google drive. Instead, it works against another mysterious google drive space that's independent of my drive and owned by that service account, which makes it impossible to access the files with my own google account. Is that a problem with the PHP SDK? Should I try version 1 or other SDK in other languages? |
You have two options: 1. Create an "installed application" and use a refresh tokenThis is what I outlined in my code sample above. This does not use a service account, but instead uses an Oauth "installed app" which fetches the access token with a refresh token indefinitely 2. Create a "service account" and from your USER account, authorize the client so it shows up herethis is trickier, as you will need to create an OAuth Client just to authorize your user go to API Manager > Credentials > New Credentials > OAuth Client ID and then select other as the client type. Now download those credentials (below, $client->setAuthConfig('/path/to/client_secret_xxx.json');
$client->addScope("https://www.googleapis.com/auth/drive");
$client->setRedirectUri("urn:ietf:wg:oauth:2.0:oob"); // this is so you can grab the code in the browser
header('Location: '. $client->createAuthUrl());exit; now you can copy the code and exchange it for an access token like this: $client->fetchAccessTokenWithAuthCode($code);
After all this, you will see the client show up under your authorized clients: NEXT, you can switch back to your $client->setAuthConfig('/path/to/service-account-credentials.json');
$client->addScope("https://www.googleapis.com/auth/drive");
$client->setSubject('youremail@gmail.com');
// continue to upload with Drive forever and ever... |
@bshaffer Thanks a lot for your detailed explanation! 👍 |
@bshaffer Now it says 'Requested client not authorized', even though I just pushed accept and copied the code and put it in the fetch function! |
@AndreyNazarchuk does the authorized application show up in your authorized applications? If not, I assume the access token was not fetched as expected. |
@bshaffer It did once, then after that I cant get it to show up again. |
@bshaffer How can I ensure token is fetched as expected? |
If the line |
@bshaffer Your post saved my day!! I've been trying all kinds of things, and couldnt make it work as I wanted to, and your step-by-step guide has just been wonderful!! Thank YOU! After almost 10 hours of work with this: THANK YOU ! |
hey, I am bigner in PHP and i want to upload csv/pdf file in drive programatically. namespace GoogleApi;
require 'GoogleApi/Client.php';
require 'GoogleApi/Contrib/apiOauth2Service.php';
require 'GoogleApi/Contrib/apiDriveService.php';
$pdfFile = 'files/pdf.pdf';
//$pdfFile = 'files/report.csv';
// API Console: https://code.google.com/apis/console/
// Create an API project ("web applications") and put the client id and client secret in config.ini.
// Set up the Drive SDK in the API console.
// Create a Chrome extension, set the "container" and "api_console_project_id" parameters, and install it.
$config = parse_ini_file('php.ini'); // client_id, client_secret
// initialise the client
$client = new Client();
$client->setUseObjects(true);
$client->setAuthClass('apiOAuth2');
$client->setScopes(array('https://www.googleapis.com/auth/drive.file'));
$client->setClientId($config['932339130642-s0t08hcbtvbnp8e6e9f9q7c2gdkba7hn.apps.googleusercontent.com']);
$client->setClientSecret($config['u1cKtQ2b6iQ5zZJF7zYNQrUu']);
$client->setRedirectUri($config['http://localhost/test/driver_data']);
$client->setAccessToken(authenticate($client));
// initialise the Google Drive service
$service = new apiDriveService($client);
// create and upload a new Google Drive file, including the data
try {
$file = new DriveFile;
$file->setTitle(basename($pdfFile));
$file->setMimeType('application/pdf');
$result = $service->files->insert($file, array('data' => file_get_contents($pdfFile), 'mimeType' => 'application/pdf'));
}
catch (Exception $e) {
print $e->getMessage();
}
print_r($result);
function authenticate($client, $file = 'token.json'){
if (file_exists($file)) return file_get_contents($file);
$_GET['code'] = ''; // insert the verification code here
// print the authentication URL
if (!$_GET['code']) {
exit($client->createAuthUrl(array('https://www.googleapis.com/auth/drive.file')) . "\n");
}
file_put_contents($file, $client->authenticate());
exit('Authentication saved to token.json - now run this script again.');
} I am using above code but not working...Please help me. Thank You |
@abhay1003 Please open a new issue with your question, as this issue has been closed. |
@bshaffer |
I am having the exact same issue.
Fatal error: Uncaught exception 'Google_Service_Exception' with message '{ Any idea what I might be doing wrong? Thanks. |
@ignacvucko; my best guess is;
Yes, this is probably so, but let me check tomorrow. |
My problem is even more strange. I managed to use the service account authorization, but all the files and folders that I create are hidden from My Drive dashboard. When I use this code
all the files & folders get listed like this |
Need to upload files on my PC to my own Google Drive (NOT to an end user's Google Drive) using PHP.
Is this possible using this SDK? If so, how?
The text was updated successfully, but these errors were encountered: