Authenticate your users

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

Your application must use OAuth 2.0 to authorize requests. No other authorization protocols are supported. If your application uses Google Sign-In, some aspects of authorization are handled for you.

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:

  1. 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.
  2. Activate the Drive API in the Google API Console. (If the API isn't listed in the API Console, then skip this step.)
  3. When your application needs access to user data, it asks Google for a particular scope of access.
  4. Google displays a consent screen to the user, asking them to authorize your application to request some of their data.
  5. If the user approves, then Google gives your application a short-lived access token.
  6. Your application requests user data, attaching the access token to the request.
  7. 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:

Scope Meaning 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 downloadUrl and thumbnail), but does not allow any access to read, download, write or upload file content. Does not support file creation, trashing or deletion. Also does not allow changing folders or sharing in order to prevent access escalation. Allows read-only access to file metadata (excluding downloadUrl and thumbnail), but does not allow any access to read or download file content. 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.

The scope strikes this balance in a practical way. Presumably, users only open or create a file with an app that they trust, for reasons they understand.

Requesting drive-wide read-only scope for an app

Read-only access to all of a user's Drive files ( 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.

Authenticate users

Use OAuth 2.0 and Google's identity APIs to authenticate new and existing users. Whenever you can avoid it, don't require users to create new passwords for your application.

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 state parameter 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.

Such a page could present a friendly parting message along with a link to your privacy policy and a meaningful explanation of why your app needs the information -- and, of course an option to be redirected back into the OAuth flow. If you can convince users that they'll get something valuable in return for access to their information, then they may decide to grant access.

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

Enviar comentários sobre…

Precisa de ajuda? Acesse nossa página de suporte.