google drive api php
PHP Quickstart
Complete the steps described in the rest of this page to create a simple PHP command-line application that makes requests to the Drive API.
Prerequisites
To run this quickstart, you need the following prerequisites:
Step 1: Install the Google Client Library
See the library’s installation page for the alternative installation options.
Step 2: Set up the sample
Create a file named quickstart.php in your working directory and copy in the following code:
Step 3: Run the sample
Run the sample using the following command:
The first time you run the sample, it prompts you to authorize access:
Browse to the provided URL in your web browser.
If you are not already signed in to your Google account, you are be prompted to sign in. If you are signed in to multiple Google accounts, you are asked to select one account to use for the authorization.
Notes
Troubleshooting
This section describes some common issues that you may encounter while attempting to run this quickstart and suggests possible solutions.
SSL certificate problem: unable to get local issuer certificate
This error indicates that the underlying HTTP libraries can’t find a certificate store, and are therefore unable to setup the SSL connection to Google’s servers. See the Guzzle library’s documentation for information on how to setup a certificate store on your machine.
Uncaught InvalidArgumentException: missing the required redirect URI
This error occurs when the credentials.json file used contains a client ID of the wrong type. This code requires an OAuth client ID of type Other, which will be created for you when using the button in Step 1. If creating your own client ID please ensure you select the correct type.
This app isn’t verified
If the OAuth consent screen displays the warning «This app isn’t verified,» your app is requesting scopes that provide access to sensitive user data. If your application uses sensitive scopes, your your app must go through the verification process to remove that warning and other limitations. During the development phase you can continue past this warning by clicking Advanced > Go to
Further reading
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Use Google Drive API «without» authentication screen from php
This is the scenario I’m facing. I have a php powered web application from which I need to access some links of files that I have on my Google Drive account. I ONLY need to access to my files in MY account, because I store same images that I have to serve to different clients. I tried to look into the Google Drive API documentation, but I don’t need OAuth and the consent screen. The app should access MY Google Drive account in «background», just to retrieve some informations that it has to present to the clients, without them having to do anything.
I read very similar questions here on StackOverflow, but none of those really helped me. some mention the use of Service Accounts, other say that they are not useful for this purpose.
Could you help me to understand which is the best approach in this scenario? Thanks a lot in advance!
1 Answer 1
What you are a looking for is a service account. Service accounts are dummy users they actually have their own google drive account. Because of the fact that they are a user you can then share files with them like you would any other user. Once the file is shared with the service account it has access to it and can then use it like you would with Oauth2 when you login only you wont need to login and consent to the access because you have already configured the access.
Introduction to Google Drive API
The Google Drive API allows you to create apps that leverage Google Drive cloud storage. You can develop applications that integrate with Google Drive, and create robust functionality in your application using Google Drive API.
This diagram shows the relationship between your Google Drive app, Google Drive, and Google Drive API:
Figure 1. Google Drive API Relationship Diagram
These terms define the key components shown in Figure 1:
Google Drive Google’s cloud file storage service that provides users with a personal storage space, called My Drive, and the option to access collaborative shared folders, called shared drives. Google Drive API The REST API that allows you to leverage Google Drive storage from within your app. Google Drive app An app that leverages Google Drive as its storage solution. Google Drive UI Google’s user interface that manages files stored on Google Drive. If your app is an editor-type app, such as a spreadsheet or word processor app, you can integrate with the Drive UI to create and open files within your app. My Drive A Google Drive storage location that a specific user owns. Files stored on My Drive can be shared with other users, but ownership of the content remains specific to an individual user. OAuth 2.0 The authorization protocol that Google Drive API requires to authenticate your app users. If your application uses Google Sign-in, it handles the OAuth 2.0 flow and application access tokens. Shared drive A Google Drive storage location that owns files that multiple users share to collaborate. Any user with access to a shared drive has access to all files it contains. Users can also be granted access to individual files inside the shared drive.
What can you do with Google Drive API?
You can use Google Drive API to:
Next steps
To get started with Google Drive API:
Try a quickstart to learn how to configure and run a Google Drive app.
Learn how to enable the Drive API within your app’s Cloud Platform project.
Learn how to authenticate your users so they can access the Google Drive API with your Google Drive app.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Google drive api php
One specific approach to batching, the Global HTTP Batch endpoint ( www.googleapis.com/batch ), was shut down on August 12, 2020, as announced on the Google Developers blog. Other approaches to batching still work, as documented on the rest of this page. If your code uses the Global HTTP Batch endpoint, then see the blog post for instructions on transitioning to use other approaches, such as API-specific HTTP batch endpoints ( www.googleapis.com/batch/ API / VERSION ).
This document shows how to batch API calls together to reduce the number of HTTP connections your client has to make.
This document is specifically about making a batch request by sending an HTTP request. If, instead, you’re using a Google client library to make a batch request, see the client library’s documentation.
Overview
Each HTTP connection that your client makes results in a certain amount of overhead. The Drive API supports batching, to allow your client to put several API calls into a single HTTP request.
Examples of situations when you might want to use batching:
In each case, instead of sending each call separately, you can group them together into a single HTTP request. All the inner requests must go to the same Google API.
You’re limited to 100 calls in a single batch request. If you need to make more calls than that, use multiple batch requests.
Note: The batch system for the Drive API uses the same syntax as the OData batch processing system, but the semantics differ.
Note: Batch requests with more than 100 calls may result in an error.
Note: There is an 8000 character limit on the length of the URL for each inner request.
Note: Currently, Google Drive does not support batch operations for media, either for upload or download.
Batch details
Note: A set of n requests batched together counts toward your usage limit as n requests, not as one request. The batch request is taken apart into a set of requests before processing.
Format of a batch request
A batch request is a single standard HTTP request containing multiple Drive API calls, using the multipart/mixed content type. Within that main HTTP request, each of the parts contains a nested HTTP request.
Each part begins with its own Content-Type: application/http HTTP header. It can also have an optional Content-ID header. However, the part headers are just there to mark the beginning of the part; they’re separate from the nested request. After the server unwraps the batch request into separate requests, the part headers are ignored.
The body of each part is itself a complete HTTP request, with its own verb, URL, headers, and body. The HTTP request must only contain the path portion of the URL; full URLs are not allowed in batch requests.
For example, if you provide an Authorization header for a specific call, then that header applies only to that call. If you provide an Authorization header for the outer request, then that header applies to all of the individual calls unless they override it with Authorization headers of their own.
When the server receives the batched request, it applies the outer request’s query parameters and headers (as appropriate) to each part, and then treats each part as if it were a separate HTTP request.
Response to a batch request
The server’s response is a single standard HTTP response with a multipart/mixed content type; each part is the response to one of the requests in the batched request, in the same order as the requests.
Like the parts in the request, each response part contains a complete HTTP response, including a status code, headers, and body. And like the parts in the request, each response part is preceded by a Content-Type header that marks the beginning of the part.
Note: The server may perform your calls in any order. Don’t count on their being executed in the order in which you specified them. If you want to ensure that two calls occur in a given order, you can’t send them in a single request; instead, send the first one by itself, then wait for the response to the first one before sending the second one.
Example
The following example shows the use of batching with the Drive API.
Example batch request
Example batch response
This is the response to the example request in the previous section.
Using client libraries
The following code samples demonstrate how to make batch requests using the Google APIs client libraries. See the respective quickstart guides for more information on how to install the libraries and set them up.
Files: list
Lists the user’s files. Try it now or see an example.
Request
HTTP request
Parameters
Parameter name | Value | Description |
---|---|---|
Optional query parameters | ||
corpora | string | Groupings of files to which the query applies. Supported groupings are: ‘user’ (files created by, opened by, or shared directly with the user), ‘drive’ (files in the specified shared drive as indicated by the ‘driveId’), ‘domain’ (files shared to the user’s domain), and ‘allDrives’ (A combination of ‘user’ and ‘drive’ for all drives where the user is a member). When able, use ‘user’ or ‘drive’, instead of ‘allDrives’, for efficiency. |
corpus | string | Warning: This item is deprecated. The body of items (files/documents) to which the query applies. Deprecated: use ‘corpora’ instead. |
Authorization
This request allows authorization with at least one of the following scopes:
Scope |
---|
https://www.googleapis.com/auth/drive |
https://www.googleapis.com/auth/drive.file |
https://www.googleapis.com/auth/drive.readonly |
https://www.googleapis.com/auth/drive.metadata.readonly |
https://www.googleapis.com/auth/drive.appdata |
https://www.googleapis.com/auth/drive.apps.readonly |
https://www.googleapis.com/auth/drive.metadata |
https://www.googleapis.com/auth/drive.photos.readonly |
Some scopes are restricted and require a security assessment for your app to use them. For more information, see the authentication and authorization page.
Request body
Do not supply a request body with this method.
Response
If successful, this method returns a response body with the following structure:
Property name | Value | Description | Notes |
---|---|---|---|
kind | string | This is always drive#fileList. | |
etag | etag | The ETag of the list. | |
selfLink | string | A link back to this list. | |
nextPageToken | string | The page token for the next page of files. This will be absent if the end of the files list has been reached. If the token is rejected for any reason, it should be discarded, and pagination should be restarted from the first page of results. | |
nextLink | string | A link to the next page of files. | |
items[] | list | The list of files. If nextPageToken is populated, then this list may be incomplete and an additional page of results should be fetched. | |
incompleteSearch | boolean | Whether the search process was incomplete. If true, then some search results may be missing, since all documents were not searched. This may occur when searching multiple drives with the «allDrives» corpora, but all corpora could not be searched. When this happens, it is suggested that clients narrow their query by choosing a different corpus such as «default» or «drive». |
Examples
Note: The code examples available for this method do not represent all supported programming languages (see the client libraries page for a list of supported languages).