Confuse after quickstart - google-drive-api

I am pretty new to Google Drive SDk. After spend hours on the google drive sdk document and quick start app, I think I have known some basic concepts about it.
However, I have a easy but crucial question: how to integrate my code with Drive UI.
I mean I know I should enable Google Drive API and Google Drive SDK and do some configure staff. And I did.
I set up the MIME type that my app should be able to open and create, but it doesn't work. (I used the most easy one: plain/text: txt).
In fact, I think this is not the most serious problem. I am confused about how to integrate my code with Drive UI.
I mean the quick start app works fine, but it just simply upload a file. What if I want to perform some complicate action after user open a certain file with my app?
Ideally, I think it should redirect the user to my open URL and all the actions should go with that site, right?
(I am using Python, in case that could make any differences)
I am really confused. Excuse my chaos description, please.
Thanks in advance!
EDIT:
Thanks for the answering below! I think I need to detailed explain my needs from google drive sdk.
Basically, I want the user who have installed my app be able to open a certain file (which should be spreadsheet or txt) on google drive. Then it will redirect the user to my given Open URL (which I create a site by using google site). On that site, user should be able to view and import the content of file into a certain database. None of the operations above require any technique knowledges. I certainly can do this by writing python script and run it locally to import the content of files on google drive. However, there are two issues: first, I need to run it locally. I mean I did use the google drive api, but I didn't explore the real benefit of api; second, it is still a command line script, and has to be ran on linux.
I hope I have illustrated my request. And I appreciate any help in advance!

