I would first like to apologize if this question is a duplicate; perhaps I'm not being able to word what I need correctly because I really couldn't find a solution for my need after several hours of research...
I need logged users of my application (PHP, Laravel 5.7) to be able to download files from a specific folder on my Google Drive on my behalf; of course, without having my credentials, but I'm being unable to figure out how to make the request; all the examples I found were to allow users to download their own files after they authenticate with their credentials. Since Google Drive API uses OAUTH2, I thought I could just send a request to some endpoint with Authentication header bearing my token and the "file id" and the download would start but after scavenging the docs I still couldn't find this endpoint.
EDIT: Thanks to Osama Ibrahim for pointing to me the above endpoint.
Sorry for long text, tl;dr:
I need to know how my application can download a file on my drive programatically, requesting a token with my credentials without having to go through the google login UI.
Related
I'm trying to set up a page using XAMPP which will take a file uploaded by the user and upload it to a Google Drive I've created. I'm aware that this is likely a newbie issue, but this is my first time trying to use the Drive API
Most tutorials seem to use OAuth when creating the required credentials, however as I understand it this will require the user to log in with their Google account to give the page permission to store the file. (If this isn't the case let me know.) I would prefer if this wasn't the case, so I'd prefer to use a Service Account.
The issue is that the tutorials I've found seem to require a 'credentials.json' file, which I've gathered is provided by the OAuth system.
I've created a Service Account which has the relevant permissions for the Google Drive API. As far as I understand, OAuth shouldn't be required for my webpage as the files which will be stored will not contain any of the user's private information.
If you know how I can get the file uploads set up using the created Service Account it would be a big help.
I have a web application which is configured with my mail id. I have functionality of view google drive files in screen. Any user who logged in my site able to view the drive file (located in my mail id).But it is working only if the end user login with Gmail in the same browser. I want my application should work with my mail id that i configured in my system. Every user who logged into my site should able to view the drive file even though they don't have any permission to the drive file. Also I don't want my drive files should be public.so that any one with link cannot view the files. Am using google drive php client v3 for it. I didn't find any code samples regarding this. Any help/clarification is appreciated guys really
I have a web application which is configured with my mail id.
I would be intrested to know how you have achived this, I suspect that you mean you logged in your application using Oauth2 and are running it with the same Oauth2 token.
I have functionality of view google drive files in screen.
I suspect that you mean you can open this file in the Google drive web application. Probably using the weblink from the file.get response.
Any user who logged in my site able to view the drive file (located in my mail id). But it is working only if the end user login with Gmail in the same browser.
Just because your application using the google drive api using your credentials with Oauth2 does not mean that the weblink will open for anyone but you or someone who has been granted access. Anyone who wants to view the file will still have either access to login to your account or have permission on the file from their own google drive account.
I do not recommend you give everyone the login and password to your google account.
I want my application should work with my mail id that i configured in my system.
This is not possible you might be able to get around it using a service account with domain wide delegation to a workspace account but its still only going to give you Google drive api access. It will not give a random person access to a file on your drive account. As the Google drive web application will only show a file that the user has access to.
Every user who logged into my site should able to view the drive file even though they don't have any permission to the drive file.
This may be possible if you set the file to public.
Also I don't want my drive files should be public.
You need to understand the difference between private and public data. Private data is owned by someone, and to get access to that you must have the permission of that person. In order to get permission to access a google drive file if you don't want to set it public you will need to share the file with this person.
so that any one with link cannot view the files.
In that case the file is private and you will need to share it with them.
Am using google drive php client v3 for it. I didn't find any code samples regarding this.
That is because what you want to do is not possible.
comments.
In order to access a file stored on Google drive someone needs to have access to it. Even if you set the file public I suspect the user will still need to be logged into their own google drive account.
Google drive is a file storage system its not meant for file hosting in your application.
Even using a service account will not help you in this case as it sounds like you want to open the file in the google drive web application.
I have a small site/web application where registered users can see some files that I periodically prepare for them; I store these files in separate directories and every user can only see and download their own files.
Now, I'm moving these directories to Google Drive and I would keep functionalities like file listing and downloading.
Reading the official docs I found some interesting examples about file download/upload, but all of them talk about authorization that user must give in order to access to their Drive and this is not what I'm looking for, I don't want access to their Drive!
As I can see, the doc is not contempling the case that someone may want to give (read-only) access to own Drive. But maybe I'm missing something...
Basically, I need the list of files on my Drive account and, if requested, a way to download one (or more). The only one that has the auth to Drive account is my server, users will send request from it.
Is it possible? I think yes, but I can't find any docs about that. How can I authorize my server?
I think you're looking for the Using OAuth 2.0 for Web Server Applications where you'll be using service accounts to achieve your goal.
"This document explains how web server applications use the Google API
Client Library for PHP to implement OAuth 2.0 authorization to access
Google APIs. OAuth 2.0 allows users to share specific data with an
application while keeping their usernames, passwords, and other
information private. For example, an application can use OAuth 2.0 to
obtain permission from users to store files in their Google Drives."
The snippets are in PHP too, so it works in your favor.
after days of searching, I have concluded that the right question would be better. I am working on app backend. I need it to backup some files regularly on Google Drive. Do I understand it correctly that I need Google For Work account so I can use Server2Server?
According to this
https://developers.google.com/api-client-library/php/auth/service-accounts
Google wants me to grant access only via Google For Work account. Have I missed anything if I just want my web based php script executed by cron to backup few files on Google Drive? Is there any other option?
Thank you!
The account that Google Drive connects to is determined by the content of your OAuth Access Token. If you'd like to do server-to-drive, where the Drive account is your own, see the instructions in How do I authorise an app (web or installed) without user intervention? (canonical ?)
I am trying to upload files into Google Drive using the Google Drive API but the problem is that I have to be logged-in every time. I searched a lot but no luck.
Is there any way I can do authentication process without manual and logged-in intervention?
If you are doing it correctly, you should only have to authenticate once save the refresh token, and use the refresh token in all your other calls.
Second option would be a service account but that is tricky as files are uploaded to the service account, unless you give it permission to upload to your own Google drive account.