Every request your application sends to the Drive API must include an authorization token. The token also identifies your application to Google.
About authorization protocols
Authorizing requests with OAuth 2.0
All requests to the Drive API must be authorized by an authenticated user.
The details of the authorization process, or "flow," for OAuth 2.0 vary somewhat depending on what kind of application you're writing. The following general process applies to all application types:
- When you create your application, you register it using the Google API Console. Google then provides information you'll need later, such as a client ID and a client secret.
- Activate the Drive API in the Google API Console. (If the API isn't listed in the API Console, then skip this step.)
- When your application needs access to user data, it asks Google for a particular scope of access.
- Google displays a consent screen to the user, asking them to authorize your application to request some of their data.
- If the user approves, then Google gives your application a short-lived access token.
- Your application requests user data, attaching the access token to the request.
- If Google determines that your request and the token are valid, it returns the requested data.
Some flows include additional steps, such as using refresh tokens to acquire new access tokens. For detailed information about flows for various types of applications, see Google's OAuth 2.0 documentation.
Here's the OAuth 2.0 scope information for the Drive API:
||Full, permissive scope to access all of a user's files, excluding the Application Data folder. Request this scope only when it is strictly necessary.|
||Allows read-only access to file metadata and file content.|
||Allows access to the Application Data folder.|
||Per-file access to files created or opened by the app. File authorization is granted on a per-user basis and is revoked when the user deauthorizes the app.|
||Special scope used to let users approve installation of an app.|
||Allows read-write access to file metadata (excluding
||Allows read-only access to file metadata (excluding
||Allows access to Apps Script files.|
||Allows read-only access to installed apps.|
If your app requires access to any other Google APIs, you can add those scopes as well. For more information about Google API scopes, see Using OAuth 2.0 to Access Google APIs.
What scope or scopes does my app need?
As a general rule, choose the most restrictive scope possible, and avoid requesting scopes that your app does not actually need. Users more readily grant access to limited, clearly described scopes. Conversely, users may hesitate to grant broad access to their files unless they truly trust your app and understand why it needs the information.
strikes this balance in a practical way. Presumably, users only
open or create a file with an app that they trust, for reasons they
Requesting drive-wide read-only scope for an app
Read-only access to all of a user's Drive files
https://www.googleapis.com/auth/drive.readonly) may be useful
for certain apps. For instance, a photo browser might need to
reorganize image files in a unique presentation order for a
slideshow, or a mobile app might have to work around unique display
constraints without needing to write anything. For apps that only
need to read file metadata for all files in Drive, there's
To request access using OAuth 2.0, your application needs the scope information, as well as information that Google supplies when you register your application (such as the client ID and the client secret).
Tip: The Google APIs client libraries can handle some of the authorization process for you. They are available for a variety of programming languages; check the page with libraries and samples for more details.
Authorize user access
The OAuth 2.0 framework for Drive apps, as described in About Authorization, solves a lot of authorization challenges. However, some areas merit special attention. All Drive Apps should:
- Treat all Create New and Open with events like potential logins. Some users
may have multiple accounts. If the user ID in the
stateparameter does not match the current session, you may need to end the current session for your app and log in as the requested user.
Handle declined access requests gracefully
Prepare for cases where users click No Thanks in the OAuth dialog and decline access to your app. Rather than
returning an unsightly error page (which is the result if you take no mitigating action),
you can catch the
access_denied string in the query parameter
error and display a reasonable page in response.
Perform G Suite Domain-Wide Delegation of Authority
In enterprise applications you may want to programmatically access users data without any manual authorization on their part. In G Suite domains, the domain administrator can grant to third party applications domain-wide access to its users' data — this is referred as domain-wide delegation of authority. To delegate authority this way, domain administrators can use service accounts with OAuth 2.0.
For additional detailed information, see Using OAuth 2.0 for Server to Server Applications