Install the client library:
pip install --upgrade google-api-python-client
Acquire an access token as explained on the Python Quickstart example. Init a Drive service to be able to talk to the API and authorize http transporter with your credentials. You need to make requests to get file metadata and file contents.
Retrieve file metadata:
http = httplib2.Http()
http = credentials.authorize(http)
drive_service = build('drive', 'v2', http=http)
f = drive_service.files().get(fileId=file_id).execute()
Retrieve file contents:
downloadUrl = f.get('downloadUrl')
f['content'] = drive_service._http.request(downloadUrl)
(Don't forget content retrieving request should be authorized and authenticated, that's why we use drive_service._http to make that request.

Related

Google Drive API 403 for Google Docs files specifically

I have been running GDrive API v3 using our Node.js API for awhile now (1+ year), and everything has been functional for creating (drive.files.create) & reading (drive.files.get) DOCX files through the Google Drive API logic using a service account. Everything has been working perfectly fine.
Today I am trying to extend our infrastructure to handle some generic Google Docs files (rather than the existing DOCX stored within GDrive) using the same service account, but the GDrive API is now returning a 403 "Forbidden" error for these files specifically. I can't seem to figure out why there would be a difference in permissions for two file formats that are within the same service account (is the owner of both) and are using the same OAuth token system that I set up originally. The service account itself created the file, so I am a little shocked that it can't then access the same file using the GDrive API.
My question is, is it not possible to use GDrive API to access Google Doc files, even when the files are within a GDrive folder? I can't seem to find any definitive information online on whether we specifically have to use the Google Docs API to access Docs files (seems silly if true). If it is possible, is there an alternative SCOPE that I need to assign to this service account to access Google Docs items specifically? Right now the only SCOPE assigned is https://www.googleapis.com/auth/drive, which according to documentation should be enough to access all of our GDrive files.
I can share some code, but there is not exactly a lot more than what I explained above. I would like to not have to entirely re-do my GDrive OAuth permissions if possible, as that took me a long time in the first place, but if that is the only way, I would love to hear suggestions.
Thanks!

requests: Get last modified time of Google Doc or Sheet?

I want to download a Google Sheet (and/or Doc, or Colab Notebook) from an "Anyone can View" sharing URL, if the file is newer than my local copy. To do that, I need to find out when the remote file was last modified. Which I thought shouldn't be hard.
There are threads explaining how to do this for regular files on websites that make use of the HTML Last-Modified property, but Google doesn't provide this field in its headers. It provides a Date: but that's just the download date/time that updates every moment.
I see threads about doing this from within the Doc or Sheet itself. My question is not about that. I'm talking about getting the info remotely by running a python script on my local machine.
I see a thread about using the Google Drive API v3, but....is it really necessary to go through all that (e.g. install oauth, register an API key, etc. effectively create an entire Google app *) just to find out when a publicly-available file was last modified? Is there an easier way?
Thanks!
EDIT: * I started down the road of Google Drive API but I find it confusing and overwhelming. It's like they think I'm trying to create an app for general users for the Android Store, instead of just myself. (??)

Google Drive SDK: using drive.install scope to integrate web app into "open with" UI not working

I have tried without much success to integrate my web application within the Google Drive UI (to open files). I cannot get the app to install using the OAuth scope https://www.googleapis.com/auth/drive.install.
I was wondering if anybody could share their Drive SDK configuration to see if I missed something. I tried installing my app using the Google API Console by selecting the Drive Integration to automatically authenticate. I also tried manually making the OAuth request using the google-api-client gem.
I always get the permissions dialog and I can see the following being requested: Add itself to Google Drive. I also get redirected back to my application like a normal OAuth request, but the application is never listed in Google Drive.
I have been requesting the following scopes:
https://www.googleapis.com/auth/drive.install
https://www.googleapis.com/auth/drive.file
profile
user
Allowing Import and these Secondary MIME Types:
application/vnd.google.drive.ext-type.html
text/html
I followed the documentation here: https://developers.google.com/drive/web/enable-sdk
Thanks in advance for any help!!
I was able to solve this by modifying my OAuth flow. It wasn't exchanging the authentication code for an access token before. Once I made it to that point everything worked as expected.
My issue was, that I didn't have this set:
Allowing Import and these Secondary MIME Types:
application/vnd.google.drive.ext-type.html text/html
I'm working with Google Spreadsheets, so I need a mime type that a sheet can be converted to, such as text/csv, see https://developers.google.com/drive/api/v3/integrate-open#open_and_convert_google_docs_in_your_app

Incorporate Google Drive solution in existing Android app project?

I have an existing Android app project which has been already published on the Google Play, and now I would like to incorporate in my app the possibility to use the Google Drive possibilities in order to download *.jpg files directly to the sd card when the app is opened or load for the first time. Like this I liberate the internal memories of the phones where this app is installed (All the images are stored in the resources of the app actually which is >~ 20MB). I have read a lot of forums concerning the Google Drive SDK or API subjects and I can say that I'm a little bit lost on the manner to manage it for my present app. Some ideas or suggestions would be very very very appreciated.
Thank you very much).
Please see this question for the basics on how to integrate your Android app with Google Drive SDK: Access to Google Drive from self implemented Android application
In it, I reference a good Google+ tutorial on how to get started, and it's easy enough to take the concepts and apply them to an existing app. (That's how I did it too.) Before you get started, sign up for the Google APIs (https://code.google.com/apis/console/), get into the API Console, and turn on both the Google Drive API and Drive SDK. This'll make coding go a lot smoother. Let me know if you have any problems.
One addendum for your specific case relating to *.jpg files is you could set a MIME type for fetching strictly the pictures. It'd go something like this:
request = service.files().list(); // .setQ("mimeType=\"text/plain\"");
Unfortunately setQ is commented out because it would fail to return any results when I used it. However, you may have better luck with the image/jpeg MIME type.

I am facing "The authenticated user has not installed the app with client id" error even after installing the app [duplicate]

I'm working on a Google Drive interface for Emacs. The concept is that Emacs could provide a platform-agnostic way to load, modify and save text documents stored in Google Drive. I've registered my app and can authenticate with OAuth2 and get a file listing with the Docs List API, but when I try to execute an Insert with the Google Drive API, I see an error:
"The authenticated user has not installed the app with client id ..."
Reading further, it seems I need to publish my Emacs application in the Chrome Web Store to get access to the Drive API. That doesn't make sense to me...I noticed that there is a FUSE project in development for Google Drive, which suggests that native development is possible. When I skimmed the code, however, I didn't see a Chrome Web Store component to getting it working.
Am I trying to misuse the API, or is there an route to make this work that makes more sense?
EDIT:
According to Ali Afshar, of the Google Drive team, installation is no longer required to use this API. So what follows may no longer be relevant, but will be left for historical purposes.
So, first off the API does not support application development in the sense that we are both doing it, I wouldn't use the word native though. The good news is I have been doing some research and Google Drive is really just a rebranding of Google Docs. So the Google Docs API could be a good choice as well for the same purposes.
Anyway, here's the steps to solve the error: "The authenticated user has not installed the app with client id ..." Which is a 403 error, for the sake of this answer. These steps assume you have set up an app in the chrome web store as is required, and installed it. I am working on my local machine too, with my project: http://github.com/tom-dignan/gdrive-cli which I have gotten past this error, so I think you should keep plugging away at your emacs version, because I think we can make this work.
a. Open the Google APIs console.
b. Confirm you've already enabled the apis under "API Access" both the API and SDK for Google drive should be enabled. There you get your client secrets/api keys and such. I am almost positive you've done this already, so go ahead to C. (this is here for others who may have missed it)
c. In the left navigation bar, under "Drive SDK" you will need to do the following:
Add a "Support URL" (required)
Add at least a small 16x16 application icon (required)
Add "OAuth Client ID (Required)" under Drive Integration (I was just tinkering and this seems to be the key field.)
Add "Open URL (Required) URL to open for your app from the google drive UI."
Check off "Multiple File Support"
Add some MIME types and file extensions, "text/plain", and txt for example
Add the the auth scopes:
https://www.googleapis.com/auth/userinfo.email
https://www.googleapis.com/auth/userinfo.profile
Don't bother trying to add the auth scopes for Google Docs here, because it won't work. Google does not want us to use it that way because files that drive apps create should be private to that app. Integration with Google Docs will have to be separate.
Now I know you must be thinking "why do I have to add some of these..." It's because the form makes them required fields. In mine, I put a couple URLs that point to static HTML pages.
Once you've done the above, clean up your state and reinstall your chrome app. Then try your code again, and it should stop giving you a 403.