Использование OAuth 2.0 для приложений веб-сервера

В этом документе объясняется, как приложения веб-сервера используют клиентские библиотеки Google API или конечные точки Google OAuth 2.0 для реализации авторизации OAuth 2.0 для доступа к API Google.

OAuth 2.0 позволяет пользователям обмениваться определенными данными с приложением, сохраняя при этом свои имена пользователей, пароли и другую информацию конфиденциальной. Например, приложение может использовать OAuth 2.0 для получения от пользователей разрешения на хранение файлов на их Google Дисках.

Этот поток OAuth 2.0 предназначен специально для авторизации пользователей. Он предназначен для приложений, которые могут хранить конфиденциальную информацию и поддерживать состояние. Правильно авторизованное приложение веб-сервера может получить доступ к API во время взаимодействия пользователя с приложением или после того, как пользователь покинул приложение.

Приложения веб-сервера часто также используют учетные записи служб для авторизации запросов API, особенно при вызове облачных API для доступа к данным проекта, а не к данным пользователя. Приложения веб-сервера могут использовать учетные записи служб вместе с авторизацией пользователя.

Клиентские библиотеки

В примерах на этой странице для конкретных языков используются клиентские библиотеки Google API для реализации авторизации OAuth 2.0. Чтобы запустить примеры кода, сначала необходимо установить клиентскую библиотеку для вашего языка.

Когда вы используете клиентскую библиотеку Google API для обработки потока OAuth 2.0 вашего приложения, клиентская библиотека выполняет множество действий, которые в противном случае приложению пришлось бы выполнять самостоятельно. Например, он определяет, когда приложение может использовать или обновлять сохраненные токены доступа, а также когда приложение должно повторно получить согласие. Клиентская библиотека также генерирует правильные URL-адреса перенаправления и помогает реализовать обработчики перенаправления, которые обменивают коды авторизации на токены доступа.

Клиентские библиотеки Google API для серверных приложений доступны для следующих языков:

Предварительные условия

Включите API для вашего проекта

Любое приложение, которое вызывает API Google, должно включить эти API в API Console.

Чтобы включить API для вашего проекта:

  1. Open the API Library в Google API Console.
  2. If prompted, select a project, or create a new one.
  3. API Library перечисляет все доступные API, сгруппированные по семействам продуктов и популярности. Если API, который вы хотите включить, не отображается в списке, воспользуйтесь поиском, чтобы найти его, или нажмите «Просмотреть все» в семействе продуктов, к которому он принадлежит.
  4. Выберите API, который вы хотите включить, затем нажмите кнопку «Включить» .
  5. If prompted, enable billing.
  6. If prompted, read and accept the API's Terms of Service.

Создать учетные данные для авторизации

Любое приложение, использующее OAuth 2.0 для доступа к API Google, должно иметь учетные данные авторизации, которые идентифицируют приложение на сервере Google OAuth 2.0. Следующие шаги объясняют, как создать учетные данные для вашего проекта. Затем ваши приложения смогут использовать учетные данные для доступа к API, которые вы включили для этого проекта.

  1. Go to the Credentials page.
  2. Нажмите Создать клиента .
  3. Выберите тип приложения веб-приложения .
  4. Заполните форму и нажмите «Создать» . Приложения, использующие такие языки и платформы, как PHP, Java, Python, Ruby и .NET, должны указывать авторизованные URI перенаправления . URI перенаправления — это конечные точки, на которые сервер OAuth 2.0 может отправлять ответы. Эти конечные точки должны соответствовать правилам проверки Google .

    Для тестирования вы можете указать URI, ссылающиеся на локальный компьютер, например http://localhost:8080 . Имея это в виду, обратите внимание, что во всех примерах в этом документе в качестве URI перенаправления используется http://localhost:8080 .

    Мы рекомендуем вам спроектировать конечные точки аутентификации вашего приложения так, чтобы ваше приложение не предоставляло коды авторизации другим ресурсам на странице.

После создания учетных данных загрузите файл client_secret.json из API Console. Надежно храните файл в месте, доступном только вашему приложению.

Определить области доступа

Области позволяют вашему приложению запрашивать доступ только к тем ресурсам, которые ему необходимы, а также позволяют пользователям контролировать объем доступа, который они предоставляют вашему приложению. Таким образом, может существовать обратная зависимость между количеством запрошенных областей и вероятностью получения согласия пользователя.

Прежде чем приступить к реализации авторизации OAuth 2.0, мы рекомендуем вам определить области, для доступа к которым вашему приложению потребуется разрешение.

Мы также рекомендуем, чтобы ваше приложение запрашивало доступ к областям авторизации посредством процесса добавочной авторизации , в котором ваше приложение запрашивает доступ к пользовательским данным в контексте. Эта передовая практика помогает пользователям легче понять, почему вашему приложению нужен запрашиваемый доступ.

Документ «Области API OAuth 2.0» содержит полный список областей, которые вы можете использовать для доступа к API Google.

Языковые требования

Чтобы запустить любой из примеров кода в этом документе, вам потребуется учетная запись Google, доступ к Интернету и веб-браузер. Если вы используете одну из клиентских библиотек API, также ознакомьтесь с требованиями для конкретного языка ниже.

PHP

Чтобы запустить примеры кода PHP из этого документа, вам потребуется:

  • PHP 8.0 или более поздней версии с установленным интерфейсом командной строки (CLI) и расширением JSON.
  • Инструмент управления зависимостями Composer .
  • Клиентская библиотека API Google для PHP:

    composer require google/apiclient:^2.15.0

Дополнительную информацию см. в разделе Клиентская библиотека API Google для PHP .

Питон

Чтобы запустить примеры кода Python из этого документа, вам потребуется:

  • Python 3.7 или выше
  • Инструмент управления пакетами pip .
  • Клиентская библиотека API Google для версии Python 2.0:
    pip install --upgrade google-api-python-client
  • google-auth , google-auth-oauthlib и google-auth-httplib2 для авторизации пользователя.
    pip install --upgrade google-auth google-auth-oauthlib google-auth-httplib2
  • Платформа веб-приложений Flask Python.
    pip install --upgrade flask
  • HTTP-библиотека requests .
    pip install --upgrade requests

Если вы не можете обновить Python, ознакомьтесь с примечанием к выпуску клиентской библиотеки Google API Python и соответствующим руководством по миграции.

Руби

Чтобы запустить примеры кода Ruby, приведенные в этом документе, вам потребуется:

  • Руби 2.6 или выше
  • Библиотека Google Auth для Ruby:

    gem install googleauth
  • Клиентские библиотеки для Google API Диска и Календаря:

    gem install google-apis-drive_v3 google-apis-calendar_v3
  • Платформа веб-приложений Sinatra Ruby.

    gem install sinatra

Node.js

Чтобы запустить примеры кода Node.js из этого документа, вам потребуется:

  • Поддерживаемый LTS, активный LTS или текущая версия Node.js.
  • Клиент Google API Node.js:

    npm install googleapis crypto express express-session

HTTP/REST

Вам не нужно устанавливать какие-либо библиотеки, чтобы иметь возможность напрямую вызывать конечные точки OAuth 2.0.

Получение токенов доступа OAuth 2.0

Следующие шаги показывают, как ваше приложение взаимодействует с сервером OAuth 2.0 Google, чтобы получить согласие пользователя на выполнение запроса API от имени пользователя. Ваше приложение должно получить это согласие, прежде чем оно сможет выполнить запрос Google API, требующий авторизации пользователя.

В приведенном ниже списке кратко суммированы эти шаги:

  1. Ваше приложение определяет необходимые ему разрешения.
  2. Ваше приложение перенаправляет пользователя в Google вместе со списком запрошенных разрешений.
  3. Пользователь решает, предоставлять ли разрешения вашему приложению.
  4. Ваше приложение узнает, что решил пользователь.
  5. Если пользователь предоставил запрошенные разрешения, ваше приложение получает токены, необходимые для выполнения запросов API от имени пользователя.

Шаг 1: Установите параметры авторизации

Ваш первый шаг — создать запрос на авторизацию. Этот запрос устанавливает параметры, которые идентифицируют ваше приложение и определяют разрешения, которые пользователю будет предложено предоставить вашему приложению.

  • Если вы используете клиентскую библиотеку Google для аутентификации и авторизации OAuth 2.0, вы создаете и настраиваете объект, который определяет эти параметры.
  • Если вы вызовете конечную точку Google OAuth 2.0 напрямую, вы создадите URL-адрес и установите параметры для этого URL-адреса.

На вкладках ниже определяются поддерживаемые параметры авторизации для приложений веб-сервера. В примерах для конкретного языка также показано, как использовать клиентскую библиотеку или библиотеку авторизации для настройки объекта, который задает эти параметры.

PHP

Следующий фрагмент кода создает объект Google\Client() , который определяет параметры запроса авторизации.

Этот объект использует информацию из вашего файла client_secret.json для идентификации вашего приложения. (Подробнее об этом файле см. в разделе Создание учетных данных для авторизации .) Объект также определяет области, к которым ваше приложение запрашивает разрешение на доступ, и URL-адрес конечной точки аутентификации вашего приложения, которая будет обрабатывать ответ от сервера Google OAuth 2.0. Наконец, код устанавливает необязательные параметры access_type и include_granted_scopes .

Например, этот код запрашивает автономный доступ только для чтения к метаданным Google Диска и событиям календаря пользователя:

use Google\Client;

$client = new Client();

// Required, call the setAuthConfig function to load authorization credentials from
// client_secret.json file.
$client->setAuthConfig('client_secret.json');

// Required, to set the scope value, call the addScope function
$client->addScope([Google\Service\Drive::DRIVE_METADATA_READONLY, Google\Service\Calendar::CALENDAR_READONLY]);

// Required, call the setRedirectUri function to specify a valid redirect URI for the
// provided client_id
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php');

// Recommended, offline access will give you both an access and refresh token so that
// your app can refresh the access token without user interaction.
$client->setAccessType('offline');

// Recommended, call the setState function. Using a state value can increase your assurance that
// an incoming connection is the result of an authentication request.
$client->setState($sample_passthrough_value);

// Optional, if your application knows which user is trying to authenticate, it can use this
// parameter to provide a hint to the Google Authentication Server.
$client->setLoginHint('hint@example.com');

// Optional, call the setPrompt function to set "consent" will prompt the user for consent
$client->setPrompt('consent');

// Optional, call the setIncludeGrantedScopes function with true to enable incremental
// authorization
$client->setIncludeGrantedScopes(true);

Питон

В следующем фрагменте кода используется модуль google-auth-oauthlib.flow для создания запроса авторизации.

Код создает объект Flow , который идентифицирует ваше приложение, используя информацию из файла client_secret.json , который вы скачали после создания учетных данных для авторизации . Этот объект также определяет области, к которым ваше приложение запрашивает разрешение на доступ, и URL-адрес конечной точки аутентификации вашего приложения, которая будет обрабатывать ответ от сервера Google OAuth 2.0. Наконец, код устанавливает необязательные параметры access_type и include_granted_scopes .

Например, этот код запрашивает автономный доступ только для чтения к метаданным Google Диска и событиям календаря пользователя:

import google.oauth2.credentials
import google_auth_oauthlib.flow

# Required, call the from_client_secrets_file method to retrieve the client ID from a
# client_secret.json file. The client ID (from that file) and access scopes are required. (You can
# also use the from_client_config method, which passes the client configuration as it originally
# appeared in a client secrets file but doesn't access the file itself.)
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file('client_secret.json',
    scopes=['https://www.googleapis.com/auth/drive.metadata.readonly',
            'https://www.googleapis.com/auth/calendar.readonly'])

# Required, indicate where the API server will redirect the user after the user completes
# the authorization flow. The redirect URI is required. The value must exactly
# match one of the authorized redirect URIs for the OAuth 2.0 client, which you
# configured in the API Console. If this value doesn't match an authorized URI,
# you will get a 'redirect_uri_mismatch' error.
flow.redirect_uri = 'https://www.example.com/oauth2callback'

# Generate URL for request to Google's OAuth 2.0 server.
# Use kwargs to set optional request parameters.
authorization_url, state = flow.authorization_url(
    # Recommended, enable offline access so that you can refresh an access token without
    # re-prompting the user for permission. Recommended for web server apps.
    access_type='offline',
    # Optional, enable incremental authorization. Recommended as a best practice.
    include_granted_scopes='true',
    # Optional, if your application knows which user is trying to authenticate, it can use this
    # parameter to provide a hint to the Google Authentication Server.
    login_hint='hint@example.com',
    # Optional, set prompt to 'consent' will prompt the user for consent
    prompt='consent')

Руби

Используйте созданный вами файл client_secrets.json для настройки объекта клиента в вашем приложении. При настройке клиентского объекта вы указываете области, к которым ваше приложение должно иметь доступ, а также URL-адрес конечной точки аутентификации вашего приложения, которая будет обрабатывать ответ от сервера OAuth 2.0.

Например, этот код запрашивает автономный доступ только для чтения к метаданным Google Диска и событиям календаря пользователя:

require 'googleauth'
require 'googleauth/web_user_authorizer'
require 'googleauth/stores/redis_token_store'

require 'google/apis/drive_v3'
require 'google/apis/calendar_v3'

# Required, call the from_file method to retrieve the client ID from a
# client_secret.json file.
client_id = Google::Auth::ClientId.from_file('/path/to/client_secret.json')

# Required, scope value 
# Access scopes for two non-Sign-In scopes: Read-only Drive activity and Google Calendar.
scope = ['Google::Apis::DriveV3::AUTH_DRIVE_METADATA_READONLY',
         'Google::Apis::CalendarV3::AUTH_CALENDAR_READONLY']

# Required, Authorizers require a storage instance to manage long term persistence of
# access and refresh tokens.
token_store = Google::Auth::Stores::RedisTokenStore.new(redis: Redis.new)

# Required, indicate where the API server will redirect the user after the user completes
# the authorization flow. The redirect URI is required. The value must exactly
# match one of the authorized redirect URIs for the OAuth 2.0 client, which you
# configured in the API Console. If this value doesn't match an authorized URI,
# you will get a 'redirect_uri_mismatch' error.
callback_uri = '/oauth2callback'

# To use OAuth2 authentication, we need access to a CLIENT_ID, CLIENT_SECRET, AND REDIRECT_URI
# from the client_secret.json file. To get these credentials for your application, visit
# https://console.cloud.google.com/apis/credentials.
authorizer = Google::Auth::WebUserAuthorizer.new(client_id, scope,
                                                token_store, callback_uri)

Ваше приложение использует объект клиента для выполнения операций OAuth 2.0, таких как создание URL-адресов запроса авторизации и применение токенов доступа к HTTP-запросам.

Node.js

Следующий фрагмент кода создает объект google.auth.OAuth2 , который определяет параметры запроса авторизации.

Этот объект использует информацию из вашего файла client_secret.json для идентификации вашего приложения. Чтобы запросить у пользователя разрешения на получение токена доступа, вы перенаправляете его на страницу согласия. Чтобы создать URL-адрес страницы согласия:

const {google} = require('googleapis');
const crypto = require('crypto');
const express = require('express');
const session = require('express-session');

/**
 * To use OAuth2 authentication, we need access to a CLIENT_ID, CLIENT_SECRET, AND REDIRECT_URI
 * from the client_secret.json file. To get these credentials for your application, visit
 * https://console.cloud.google.com/apis/credentials.
 */
const oauth2Client = new google.auth.OAuth2(
  YOUR_CLIENT_ID,
  YOUR_CLIENT_SECRET,
  YOUR_REDIRECT_URL
);

// Access scopes for two non-Sign-In scopes: Read-only Drive activity and Google Calendar.
const scopes = [
  'https://www.googleapis.com/auth/drive.metadata.readonly',
  'https://www.googleapis.com/auth/calendar.readonly'
];

// Generate a secure random state value.
const state = crypto.randomBytes(32).toString('hex');

// Store state in the session
req.session.state = state;

// Generate a url that asks permissions for the Drive activity and Google Calendar scope
const authorizationUrl = oauth2Client.generateAuthUrl({
  // 'online' (default) or 'offline' (gets refresh_token)
  access_type: 'offline',
  /** Pass in the scopes array defined above.
    * Alternatively, if only one scope is needed, you can pass a scope URL as a string */
  scope: scopes,
  // Enable incremental authorization. Recommended as a best practice.
  include_granted_scopes: true,
  // Include the state parameter to reduce the risk of CSRF attacks.
  state: state
});

Важное примечание . refresh_token возвращается только при первой авторизации. Подробнее здесь .

HTTP/REST

Конечная точка Google OAuth 2.0 находится по адресу https://accounts.google.com/o/oauth2/v2/auth . Эта конечная точка доступна только через HTTPS. Обычные HTTP-соединения отклоняются.

Сервер авторизации Google поддерживает следующие параметры строки запроса для приложений веб-сервера:

Параметры
client_id Необходимый

Идентификатор клиента для вашего приложения. Вы можете найти это значение в .

redirect_uri Необходимый

Определяет, куда сервер API перенаправляет пользователя после того, как пользователь завершает процесс авторизации. Значение должно точно совпадать с одним из авторизованных URI перенаправления для клиента OAuth 2.0, который вы настроили в настройках вашего клиента. . Если это значение не соответствует авторизованному URI перенаправления для предоставленного client_id , вы получите ошибку redirect_uri_mismatch .

Обратите внимание, что схема http или https , регистр и косая черта в конце (' / ') должны совпадать.

response_type Необходимый

Определяет, возвращает ли конечная точка Google OAuth 2.0 код авторизации.

Установите значение параметра для code приложений веб-сервера.

scope Необходимый

Список областей, разделенных пробелами, которые определяют ресурсы, к которым ваше приложение может получить доступ от имени пользователя. Эти значения указывают на экран согласия, который Google отображает пользователю.

Области позволяют вашему приложению запрашивать доступ только к тем ресурсам, которые ему необходимы, а также позволяют пользователям контролировать объем доступа, который они предоставляют вашему приложению. Таким образом, существует обратная зависимость между количеством запрашиваемых областей и вероятностью получения согласия пользователя.

Мы рекомендуем, чтобы ваше приложение запрашивало доступ к областям авторизации в контексте, когда это возможно. Запрашивая доступ к пользовательским данным в контексте посредством добавочной авторизации , вы помогаете пользователям легче понять, почему вашему приложению нужен запрашиваемый доступ.

access_type Рекомендуется

Указывает, может ли ваше приложение обновлять токены доступа, когда пользователь отсутствует в браузере. Допустимые значения параметров: online (значение по умолчанию) и offline .

Установите значение offline если вашему приложению необходимо обновлять токены доступа, когда пользователь отсутствует в браузере. Это метод обновления токенов доступа, описанный далее в этом документе. Это значение указывает серверу авторизации Google вернуть токен обновления и токен доступа при первом обмене вашим приложением кода авторизации на токены.

state Рекомендуется

Указывает любое строковое значение, которое ваше приложение использует для поддержания состояния между вашим запросом авторизации и ответом сервера авторизации. Сервер возвращает точное значение, которое вы отправляете в виде пары name=value в компоненте URL-запроса ( ? ) redirect_uri после того, как пользователь соглашается или отклоняет запрос доступа вашего приложения.

Этот параметр можно использовать для нескольких целей, например для направления пользователя к правильному ресурсу в вашем приложении, отправки одноразовых номеров и предотвращения подделки межсайтовых запросов. Поскольку ваш redirect_uri можно угадать, использование значения state может повысить вашу уверенность в том, что входящее соединение является результатом запроса аутентификации. Если вы генерируете случайную строку или кодируете хэш файла cookie или другое значение, фиксирующее состояние клиента, вы можете проверить ответ, чтобы дополнительно убедиться, что запрос и ответ исходят из одного и того же браузера, обеспечивая защиту от таких атак, как подделка межсайтового запроса . См. документацию OpenID Connect , где приведен пример создания и подтверждения токена state .

include_granted_scopes Необязательный

Позволяет приложениям использовать дополнительную авторизацию для запроса доступа к дополнительным областям в контексте. Если вы установите для этого параметра значение true и запрос на авторизацию будет предоставлен, то новый токен доступа также будет охватывать все области, к которым пользователь ранее предоставил доступ приложению. Примеры см. в разделе добавочной авторизации .

login_hint Необязательный

Если ваше приложение знает, какой пользователь пытается пройти аутентификацию, оно может использовать этот параметр, чтобы предоставить подсказку серверу аутентификации Google. Сервер использует подсказку, чтобы упростить процесс входа в систему, либо предварительно заполнив поле электронной почты в форме входа, либо выбрав соответствующий сеанс с несколькими входами.

Установите в качестве значения параметра адрес электронной почты или sub идентификатор, который эквивалентен идентификатору Google ID пользователя.

prompt Необязательный

Список подсказок, разделенных пробелами и чувствительных к регистру, которые должны быть представлены пользователю. Если вы не укажете этот параметр, пользователю будет предложено это сделать только при первом запросе доступа вашего проекта. Дополнительную информацию см. в разделе «Запрос на повторное согласие» .

Возможные значения:

none Не отображайте экраны аутентификации или согласия. Не должно быть указано с другими значениями.
consent Запросить у пользователя согласие.
select_account Предложите пользователю выбрать учетную запись.

Шаг 2. Перенаправление на сервер Google OAuth 2.0.

Перенаправьте пользователя на сервер Google OAuth 2.0, чтобы инициировать процесс аутентификации и авторизации. Обычно это происходит, когда вашему приложению впервые требуется доступ к данным пользователя. В случае добавочной авторизации этот шаг также происходит, когда вашему приложению впервые требуется доступ к дополнительным ресурсам, на доступ к которым у него еще нет разрешения.

PHP

  1. Создайте URL-адрес для запроса доступа с сервера Google OAuth 2.0:
    $auth_url = $client->createAuthUrl();
  2. Перенаправьте пользователя на $auth_url :
    header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));

Питон

В этом примере показано, как перенаправить пользователя на URL-адрес авторизации с помощью платформы веб-приложения Flask:

return flask.redirect(authorization_url)

Руби

  1. Создайте URL-адрес для запроса доступа с сервера Google OAuth 2.0:
    auth_uri = authorizer.get_authorization_url(request: request)
  2. Перенаправьте пользователя на auth_uri .

Node.js

  1. Используйте сгенерированный URL-адрес authorizationUrl из метода generateAuthUrl на шаге 1, чтобы запросить доступ к серверу Google OAuth 2.0.
  2. Перенаправьте пользователя на authorizationUrl .
    res.redirect(authorizationUrl);

HTTP/REST

Пример перенаправления на сервер авторизации Google

Ниже показан пример URL-адреса с разрывами строк и пробелами для удобства чтения.

https://accounts.google.com/o/oauth2/v2/auth?
 scope=https%3A//www.googleapis.com/auth/drive.metadata.readonly%20https%3A//www.googleapis.com/auth/calendar.readonly&
 access_type=offline&
 include_granted_scopes=true&
 response_type=code&
 state=state_parameter_passthrough_value&
 redirect_uri=https%3A//oauth2.example.com/code&
 client_id=client_id

После создания URL-адреса запроса перенаправьте на него пользователя.

Сервер OAuth 2.0 Google аутентифицирует пользователя и получает от него согласие на доступ вашего приложения к запрошенным областям. Ответ отправляется обратно в ваше приложение с использованием указанного вами URL-адреса перенаправления.

Шаг 3. Google запрашивает у пользователя согласие

На этом этапе пользователь решает, предоставить ли вашему приложению запрошенный доступ. На этом этапе Google отображает окно согласия, в котором отображается имя вашего приложения и сервисов Google API, к которым он запрашивает разрешение, с учетными данными авторизации пользователя и сводной информацией об объемах доступа, которые необходимо предоставить. Затем пользователь может согласиться предоставить доступ к одной или нескольким областям, запрошенным вашим приложением, или отклонить запрос.

На этом этапе вашему приложению не нужно ничего делать, поскольку оно ожидает ответа от сервера Google OAuth 2.0, указывающего, был ли предоставлен какой-либо доступ. Этот ответ объясняется на следующем шаге.

Ошибки

Запросы к конечной точке авторизации OAuth 2.0 Google могут отображать сообщения об ошибках вместо ожидаемых потоков аутентификации и авторизации. Ниже перечислены распространенные коды ошибок и предлагаемые решения.

admin_policy_enforced

Аккаунт Google не может авторизовать одну или несколько запрошенных областей в соответствии с правилами администратора Google Workspace. Дополнительную информацию о том, как администратор может ограничить доступ ко всем областям или конфиденциальным и ограниченным областям до тех пор, пока доступ не будет явно предоставлен вашему идентификатору клиента OAuth, см. в справочной статье администратора Google Workspace «Управление тем, какие сторонние и внутренние приложения получают доступ к данным Google Workspace».

disallowed_useragent

Конечная точка авторизации отображается внутри встроенного пользовательского агента, запрещенного политиками Google OAuth 2.0 .

Андроид

Разработчики Android могут столкнуться с этим сообщением об ошибке при открытии запросов авторизации в android.webkit.WebView . Вместо этого разработчикам следует использовать библиотеки Android, такие как Google Sign-In для Android или AppAuth OpenID Foundation для Android .

Веб-разработчики могут столкнуться с этой ошибкой, когда приложение Android открывает общую веб-ссылку во встроенном пользовательском агенте, и пользователь переходит к конечной точке авторизации Google OAuth 2.0 с вашего сайта. Разработчики должны разрешить открытие общих ссылок в обработчике ссылок по умолчанию операционной системы, который включает в себя как обработчики ссылок приложений Android , так и браузерное приложение по умолчанию. Библиотека пользовательских вкладок Android также поддерживается.

iOS

Разработчики iOS и macOS могут столкнуться с этой ошибкой при открытии запросов авторизации в WKWebView . Вместо этого разработчикам следует использовать библиотеки iOS, такие как Google Sign-In для iOS или AppAuth OpenID Foundation для iOS .

Веб-разработчики могут столкнуться с этой ошибкой, когда приложение iOS или macOS открывает общую веб-ссылку во встроенном пользовательском агенте, и пользователь переходит к конечной точке авторизации Google OAuth 2.0 с вашего сайта. Разработчики должны разрешить открытие общих ссылок в обработчике ссылок по умолчанию операционной системы, который включает в себя как обработчики универсальных ссылок , так и браузерное приложение по умолчанию. Библиотека SFSafariViewController также поддерживается.

org_internal

Идентификатор клиента OAuth в запросе является частью проекта, ограничивающего доступ к аккаунтам Google в конкретной организации Google Cloud . Дополнительные сведения об этом параметре конфигурации см. в разделе «Тип пользователя» в справочной статье «Настройка экрана согласия OAuth».

invalid_client

Секретный ключ клиента OAuth неверен. Просмотрите конфигурацию клиента OAuth , включая идентификатор клиента и секретный ключ, использованные для этого запроса.

invalid_grant

При обновлении токена доступа или использовании добавочной авторизации срок действия токена может истек или он стал недействительным. Аутентифицируйте пользователя еще раз и запросите согласие пользователя на получение новых токенов. Если вы продолжаете видеть эту ошибку, убедитесь, что ваше приложение настроено правильно и что вы используете правильные токены и параметры в своем запросе. В противном случае учетная запись пользователя могла быть удалена или отключена.

redirect_uri_mismatch

redirect_uri , переданный в запросе авторизации, не соответствует авторизованному URI перенаправления для идентификатора клиента OAuth. Просмотрите разрешенные URI перенаправления в .

Параметр redirect_uri может относиться к внеполосному потоку OAuth (OOB), который устарел и больше не поддерживается. Обратитесь к руководству по миграции , чтобы обновить интеграцию.

invalid_request

Что-то не так с вашим запросом. Это может быть связано с рядом причин:

  • Запрос был неправильно отформатирован
  • В запросе отсутствовали необходимые параметры
  • В запросе используется метод авторизации, который Google не поддерживает. Убедитесь, что в вашей интеграции OAuth используется рекомендуемый метод интеграции.

Шаг 4. Обработка ответа сервера OAuth 2.0

Сервер OAuth 2.0 отвечает на запрос доступа вашего приложения, используя URL-адрес, указанный в запросе.

Если пользователь одобряет запрос на доступ, то ответ содержит код авторизации. Если пользователь не одобряет запрос, ответ содержит сообщение об ошибке. Код авторизации или сообщение об ошибке, возвращаемое веб-серверу, появляется в строке запроса, как показано ниже:

Ответ об ошибке:

https://oauth2.example.com/auth?error=access_denied

Ответ с кодом авторизации:

https://oauth2.example.com/auth?code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7

Пример ответа сервера OAuth 2.0

Вы можете протестировать этот процесс, щелкнув следующий пример URL-адреса, который запрашивает доступ только для чтения для просмотра метаданных файлов на вашем Google Диске и доступ только для чтения для просмотра событий вашего Календаря Google:

https://accounts.google.com/o/oauth2/v2/auth?
 scope=https%3A//www.googleapis.com/auth/drive.metadata.readonly%20https%3A//www.googleapis.com/auth/calendar.readonly&
 access_type=offline&
 include_granted_scopes=true&
 response_type=code&
 state=state_parameter_passthrough_value&
 redirect_uri=https%3A//oauth2.example.com/code&
 client_id=client_id

После завершения потока OAuth 2.0 вы должны быть перенаправлены на http://localhost/oauth2callback , что, скорее всего, приведет к ошибке 404 NOT FOUND если ваш локальный компьютер не обслуживает файл по этому адресу. Следующий шаг предоставляет более подробную информацию об информации, возвращаемой в URI, когда пользователь перенаправляется обратно в ваше приложение.

Шаг 5. Обмен кода авторизации для токенов обновления и доступа.

После того как веб-сервер получит код авторизации, он может обменять код авторизации на токен доступа.

PHP

Чтобы обменять код авторизации на токен доступа, используйте метод fetchAccessTokenWithAuthCode :

$access_token = $client->fetchAccessTokenWithAuthCode($_GET['code']);

Питон

На странице обратного вызова используйте библиотеку google-auth для проверки ответа сервера авторизации. Затем используйте метод flow.fetch_token для обмена кода авторизации в этом ответе на токен доступа:

state = flask.session['state']
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
    'client_secret.json',
    scopes=['https://www.googleapis.com/auth/drive.metadata.readonly'],
    state=state)
flow.redirect_uri = flask.url_for('oauth2callback', _external=True)

authorization_response = flask.request.url
flow.fetch_token(authorization_response=authorization_response)

# Store the credentials in the session.
# ACTION ITEM for developers:
#     Store user's access and refresh tokens in your data store if
#     incorporating this code into your real app.
credentials = flow.credentials
flask.session['credentials'] = {
    'token': credentials.token,
    'refresh_token': credentials.refresh_token,
    'token_uri': credentials.token_uri,
    'client_id': credentials.client_id,
    'client_secret': credentials.client_secret,
    'granted_scopes': credentials.granted_scopes}

Руби

На странице обратного вызова используйте библиотеку googleauth для проверки ответа сервера авторизации. Используйте authorizer.handle_auth_callback_deferred , чтобы сохранить код авторизации и перенаправить обратно на URL-адрес, который первоначально запрашивал авторизацию. Это откладывает обмен кодом, временно сохраняя результаты в сеансе пользователя.

  target_url = Google::Auth::WebUserAuthorizer.handle_auth_callback_deferred(request)
  redirect target_url

Node.js

Чтобы обменять код авторизации на токен доступа, используйте метод getToken :

const url = require('url');

// Receive the callback from Google's OAuth 2.0 server.
app.get('/oauth2callback', async (req, res) => {
  let q = url.parse(req.url, true).query;

  if (q.error) { // An error response e.g. error=access_denied
    console.log('Error:' + q.error);
  } else if (q.state !== req.session.state) { //check state value
    console.log('State mismatch. Possible CSRF attack');
    res.end('State mismatch. Possible CSRF attack');
  } else { // Get access and refresh tokens (if access_type is offline)

    let { tokens } = await oauth2Client.getToken(q.code);
    oauth2Client.setCredentials(tokens);
});

HTTP/REST

Чтобы обменять код авторизации на токен доступа, вызовите конечную точку https://oauth2.googleapis.com/token и установите следующие параметры:

Поля
client_id Идентификатор клиента, полученный из .
client_secret Секрет клиента, полученный от .
code Код авторизации, возвращенный из первоначального запроса.
grant_type Как определено в спецификации OAuth 2.0 , значение этого поля должно быть установлено в authorization_code .
redirect_uri Один из URI перенаправления, перечисленных для вашего проекта в для данного client_id .

В следующем фрагменте показан пример запроса:

POST /token HTTP/1.1
Host: oauth2.googleapis.com
Content-Type: application/x-www-form-urlencoded

code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7&
client_id=your_client_id&
client_secret=your_client_secret&
redirect_uri=https%3A//oauth2.example.com/code&
grant_type=authorization_code

Google отвечает на этот запрос, возвращая объект JSON, содержащий кратковременный токен доступа и токен обновления. Обратите внимание, что токен обновления возвращается только в том случае, если ваше приложение установило для параметра access_type значение offline в первоначальном запросе к серверу авторизации Google .

Ответ содержит следующие поля:

Поля
access_token Токен, который ваше приложение отправляет для авторизации запроса Google API.
expires_in Оставшийся срок действия токена доступа в секундах.
refresh_token Токен, который можно использовать для получения нового токена доступа. Токены обновления действительны до тех пор, пока пользователь не отзовет доступ или пока не истечет срок действия токена обновления. Опять же, это поле присутствует в этом ответе только в том случае, если вы установили для параметра access_type значение offline в первоначальном запросе к серверу авторизации Google.
refresh_token_expires_in Оставшееся время жизни токена обновления в секундах. Это значение устанавливается только тогда, когда пользователь предоставляет доступ на основе времени .
scope Области доступа, предоставляемые access_token выражаются в виде списка строк, разделенных пробелами и чувствительных к регистру.
token_type Тип возвращенного токена. В настоящее время значение этого поля всегда установлено на Bearer .

В следующем фрагменте показан пример ответа:

{
  "access_token": "1/fFAGRNJru1FTz70BzhT3Zg",
  "expires_in": 3920,
  "token_type": "Bearer",
  "scope": "https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/calendar.readonly",
  "refresh_token": "1//xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI"
}

Ошибки

При обмене кода авторизации на токен доступа вместо ожидаемого ответа вы можете столкнуться со следующей ошибкой. Ниже перечислены распространенные коды ошибок и предлагаемые решения.

invalid_grant

Предоставленный код авторизации недействителен или имеет неверный формат. Запросите новый код, перезапустив процесс OAuth, чтобы снова запросить у пользователя согласие.

Шаг 6. Проверьте, какие области действия предоставлены пользователями

При запросе нескольких разрешений (областей) пользователи могут не предоставить вашему приложению доступ ко всем из них. Ваше приложение должно проверять, какие области действительно были предоставлены, и корректно обрабатывать ситуации, когда некоторые разрешения запрещены, обычно путем отключения функций, которые полагаются на эти запрещенные области.

Однако есть исключения. Приложения Google Workspace Enterprise с делегированием полномочий на уровне домена или приложения, отмеченные как Надежные , обходят экран согласия с детальными разрешениями. В этих приложениях пользователи не увидят экран согласия с детальными разрешениями. Вместо этого ваше приложение либо получит все запрошенные области, либо не получит ни одной.

Более подробную информацию см. в разделе Как обрабатывать детальные разрешения .

PHP

Чтобы проверить, какие области действия предоставил пользователь, используйте метод getGrantedScope() :

// Space-separated string of granted scopes if it exists, otherwise null.
$granted_scopes = $client->getOAuth2Service()->getGrantedScope();

// Determine which scopes user granted and build a dictionary
$granted_scopes_dict = [
  'Drive' => str_contains($granted_scopes, Google\Service\Drive::DRIVE_METADATA_READONLY),
  'Calendar' => str_contains($granted_scopes, Google\Service\Calendar::CALENDAR_READONLY)
];

Питон

Возвращенный объект credentials имеет свойство granted_scopes , которое представляет собой список областей, которые пользователь предоставил вашему приложению.

credentials = flow.credentials
flask.session['credentials'] = {
    'token': credentials.token,
    'refresh_token': credentials.refresh_token,
    'token_uri': credentials.token_uri,
    'client_id': credentials.client_id,
    'client_secret': credentials.client_secret,
    'granted_scopes': credentials.granted_scopes}

Следующая функция проверяет, какие области действия пользователь предоставил вашему приложению.

def check_granted_scopes(credentials):
  features = {}
  if 'https://www.googleapis.com/auth/drive.metadata.readonly' in credentials['granted_scopes']:
    features['drive'] = True
  else:
    features['drive'] = False

  if 'https://www.googleapis.com/auth/calendar.readonly' in credentials['granted_scopes']:
    features['calendar'] = True
  else:
    features['calendar'] = False

  return features

Руби

При одновременном запросе нескольких областей проверьте, какие области были предоставлены, с помощью свойства scope объекта credentials .

# User authorized the request. Now, check which scopes were granted.
if credentials.scope.include?(Google::Apis::DriveV3::AUTH_DRIVE_METADATA_READONLY)
  # User authorized read-only Drive activity permission.
  # Calling the APIs, etc
else
  # User didn't authorize read-only Drive activity permission.
  # Update UX and application accordingly
end

# Check if user authorized Calendar read permission.
if credentials.scope.include?(Google::Apis::CalendarV3::AUTH_CALENDAR_READONLY)
  # User authorized Calendar read permission.
  # Calling the APIs, etc.
else
  # User didn't authorize Calendar read permission.
  # Update UX and application accordingly
end

Node.js

При одновременном запросе нескольких областей проверьте, какие области были предоставлены через свойство scope объекта tokens .

// User authorized the request. Now, check which scopes were granted.
if (tokens.scope.includes('https://www.googleapis.com/auth/drive.metadata.readonly'))
{
  // User authorized read-only Drive activity permission.
  // Calling the APIs, etc.
}
else
{
  // User didn't authorize read-only Drive activity permission.
  // Update UX and application accordingly
}

// Check if user authorized Calendar read permission.
if (tokens.scope.includes('https://www.googleapis.com/auth/calendar.readonly'))
{
  // User authorized Calendar read permission.
  // Calling the APIs, etc.
}
else
{
  // User didn't authorize Calendar read permission.
  // Update UX and application accordingly
}

HTTP/REST

Чтобы проверить, предоставил ли пользователь вашему приложению доступ к определенной области, проверьте поле scope в ответе токена доступа. Области доступа, предоставляемые access_token, выражаются в виде списка строк, разделенных пробелами и чувствительных к регистру.

Например, следующий пример ответа токена доступа показывает, что пользователь предоставил вашему приложению доступ только для чтения к действиям на Диске и разрешениям на события календаря:

  {
    "access_token": "1/fFAGRNJru1FTz70BzhT3Zg",
    "expires_in": 3920,
    "token_type": "Bearer",
    "scope": "https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/calendar.readonly",
    "refresh_token": "1//xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI"
  }

Вызов API Google

PHP

Используйте токен доступа для вызова API Google, выполнив следующие шаги:

  1. Если вам нужно применить токен доступа к новому объекту Google\Client — например, если вы сохранили токен доступа в сеансе пользователя — используйте метод setAccessToken :
    $client->setAccessToken($access_token);
  2. Создайте объект службы для API, который вы хотите вызвать. Вы создаете объект службы, предоставляя авторизованный объект Google\Client конструктору API, который вы хотите вызвать. Например, чтобы вызвать Drive API:
    $drive = new Google\Service\Drive($client);
  3. Отправляйте запросы к службе API, используя интерфейс, предоставляемый объектом службы . Например, чтобы вывести список файлов на Google Диске аутентифицированного пользователя:
    $files = $drive->files->listFiles(array());

Питон

После получения токена доступа ваше приложение может использовать этот токен для авторизации запросов API от имени определенной учетной записи пользователя или учетной записи службы. Используйте учетные данные авторизации для конкретного пользователя, чтобы создать объект службы для API, который вы хотите вызвать, а затем используйте этот объект для выполнения авторизованных запросов API.

  1. Создайте объект службы для API, который вы хотите вызвать. Вы создаете объект службы, вызывая метод build библиотеки googleapiclient.discovery с указанием имени и версии API, а также учетных данных пользователя: например, для вызова версии 3 Drive API:
    from googleapiclient.discovery import build
    
    drive = build('drive', 'v2', credentials=credentials)
  2. Отправляйте запросы к службе API, используя интерфейс, предоставляемый объектом службы . Например, чтобы вывести список файлов на Google Диске аутентифицированного пользователя:
    files = drive.files().list().execute()

Руби

После получения токена доступа ваше приложение может использовать этот токен для выполнения запросов API от имени определенной учетной записи пользователя или учетной записи службы. Используйте учетные данные авторизации для конкретного пользователя, чтобы создать объект службы для API, который вы хотите вызвать, а затем используйте этот объект для выполнения авторизованных запросов API.

  1. Создайте объект службы для API, который вы хотите вызвать. Например, чтобы вызвать версию 3 Drive API:
    drive = Google::Apis::DriveV3::DriveService.new
  2. Установите учетные данные в сервисе:
    drive.authorization = credentials
  3. Отправляйте запросы к службе API, используя интерфейс, предоставляемый объектом службы . Например, чтобы вывести список файлов на Google Диске аутентифицированного пользователя:
    files = drive.list_files

Альтернативно, авторизация может быть предоставлена ​​для каждого метода путем предоставления параметра options методу:

files = drive.list_files(options: { authorization: credentials })

Node.js

После получения токена доступа и установления его на объект OAuth2 , используйте объект, чтобы вызвать API Google. Ваше приложение может использовать этот токен для авторизации запросов API от имени данной учетной записи пользователя или учетной записи службы. Создайте сервисный объект для API, который вы хотите позвонить. Например, в следующем коде используется API Google Drive для перечисления имен файлов в дисков пользователя.

const { google } = require('googleapis');

// Example of using Google Drive API to list filenames in user's Drive.
const drive = google.drive('v3');
drive.files.list({
  auth: oauth2Client,
  pageSize: 10,
  fields: 'nextPageToken, files(id, name)',
}, (err1, res1) => {
  if (err1) return console.log('The API returned an error: ' + err1);
  const files = res1.data.files;
  if (files.length) {
    console.log('Files:');
    files.map((file) => {
      console.log(`${file.name} (${file.id})`);
    });
  } else {
    console.log('No files found.');
  }
});

Http/rest

После того, как ваше приложение получит токен доступа, вы можете использовать токен для вызова вызовов в Google API от имени данной учетной записи пользователя, если были предоставлены объем доступа, требуемый API. Для этого включите токен доступа в запрос к API, включив либо параметр запроса access_token , либо значение Authorization Bearer HTTP. Когда это возможно, заголовок HTTP является предпочтительным, потому что строки запросов, как правило, видны в журналах сервера. В большинстве случаев вы можете использовать клиентскую библиотеку для настройки ваших вызовов для Google API (например, при вызове API файлов диска ).

Вы можете попробовать все API Google и просмотреть их прицелы на игровой площадке OAuth 2.0 .

Http Получите примеры

Вызов к конечной точке drive.files (API файлов диска) с использованием Authorization: Bearer может выглядеть следующим образом. Обратите внимание, что вам нужно указать свой собственный токен доступа:

GET /drive/v2/files HTTP/1.1
Host: www.googleapis.com
Authorization: Bearer access_token

Вот призыв к тому же API для аутентифицированного пользователя, используя параметр строки запроса access_token :

GET https://www.googleapis.com/drive/v2/files?access_token=access_token

Примеры curl

Вы можете проверить эти команды с помощью приложения командной строки curl . Вот пример, который использует опцию заголовка HTTP (предпочтительно):

curl -H "Authorization: Bearer access_token" https://www.googleapis.com/drive/v2/files

Или, в качестве альтернативы, опция параметра строки запроса:

curl https://www.googleapis.com/drive/v2/files?access_token=access_token

Полный пример

В следующем примере печатает список файлов, форматированных JSON, в Google Drive пользователя после того, как пользователь выступает и дает согласие на приложение на доступ к метаданным диска пользователя.

PHP

Чтобы запустить этот пример:

  1. В API Console, добавьте URL локальной машины в список URL -адресов перенаправления. Например, добавьте http://localhost:8080 .
  2. Создайте новый каталог и измените его. Например:
    mkdir ~/php-oauth2-example
    cd ~/php-oauth2-example
  3. Установите клиентскую библиотеку Google API для PHP с помощью Composer :
    composer require google/apiclient:^2.15.0
  4. Создайте файлы index.php и oauth2callback.php с следующим содержанием.
  5. Запустите пример со встроенным тестовым веб-сервером PHP:
    php -S localhost:8080 ~/php-oauth2-example

index.php

<?php
require_once __DIR__.'/vendor/autoload.php';

session_start();

$client = new Google\Client();
$client->setAuthConfig('client_secret.json');

// User granted permission as an access token is in the session.
if (isset($_SESSION['access_token']) && $_SESSION['access_token'])
{
  $client->setAccessToken($_SESSION['access_token']);
  
  // Check if user granted Drive permission
  if ($_SESSION['granted_scopes_dict']['Drive']) {
    echo "Drive feature is enabled.";
    echo "</br>";
    $drive = new Drive($client);
    $files = array();
    $response = $drive->files->listFiles(array());
    foreach ($response->files as $file) {
        echo "File: " . $file->name . " (" . $file->id . ")";
        echo "</br>";
    }
  } else {
    echo "Drive feature is NOT enabled.";
    echo "</br>";
  }

   // Check if user granted Calendar permission
  if ($_SESSION['granted_scopes_dict']['Calendar']) {
    echo "Calendar feature is enabled.";
    echo "</br>";
  } else {
    echo "Calendar feature is NOT enabled.";
    echo "</br>";
  }
}
else
{
  // Redirect users to outh2call.php which redirects users to Google OAuth 2.0
  $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php';
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
?>

oauth2callback.php

<?php
require_once __DIR__.'/vendor/autoload.php';

session_start();

$client = new Google\Client();

// Required, call the setAuthConfig function to load authorization credentials from
// client_secret.json file.
$client->setAuthConfigFile('client_secret.json');
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST']. $_SERVER['PHP_SELF']);

// Required, to set the scope value, call the addScope function.
$client->addScope([Google\Service\Drive::DRIVE_METADATA_READONLY, Google\Service\Calendar::CALENDAR_READONLY]);

// Enable incremental authorization. Recommended as a best practice.
$client->setIncludeGrantedScopes(true);

// Recommended, offline access will give you both an access and refresh token so that
// your app can refresh the access token without user interaction.
$client->setAccessType("offline");

// Generate a URL for authorization as it doesn't contain code and error
if (!isset($_GET['code']) && !isset($_GET['error']))
{
  // Generate and set state value
  $state = bin2hex(random_bytes(16));
  $client->setState($state);
  $_SESSION['state'] = $state;

  // Generate a url that asks permissions.
  $auth_url = $client->createAuthUrl();
  header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
}

// User authorized the request and authorization code is returned to exchange access and
// refresh tokens.
if (isset($_GET['code']))
{
  // Check the state value
  if (!isset($_GET['state']) || $_GET['state'] !== $_SESSION['state']) {
    die('State mismatch. Possible CSRF attack.');
  }

  // Get access and refresh tokens (if access_type is offline)
  $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);

  /** Save access and refresh token to the session variables.
    * ACTION ITEM: In a production app, you likely want to save the
    *              refresh token in a secure persistent storage instead. */
  $_SESSION['access_token'] = $token;
  $_SESSION['refresh_token'] = $client->getRefreshToken();
  
  // Space-separated string of granted scopes if it exists, otherwise null.
  $granted_scopes = $client->getOAuth2Service()->getGrantedScope();

  // Determine which scopes user granted and build a dictionary
  $granted_scopes_dict = [
    'Drive' => str_contains($granted_scopes, Google\Service\Drive::DRIVE_METADATA_READONLY),
    'Calendar' => str_contains($granted_scopes, Google\Service\Calendar::CALENDAR_READONLY)
  ];
  $_SESSION['granted_scopes_dict'] = $granted_scopes_dict;
  
  $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/';
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}

// An error response e.g. error=access_denied
if (isset($_GET['error']))
{
  echo "Error: ". $_GET['error'];
}
?>

Питон

В этом примере используется фланг -структура. Он запускает веб -приложение по адресу http://localhost:8080 который позволяет протестировать поток OAuth 2.0. Если вы пойдете на этот URL, вы должны увидеть пять ссылок:

  • API Call Drive: Эта ссылка указывает на страницу, которая пытается выполнить образец запроса API, если пользователи предоставили разрешение. При необходимости это начинает поток авторизации. Если успешно, страница отображает ответ API.
  • Mock Page, чтобы вызвать API календаря: эта ссылка указывает на MAOCHPAGE, которая пытается выполнить запрос API календаря, если пользователи предоставили разрешение. При необходимости это начинает поток авторизации. Если успешно, страница отображает ответ API.
  • Проверьте поток AUTH напрямую: эта ссылка указывает на страницу, которая пытается отправить пользователя через поток авторизации . Приложение запрашивает разрешение на отправку авторизованных запросов API от имени пользователя.
  • Отменить текущие учетные данные: эта ссылка указывает на страницу, которая отменяет разрешения, которые пользователь уже предоставил заявке.
  • Учетные данные Clear Flask Session: эта ссылка очищает учетные данные авторизации, которые хранятся в сессии Flask. Это позволяет вам увидеть, что произойдет, если пользователь, который уже предоставил разрешение вашему приложению, попытался выполнить запрос API в новом сеансе. Это также позволяет вам увидеть ответ API, который получит ваш приложение, если пользователь отменил разрешения, предоставленные вашему приложению, и ваше приложение все еще пыталось разрешить запрос с отозванным токеном доступа.
# -*- coding: utf-8 -*-

import os
import flask
import requests

import google.oauth2.credentials
import google_auth_oauthlib.flow
import googleapiclient.discovery

# This variable specifies the name of a file that contains the OAuth 2.0
# information for this application, including its client_id and client_secret.
CLIENT_SECRETS_FILE = "client_secret.json"

# The OAuth 2.0 access scope allows for access to the
# authenticated user's account and requires requests to use an SSL connection.
SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly',
          'https://www.googleapis.com/auth/calendar.readonly']
API_SERVICE_NAME = 'drive'
API_VERSION = 'v2'

app = flask.Flask(__name__)
# Note: A secret key is included in the sample so that it works.
# If you use this code in your application, replace this with a truly secret
# key. See https://flask.palletsprojects.com/quickstart/#sessions.
app.secret_key = 'REPLACE ME - this value is here as a placeholder.'

@app.route('/')
def index():
  return print_index_table()

@app.route('/drive')
def drive_api_request():
  if 'credentials' not in flask.session:
    return flask.redirect('authorize')

  features = flask.session['features']

  if features['drive']:
    # Load credentials from the session.
    credentials = google.oauth2.credentials.Credentials(
        **flask.session['credentials'])

    drive = googleapiclient.discovery.build(
        API_SERVICE_NAME, API_VERSION, credentials=credentials)

    files = drive.files().list().execute()

    # Save credentials back to session in case access token was refreshed.
    # ACTION ITEM: In a production app, you likely want to save these
    #              credentials in a persistent database instead.
    flask.session['credentials'] = credentials_to_dict(credentials)

    return flask.jsonify(**files)
  else:
    # User didn't authorize read-only Drive activity permission.
    # Update UX and application accordingly
    return '<p>Drive feature is not enabled.</p>'

@app.route('/calendar')
    def calendar_api_request():
      if 'credentials' not in flask.session:
        return flask.redirect('authorize')

      features = flask.session['features']

      if features['calendar']:
        # User authorized Calendar read permission.
        # Calling the APIs, etc.
        return ('<p>User granted the Google Calendar read permission. '+
                'This sample code does not include code to call Calendar</p>')
      else:
        # User didn't authorize Calendar read permission.
        # Update UX and application accordingly
        return '<p>Calendar feature is not enabled.</p>'

@app.route('/authorize')
def authorize():
  # Create flow instance to manage the OAuth 2.0 Authorization Grant Flow steps.
  flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
      CLIENT_SECRETS_FILE, scopes=SCOPES)

  # The URI created here must exactly match one of the authorized redirect URIs
  # for the OAuth 2.0 client, which you configured in the API Console. If this
  # value doesn't match an authorized URI, you will get a 'redirect_uri_mismatch'
  # error.
  flow.redirect_uri = flask.url_for('oauth2callback', _external=True)

  authorization_url, state = flow.authorization_url(
      # Enable offline access so that you can refresh an access token without
      # re-prompting the user for permission. Recommended for web server apps.
      access_type='offline',
      # Enable incremental authorization. Recommended as a best practice.
      include_granted_scopes='true')

  # Store the state so the callback can verify the auth server response.
  flask.session['state'] = state

  return flask.redirect(authorization_url)

@app.route('/oauth2callback')
def oauth2callback():
  # Specify the state when creating the flow in the callback so that it can
  # verified in the authorization server response.
  state = flask.session['state']

  flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
      CLIENT_SECRETS_FILE, scopes=SCOPES, state=state)
  flow.redirect_uri = flask.url_for('oauth2callback', _external=True)

  # Use the authorization server's response to fetch the OAuth 2.0 tokens.
  authorization_response = flask.request.url
  flow.fetch_token(authorization_response=authorization_response)

  # Store credentials in the session.
  # ACTION ITEM: In a production app, you likely want to save these
  #              credentials in a persistent database instead.
  credentials = flow.credentials
  
  credentials = credentials_to_dict(credentials)
  flask.session['credentials'] = credentials

  # Check which scopes user granted
  features = check_granted_scopes(credentials)
  flask.session['features'] = features
  return flask.redirect('/')
  

@app.route('/revoke')
def revoke():
  if 'credentials' not in flask.session:
    return ('You need to <a href="/authorize">authorize</a> before ' +
            'testing the code to revoke credentials.')

  credentials = google.oauth2.credentials.Credentials(
    **flask.session['credentials'])

  revoke = requests.post('https://oauth2.googleapis.com/revoke',
      params={'token': credentials.token},
      headers = {'content-type': 'application/x-www-form-urlencoded'})

  status_code = getattr(revoke, 'status_code')
  if status_code == 200:
    return('Credentials successfully revoked.' + print_index_table())
  else:
    return('An error occurred.' + print_index_table())

@app.route('/clear')
def clear_credentials():
  if 'credentials' in flask.session:
    del flask.session['credentials']
  return ('Credentials have been cleared.<br><br>' +
          print_index_table())

def credentials_to_dict(credentials):
  return {'token': credentials.token,
          'refresh_token': credentials.refresh_token,
          'token_uri': credentials.token_uri,
          'client_id': credentials.client_id,
          'client_secret': credentials.client_secret,
          'granted_scopes': credentials.granted_scopes}

def check_granted_scopes(credentials):
  features = {}
  if 'https://www.googleapis.com/auth/drive.metadata.readonly' in credentials['granted_scopes']:
    features['drive'] = True
  else:
    features['drive'] = False

  if 'https://www.googleapis.com/auth/calendar.readonly' in credentials['granted_scopes']:
    features['calendar'] = True
  else:
    features['calendar'] = False

  return features

def print_index_table():
  return ('<table>' +
          '<tr><td><a href="/test">Test an API request</a></td>' +
          '<td>Submit an API request and see a formatted JSON response. ' +
          '    Go through the authorization flow if there are no stored ' +
          '    credentials for the user.</td></tr>' +
          '<tr><td><a href="/authorize">Test the auth flow directly</a></td>' +
          '<td>Go directly to the authorization flow. If there are stored ' +
          '    credentials, you still might not be prompted to reauthorize ' +
          '    the application.</td></tr>' +
          '<tr><td><a href="/revoke">Revoke current credentials</a></td>' +
          '<td>Revoke the access token associated with the current user ' +
          '    session. After revoking credentials, if you go to the test ' +
          '    page, you should see an <code>invalid_grant</code> error.' +
          '</td></tr>' +
          '<tr><td><a href="/clear">Clear Flask session credentials</a></td>' +
          '<td>Clear the access token currently stored in the user session. ' +
          '    After clearing the token, if you <a href="/test">test the ' +
          '    API request</a> again, you should go back to the auth flow.' +
          '</td></tr></table>')

if __name__ == '__main__':
  # When running locally, disable OAuthlib's HTTPs verification.
  # ACTION ITEM for developers:
  #     When running in production *do not* leave this option enabled.
  os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'

  # This disables the requested scopes and granted scopes check.
  # If users only grant partial request, the warning would not be thrown.
  os.environ['OAUTHLIB_RELAX_TOKEN_SCOPE'] = '1'

  # Specify a hostname and port that are set as a valid redirect URI
  # for your API project in the Google API Console.
  app.run('localhost', 8080, debug=True)

Руби

В этом примере используется структура синатрой .

require 'googleauth'
require 'googleauth/web_user_authorizer'
require 'googleauth/stores/redis_token_store'

require 'google/apis/drive_v3'
require 'google/apis/calendar_v3'

require 'sinatra'

configure do
  enable :sessions

  # Required, call the from_file method to retrieve the client ID from a
  # client_secret.json file.
  set :client_id, Google::Auth::ClientId.from_file('/path/to/client_secret.json')

  # Required, scope value
  # Access scopes for two non-Sign-In scopes: Read-only Drive activity and Google Calendar.
  scope = ['Google::Apis::DriveV3::AUTH_DRIVE_METADATA_READONLY',
           'Google::Apis::CalendarV3::AUTH_CALENDAR_READONLY']

  # Required, Authorizers require a storage instance to manage long term persistence of
  # access and refresh tokens.
  set :token_store, Google::Auth::Stores::RedisTokenStore.new(redis: Redis.new)

  # Required, indicate where the API server will redirect the user after the user completes
  # the authorization flow. The redirect URI is required. The value must exactly
  # match one of the authorized redirect URIs for the OAuth 2.0 client, which you
  # configured in the API Console. If this value doesn't match an authorized URI,
  # you will get a 'redirect_uri_mismatch' error.
  set :callback_uri, '/oauth2callback'

  # To use OAuth2 authentication, we need access to a CLIENT_ID, CLIENT_SECRET, AND REDIRECT_URI
  # from the client_secret.json file. To get these credentials for your application, visit
  # https://console.cloud.google.com/apis/credentials.
  set :authorizer, Google::Auth::WebUserAuthorizer.new(settings.client_id, settings.scope,
                          settings.token_store, callback_uri: settings.callback_uri)
end

get '/' do
  # NOTE: Assumes the user is already authenticated to the app
  user_id = request.session['user_id']

  # Fetch stored credentials for the user from the given request session.
  # nil if none present
  credentials = settings.authorizer.get_credentials(user_id, request)

  if credentials.nil?
    # Generate a url that asks the user to authorize requested scope(s).
    # Then, redirect user to the url.
    redirect settings.authorizer.get_authorization_url(request: request)
  end
  
  # User authorized the request. Now, check which scopes were granted.
  if credentials.scope.include?(Google::Apis::DriveV3::AUTH_DRIVE_METADATA_READONLY)
    # User authorized read-only Drive activity permission.
    # Example of using Google Drive API to list filenames in user's Drive.
    drive = Google::Apis::DriveV3::DriveService.new
    files = drive.list_files(options: { authorization: credentials })
    "<pre>#{JSON.pretty_generate(files.to_h)}</pre>"
  else
    # User didn't authorize read-only Drive activity permission.
    # Update UX and application accordingly
  end

  # Check if user authorized Calendar read permission.
  if credentials.scope.include?(Google::Apis::CalendarV3::AUTH_CALENDAR_READONLY)
    # User authorized Calendar read permission.
    # Calling the APIs, etc.
  else
    # User didn't authorize Calendar read permission.
    # Update UX and application accordingly
  end
end

# Receive the callback from Google's OAuth 2.0 server.
get '/oauth2callback' do
  # Handle the result of the oauth callback. Defers the exchange of the code by
  # temporarily stashing the results in the user's session.
  target_url = Google::Auth::WebUserAuthorizer.handle_auth_callback_deferred(request)
  redirect target_url
end

Node.js

Чтобы запустить этот пример:

  1. В API Console, добавьте URL локальной машины в список URL -адресов перенаправления. Например, добавьте http://localhost .
  2. Убедитесь, что у вас есть техническое обслуживание LTS, активные LTS или текущий выпуск установленного Node.js.
  3. Создайте новый каталог и измените его. Например:
    mkdir ~/nodejs-oauth2-example
    cd ~/nodejs-oauth2-example
  4. Установите клиентскую библиотеку Google API для node.js с помощью NPM :
    npm install googleapis
  5. Создайте файлы main.js со следующим контентом.
  6. Запустите пример:
    node .\main.js

main.js

const http = require('http');
const https = require('https');
const url = require('url');
const { google } = require('googleapis');
const crypto = require('crypto');
const express = require('express');
const session = require('express-session');

/**
 * To use OAuth2 authentication, we need access to a CLIENT_ID, CLIENT_SECRET, AND REDIRECT_URI.
 * To get these credentials for your application, visit
 * https://console.cloud.google.com/apis/credentials.
 */
const oauth2Client = new google.auth.OAuth2(
  YOUR_CLIENT_ID,
  YOUR_CLIENT_SECRET,
  YOUR_REDIRECT_URL
);

// Access scopes for two non-Sign-In scopes: Read-only Drive activity and Google Calendar.
const scopes = [
  'https://www.googleapis.com/auth/drive.metadata.readonly',
  'https://www.googleapis.com/auth/calendar.readonly'
];

/* Global variable that stores user credential in this code example.
 * ACTION ITEM for developers:
 *   Store user's refresh token in your data store if
 *   incorporating this code into your real app.
 *   For more information on handling refresh tokens,
 *   see https://github.com/googleapis/google-api-nodejs-client#handling-refresh-tokens
 */
let userCredential = null;

async function main() {
  const app = express();

  app.use(session({
    secret: 'your_secure_secret_key', // Replace with a strong secret
    resave: false,
    saveUninitialized: false,
  }));

  // Example on redirecting user to Google's OAuth 2.0 server.
  app.get('/', async (req, res) => {
    // Generate a secure random state value.
    const state = crypto.randomBytes(32).toString('hex');
    // Store state in the session
    req.session.state = state;

    // Generate a url that asks permissions for the Drive activity and Google Calendar scope
    const authorizationUrl = oauth2Client.generateAuthUrl({
      // 'online' (default) or 'offline' (gets refresh_token)
      access_type: 'offline',
      /** Pass in the scopes array defined above.
        * Alternatively, if only one scope is needed, you can pass a scope URL as a string */
      scope: scopes,
      // Enable incremental authorization. Recommended as a best practice.
      include_granted_scopes: true,
      // Include the state parameter to reduce the risk of CSRF attacks.
      state: state
    });

    res.redirect(authorizationUrl);
  });

  // Receive the callback from Google's OAuth 2.0 server.
  app.get('/oauth2callback', async (req, res) => {
    // Handle the OAuth 2.0 server response
    let q = url.parse(req.url, true).query;

    if (q.error) { // An error response e.g. error=access_denied
      console.log('Error:' + q.error);
    } else if (q.state !== req.session.state) { //check state value
      console.log('State mismatch. Possible CSRF attack');
      res.end('State mismatch. Possible CSRF attack');
    } else { // Get access and refresh tokens (if access_type is offline)
      let { tokens } = await oauth2Client.getToken(q.code);
      oauth2Client.setCredentials(tokens);

      /** Save credential to the global variable in case access token was refreshed.
        * ACTION ITEM: In a production app, you likely want to save the refresh token
        *              in a secure persistent database instead. */
      userCredential = tokens;
      
      // User authorized the request. Now, check which scopes were granted.
      if (tokens.scope.includes('https://www.googleapis.com/auth/drive.metadata.readonly'))
      {
        // User authorized read-only Drive activity permission.
        // Example of using Google Drive API to list filenames in user's Drive.
        const drive = google.drive('v3');
        drive.files.list({
          auth: oauth2Client,
          pageSize: 10,
          fields: 'nextPageToken, files(id, name)',
        }, (err1, res1) => {
          if (err1) return console.log('The API returned an error: ' + err1);
          const files = res1.data.files;
          if (files.length) {
            console.log('Files:');
            files.map((file) => {
              console.log(`${file.name} (${file.id})`);
            });
          } else {
            console.log('No files found.');
          }
        });
      }
      else
      {
        // User didn't authorize read-only Drive activity permission.
        // Update UX and application accordingly
      }

      // Check if user authorized Calendar read permission.
      if (tokens.scope.includes('https://www.googleapis.com/auth/calendar.readonly'))
      {
        // User authorized Calendar read permission.
        // Calling the APIs, etc.
      }
      else
      {
        // User didn't authorize Calendar read permission.
        // Update UX and application accordingly
      }
    }
  });

  // Example on revoking a token
  app.get('/revoke', async (req, res) => {
    // Build the string for the POST request
    let postData = "token=" + userCredential.access_token;

    // Options for POST request to Google's OAuth 2.0 server to revoke a token
    let postOptions = {
      host: 'oauth2.googleapis.com',
      port: '443',
      path: '/revoke',
      method: 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': Buffer.byteLength(postData)
      }
    };

    // Set up the request
    const postReq = https.request(postOptions, function (res) {
      res.setEncoding('utf8');
      res.on('data', d => {
        console.log('Response: ' + d);
      });
    });

    postReq.on('error', error => {
      console.log(error)
    });

    // Post the request with data
    postReq.write(postData);
    postReq.end();
  });


  const server = http.createServer(app);
  server.listen(8080);
}
main().catch(console.error);

Http/rest

В этом примере Python используется флажок Framework и библиотека запросов , чтобы продемонстрировать веб -поток OAuth 2.0. Мы рекомендуем использовать клиентскую библиотеку Google API для Python для этого потока. (Пример на вкладке Python использует клиентскую библиотеку.)

import json
import flask
import requests

app = flask.Flask(__name__)

# To get these credentials (CLIENT_ID CLIENT_SECRET) and for your application, visit
# https://console.cloud.google.com/apis/credentials.
CLIENT_ID = '123456789.apps.googleusercontent.com'
CLIENT_SECRET = 'abc123'  # Read from a file or environmental variable in a real app

# Access scopes for two non-Sign-In scopes: Read-only Drive activity and Google Calendar.
SCOPE = 'https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/calendar.readonly'

# Indicate where the API server will redirect the user after the user completes
# the authorization flow. The redirect URI is required. The value must exactly
# match one of the authorized redirect URIs for the OAuth 2.0 client, which you
# configured in the API Console. If this value doesn't match an authorized URI,
# you will get a 'redirect_uri_mismatch' error.
REDIRECT_URI = 'http://example.com/oauth2callback'

@app.route('/')
def index():
  if 'credentials' not in flask.session:
    return flask.redirect(flask.url_for('oauth2callback'))

  credentials = json.loads(flask.session['credentials'])

  if credentials['expires_in'] <= 0:
    return flask.redirect(flask.url_for('oauth2callback'))
  else: 
    # User authorized the request. Now, check which scopes were granted.
    if 'https://www.googleapis.com/auth/drive.metadata.readonly' in credentials['scope']:
      # User authorized read-only Drive activity permission.
      # Example of using Google Drive API to list filenames in user's Drive.
      headers = {'Authorization': 'Bearer {}'.format(credentials['access_token'])}
      req_uri = 'https://www.googleapis.com/drive/v2/files'
      r = requests.get(req_uri, headers=headers).text
    else:
      # User didn't authorize read-only Drive activity permission.
      # Update UX and application accordingly
      r = 'User did not authorize Drive permission.'

    # Check if user authorized Calendar read permission.
    if 'https://www.googleapis.com/auth/calendar.readonly' in credentials['scope']:
      # User authorized Calendar read permission.
      # Calling the APIs, etc.
      r += 'User authorized Calendar permission.'
    else:
      # User didn't authorize Calendar read permission.
      # Update UX and application accordingly
      r += 'User did not authorize Calendar permission.'

  return r

@app.route('/oauth2callback')
def oauth2callback():
  if 'code' not in flask.request.args:
    state = str(uuid.uuid4())
    flask.session['state'] = state
    # Generate a url that asks permissions for the Drive activity
    # and Google Calendar scope. Then, redirect user to the url.
    auth_uri = ('https://accounts.google.com/o/oauth2/v2/auth?response_type=code'
                '&client_id={}&redirect_uri={}&scope={}&state={}').format(CLIENT_ID, REDIRECT_URI,
                                                                          SCOPE, state)
    return flask.redirect(auth_uri)
  else:
    if 'state' not in flask.request.args or flask.request.args['state'] != flask.session['state']:
      return 'State mismatch. Possible CSRF attack.', 400

    auth_code = flask.request.args.get('code')
    data = {'code': auth_code,
            'client_id': CLIENT_ID,
            'client_secret': CLIENT_SECRET,
            'redirect_uri': REDIRECT_URI,
            'grant_type': 'authorization_code'}

    # Exchange authorization code for access and refresh tokens (if access_type is offline)
    r = requests.post('https://oauth2.googleapis.com/token', data=data)
    flask.session['credentials'] = r.text
    return flask.redirect(flask.url_for('index'))

if __name__ == '__main__':
  import uuid
  app.secret_key = str(uuid.uuid4())
  app.debug = False
  app.run()

Правила проверки URI перенаправить URI

Google применяет следующие правила проверки для перенаправления URI, чтобы помочь разработчикам обеспечить безопасность своих приложений. Ваш перенаправление URI должно придерживаться этих правил. См. RFC 3986 Раздел 3 для определения домена, хоста, пути, запроса, схемы и пользователя, упомянутого ниже.

Правила валидации
Схема

Перенаправление URI должно использовать схему HTTPS, а не просто http. Localhost URI (включая IP -адрес Localhost URIS) освобождаются от этого правила.

Хозяин

Хосты не могут быть необработанными IP -адресами. IP -адреса Localhost освобождены от этого правила.

Домен
  • Хост TLDS ( домены верхнего уровня ) должен принадлежать к списку общественного суффикса .
  • Домены хоста не могут быть “googleusercontent.com” .
  • Перенаправление URI не может содержать доменов сокращения URL (например, goo.gl ), если только приложение не владеет доменом. Кроме того, если приложение, которое владеет доменом сокращения, выбирает перенаправление на этот домен, это перенаправление URI должно содержать “/google-callback/” на своем пути или заканчиваться “/google-callback” .
  • UserInfo

    Перенаправление URI не может содержать подкомпонента пользователя.

    Путь

    Перенаправление URI не может содержать прохождения пути (также называемого каталогом), который представлен “/..” или “\..” или их URL -кодировкой.

    Запрос

    Перенаправление URI не может содержать открытые перенаправления .

    Фрагмент

    Перенаправление URI не может содержать компонент фрагмента.

    Персонажи Перенаправить URI не может содержать определенных символов, включая:
    • Персонажи с подстановочными знаками ( '*' )
    • Неприемные персонажи ASCII
    • Неверные процентные кодирования (любая процентная кодировка, которая не соответствует форме кодирования URL в процентном знаке, за которым следует две шестнадцатеричные цифры)
    • Нулевые символы (кодированный нулевый символ, например, %00 , %C0%80 )

    Постепенное разрешение

    В протоколе OAuth 2.0 ваше приложение запрашивает разрешение на доступ к ресурсам, которые идентифицируются с помощью областей. Это считается лучшей практикой пользователя для запроса авторизации для ресурсов в то время, когда они вам нужны. Чтобы включить эту практику, сервер авторизации Google поддерживает постепенное авторизацию. Эта функция позволяет вам запрашивать области по мере необходимости, и, если пользователь предоставляет разрешение на новую область, возвращает код авторизации, который может быть обменен на токен, содержащий все прицелы, которые пользователь предоставил проекту.

    Например, приложение, которое позволяет людям попробовать музыкальные треки и создавать миксы, может потребоваться очень мало ресурсов во время входа, возможно, не что иное, как название человека, подписывающегося. Однако сохранение заполненного микса потребует доступа к их драйве Google. Большинство людей найдут это естественным, если бы их только попросили получить доступ к своему Google Drive в то время, когда приложение на самом деле это нужно.

    В этом случае во время входа приложение может запросить openid и profile , чтобы выполнить базовую регистрацию, а затем запросить https://www.googleapis.com/auth/drive.file Scope во время первого запроса сохранения микса.

    Для реализации постепенного авторизации вы заполняете обычный поток для запроса токена доступа, но убедитесь, что запрос на авторизацию включает ранее предоставленные области. Этот подход позволяет вашему приложению избежать необходимости управлять несколькими токенами доступа.

    Следующие правила применяются к току доступа, полученным из постепенного разрешения:

    • Токен может использоваться для доступа к ресурсам, соответствующим любой из областей, внедренных в новую, комбинированную авторизацию.
    • Когда вы используете токен обновления для комбинированного разрешения для получения токена доступа, токен доступа представляет собой комбинированное авторизацию и может использоваться для любого из значений scope , включенных в ответ.
    • Комбинированное авторизация включает в себя все прицелы, которые пользователь предоставил проекту API, даже если гранты были запрошены у разных клиентов. Например, если пользователь предоставил доступ к одной области с использованием настольного клиента приложения, а затем предоставил еще одну область одному и тому же приложению через мобильный клиент, комбинированное авторизация будет включать в себя оба применения.
    • Если вы отмените токен, который представляет собой комбинированное разрешение, доступ ко всем прицелам авторизации от имени связанного пользователя отозвана одновременно.

    Образцы кода, специфичного для конкретного языка, на шаге 1: Установите параметры авторизации и образец http/rest перенаправляют URL на шаге 2: перенаправление на сервер Google OAuth 2.0- все используют инкрементную авторизацию. Приведенные ниже образцы кода также показывают код, который вам необходимо добавить для использования инкрементной авторизации.

    PHP

    $client->setIncludeGrantedScopes(true);

    Питон

    В Python установите аргумент include_granted_scopes ключевой слов в true , чтобы убедиться, что запрос на авторизацию включает в себя ранее предоставленные области. Вполне возможно, что include_granted_scopes не будет единственным аргументом ключевого слова, который вы установили, как показано в примере ниже.

    authorization_url, state = flow.authorization_url(
        # Enable offline access so that you can refresh an access token without
        # re-prompting the user for permission. Recommended for web server apps.
        access_type='offline',
        # Enable incremental authorization. Recommended as a best practice.
        include_granted_scopes='true')

    Руби

    auth_client.update!(
      :additional_parameters => {"include_granted_scopes" => "true"}
    )

    Node.js

    const authorizationUrl = oauth2Client.generateAuthUrl({
      // 'online' (default) or 'offline' (gets refresh_token)
      access_type: 'offline',
      /** Pass in the scopes array defined above.
        * Alternatively, if only one scope is needed, you can pass a scope URL as a string */
      scope: scopes,
      // Enable incremental authorization. Recommended as a best practice.
      include_granted_scopes: true
    });

    Http/rest

    GET https://accounts.google.com/o/oauth2/v2/auth?
      client_id=your_client_id&
      response_type=code&
      state=state_parameter_passthrough_value&
      scope=https%3A//www.googleapis.com/auth/drive.metadata.readonly%20https%3A//www.googleapis.com/auth/calendar.readonly&
      redirect_uri=https%3A//oauth2.example.com/code&
      prompt=consent&
      include_granted_scopes=true

    Обновлять токен доступа (автономный доступ)

    Периодически истекают токены доступа и становятся недействительными учетными данными для соответствующего запроса API. Вы можете обновить токен доступа, не побуждая пользователя на разрешение (включая, когда пользователь не присутствует), если вы запросили автономный доступ к областям, связанным с токеном.

    • Если вы используете клиентскую библиотеку Google API, объект клиента обновляет токен доступа по мере необходимости, если вы настраиваете этот объект для автономного доступа.
    • Если вы не используете клиентскую библиотеку, вам необходимо установить параметр запроса http access_type для offline при перенаправлении пользователя на сервер Google OAuth 2.0 . В этом случае сервер авторизации Google возвращает токен обновления при обмене кода авторизации на токен доступа. Затем, если истекает токен доступа (или в любое другое время), вы можете использовать токен обновления для получения нового токена доступа.

    Запрос о автономном доступе является требованием для любого приложения, которому необходимо получить доступ к API Google, когда пользователя не присутствует. Например, приложение, которое выполняет службы резервного копирования или выполняет действия в заранее определенное время, должно иметь возможность обновить свой токен доступа, когда пользователя нет. Стиль доступа по умолчанию называется online .

    Серверные веб-приложения, установленные приложения и устройства получают токены обновления в процессе авторизации. Токены обновления обычно не используются в веб-приложениях на стороне клиента (JavaScript).

    PHP

    Если ваше приложение нуждается в автономном доступе к API Google, установите тип доступа клиента API на offline :

    $client->setAccessType("offline");

    После того, как пользователь предоставляет автономный доступ к запрошенным областям, вы можете продолжать использовать клиент API для доступа к API Google от имени пользователя, когда пользователь не работает. Клиентский объект обновляет токен доступа по мере необходимости.

    Питон

    В Python установите аргумент ключевого слова access_type в offline , чтобы убедиться, что вы сможете обновить токен Access, не заново производя пользователя для разрешения. Вполне возможно, что access_type не будет единственным аргументом ключевого слова, который вы установили, как показано в примере ниже.

    authorization_url, state = flow.authorization_url(
        # Enable offline access so that you can refresh an access token without
        # re-prompting the user for permission. Recommended for web server apps.
        access_type='offline',
        # Enable incremental authorization. Recommended as a best practice.
        include_granted_scopes='true')

    После того, как пользователь предоставляет автономный доступ к запрошенным областям, вы можете продолжать использовать клиент API для доступа к API Google от имени пользователя, когда пользователь не работает. Клиентский объект обновляет токен доступа по мере необходимости.

    Руби

    Если ваше приложение нуждается в автономном доступе к API Google, установите тип доступа клиента API на offline :

    auth_client.update!(
      :additional_parameters => {"access_type" => "offline"}
    )

    После того, как пользователь предоставляет автономный доступ к запрошенным областям, вы можете продолжать использовать клиент API для доступа к API Google от имени пользователя, когда пользователь не работает. Клиентский объект обновляет токен доступа по мере необходимости.

    Node.js

    Если ваше приложение нуждается в автономном доступе к API Google, установите тип доступа клиента API на offline :

    const authorizationUrl = oauth2Client.generateAuthUrl({
      // 'online' (default) or 'offline' (gets refresh_token)
      access_type: 'offline',
      /** Pass in the scopes array defined above.
        * Alternatively, if only one scope is needed, you can pass a scope URL as a string */
      scope: scopes,
      // Enable incremental authorization. Recommended as a best practice.
      include_granted_scopes: true
    });

    После того, как пользователь предоставляет автономный доступ к запрошенным областям, вы можете продолжать использовать клиент API для доступа к API Google от имени пользователя, когда пользователь не работает. Клиентский объект обновляет токен доступа по мере необходимости.

    Доступ токенов истекает. Эта библиотека автоматически использует токен обновления для получения нового токена доступа, если он собирается истекать. Простой способ убедиться, что вы всегда храните самые последние жетоны, - это использовать событие Tokens:

    oauth2Client.on('tokens', (tokens) => {
      if (tokens.refresh_token) {
        // store the refresh_token in your secure persistent database
        console.log(tokens.refresh_token);
      }
      console.log(tokens.access_token);
    });

    Это событие токенов происходит только в первом авторизации, и вам необходимо установить свой access_type на offline при вызове метода generateAuthUrl для получения токена обновления. Если вы уже предоставили своему приложению разрешения для регистрации, не устанавливая соответствующие ограничения для получения токена обновления, вам нужно будет повторно создать приложение для получения свежего токена обновления.

    Чтобы установить refresh_token позже, вы можете использовать метод setCredentials :

    oauth2Client.setCredentials({
      refresh_token: `STORED_REFRESH_TOKEN`
    });

    После того, как у клиента появится токен обновления, доступные токены будут приобретены и автоматически обновляться при следующем вызове API.

    Http/rest

    Чтобы обновить токен доступа, ваше приложение отправляет запрос POST HTTPS на сервер авторизации Google ( https://oauth2.googleapis.com/token ), который включает в себя следующие параметры:

    Поля
    client_id Идентификатор клиента, полученный из API Console.
    client_secret Секрет клиента, полученный из API Console.
    grant_type Как определено в спецификации OAuth 2.0 , значение этого поля должно быть установлено на refresh_token .
    refresh_token Токен обновления возвращается с обмена кодами авторизации.

    Следующий фрагмент показывает образец запроса:

    POST /token HTTP/1.1
    Host: oauth2.googleapis.com
    Content-Type: application/x-www-form-urlencoded
    
    client_id=your_client_id&
    client_secret=your_client_secret&
    refresh_token=refresh_token&
    grant_type=refresh_token

    Пока пользователь не отозвал доступ к приложению, сервер Token возвращает объект JSON, который содержит новый токен доступа. Следующий фрагмент показывает образец ответа:

    {
      "access_token": "1/fFAGRNJru1FTz70BzhT3Zg",
      "expires_in": 3920,
      "scope": "https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/calendar.readonly",
      "token_type": "Bearer"
    }

    Обратите внимание, что существуют ограничения на количество токенов обновления, которые будут выпущены; Один лимит на комбинацию клиента/пользователя, а другой на одного пользователя для всех клиентов. Вы должны сохранить токены обновления в долгосрочном хранении и продолжать использовать их, пока они остаются действительными. Если ваше приложение запрашивает слишком много токенов обновления, оно может столкнуться с этими пределами, и в этом случае старые токены обновления перестают работать.

    Отмена токена

    В некоторых случаях пользователь может захотеть отменить доступ к приложению. Пользователь может отозвать доступ, посетив настройки учетной записи . См. Раздел «Удалить сайт» или «Доступ к приложениям» сторонних сайтов и приложений с доступом к документу поддержки вашей учетной записи для получения дополнительной информации.

    Также возможно приложение программно отозвать предоставленный ему доступ. Программное отзыв важно в случаях, когда пользователь отписывает подпись, удаляет приложение или ресурсы API, необходимые приложению, значительно изменились. Другими словами, часть процесса удаления может включать запрос API для обеспечения удаления разрешений, ранее предоставленных заявлению.

    PHP

    Чтобы программно отозвать токен, позвоните revokeToken() :

    $client->revokeToken();

    Питон

    Чтобы программно отозвать токен, сделайте запрос на https://oauth2.googleapis.com/revoke , который включает токен как параметр и устанавливает заголовок Content-Type :

    requests.post('https://oauth2.googleapis.com/revoke',
        params={'token': credentials.token},
        headers = {'content-type': 'application/x-www-form-urlencoded'})

    Руби

    Чтобы программно отозвать токен, сделайте HTTP -запрос в конечную точку oauth2.revoke :

    uri = URI('https://oauth2.googleapis.com/revoke')
    response = Net::HTTP.post_form(uri, 'token' => auth_client.access_token)

    Токен может быть токеном доступа или токеном обновления. Если токен является токеном доступа и имеет соответствующий токен обновления, токен обновления также будет отменен.

    Если отзыв успешно обрабатывается, то код состояния ответа составляет 200 . Для условий ошибок возвращается код состояния 400 вместе с кодом ошибки.

    Node.js

    Чтобы программно отозвать токен, сделайте запрос HTTPS POST на /revoke конечную точку:

    const https = require('https');
    
    // Build the string for the POST request
    let postData = "token=" + userCredential.access_token;
    
    // Options for POST request to Google's OAuth 2.0 server to revoke a token
    let postOptions = {
      host: 'oauth2.googleapis.com',
      port: '443',
      path: '/revoke',
      method: 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': Buffer.byteLength(postData)
      }
    };
    
    // Set up the request
    const postReq = https.request(postOptions, function (res) {
      res.setEncoding('utf8');
      res.on('data', d => {
        console.log('Response: ' + d);
      });
    });
    
    postReq.on('error', error => {
      console.log(error)
    });
    
    // Post the request with data
    postReq.write(postData);
    postReq.end();

    Параметр токена может быть токеном доступа или токеном обновления. Если токен является токеном доступа и имеет соответствующий токен обновления, токен обновления также будет отменен.

    Если отзыв успешно обрабатывается, то код состояния ответа составляет 200 . Для условий ошибок возвращается код состояния 400 вместе с кодом ошибки.

    Http/rest

    Чтобы программно отозвать токен, ваше приложение делает запрос на https://oauth2.googleapis.com/revoke и включает токен в качестве параметра:

    curl -d -X -POST --header "Content-type:application/x-www-form-urlencoded" \
            https://oauth2.googleapis.com/revoke?token={token}

    Токен может быть токеном доступа или токеном обновления. Если токен является токеном доступа и имеет соответствующий токен обновления, токен обновления также будет отменен.

    Если отзыв успешно обрабатывается, то код состояния HTTP ответа составляет 200 . Для условий ошибок возвращается код состояния HTTP 400 вместе с кодом ошибки.

    Основанный на времени доступ

    Доступ на основе времени позволяет пользователю предоставить вашему приложению доступ к своим данным для ограниченной продолжительности для завершения действия. Доступ на основе времени доступен в избранных продуктах Google во время потока согласия, предоставляя пользователям возможность предоставить доступ в течение ограниченного периода времени. Примером является API портативности данных , который обеспечивает одноразовую передачу данных.

    Когда пользователь предоставляет доступ к вашему приложению, токен обновления истекает после указанной продолжительности. Обратите внимание, что токены обновления могут быть недействительными ранее при конкретных обстоятельствах; Смотрите эти случаи для деталей. Поле refresh_token_expires_in возвращаемое в ответе обмена кода авторизации, представляет время, оставшееся до тех пор, пока в таких случаях не истечет срок действия токена обновления.

    Реализация защиты от поперечного доступа

    Дополнительным шагом, который вы должны предпринять для защиты учетных записей ваших пользователей, является реализация защиты от аккунт, используя службу защиты Google Cross-account. Эта услуга позволяет подписаться на уведомления о событиях безопасности, которые предоставляют информацию в вашем приложении о серьезных изменениях в учетной записи пользователя. Затем вы можете использовать информацию для принятия мер в зависимости от того, как вы решите ответить на события.

    Некоторые примеры типов событий, отправленных в ваше приложение, с помощью службы защиты Google по перекрестному доступе:

    • https://schemas.openid.net/secevent/risc/event-type/sessions-revoked
    • https://schemas.openid.net/secevent/oauth/event-type/token-revoked
    • https://schemas.openid.net/secevent/risc/event-type/account-disabled

    См. Защита пользовательских учетных записей на странице защиты Cross-Account для получения дополнительной информации о том, как реализовать защиту учетных записей и полный список доступных событий.

    ,

    В этом документе объясняется, как приложения веб -серверов используют клиентские библиотеки Google API или конечные точки Google OAuth 2.0 для реализации авторизации OAuth 2.0 для доступа к API Google.

    OAuth 2.0 позволяет пользователям обмениваться конкретными данными с приложением, сохраняя при этом свои имена пользователей, пароли и другую информацию частной. Например, приложение может использовать OAuth 2.0 для получения разрешения от пользователей на хранение файлов в своих дисках Google.

    Этот поток OAuth 2.0 специально предназначен для авторизации пользователя. Он предназначен для приложений, которые могут хранить конфиденциальную информацию и поддерживать состояние. Правильно авторизованное приложение веб -сервера может получить доступ к API, в то время как пользователь взаимодействует с приложением или после того, как пользователь покинул приложение.

    Приложения веб-сервера часто также используют учетные записи служб для авторизации запросов API, особенно при вызове облачных API для доступа к данным на основе проектов, а не для пользовательских данных. Приложения веб -сервера могут использовать учетные записи услуг в сочетании с авторизацией пользователя.

    Клиентские библиотеки

    Примеры для конкретного языка на этой странице используют клиентские библиотеки Google API для реализации авторизации OAuth 2.0. Чтобы запустить образцы кода, вы должны сначала установить клиентскую библиотеку для вашего языка.

    Когда вы используете клиентскую библиотеку Google API для обработки потока вашего приложения OAuth 2.0, клиентская библиотека выполняет множество действий, которые в противном случае приложением потребовалось бы самостоятельно. Например, он определяет, когда приложение может использовать или обновлять хранимые токены доступа, а также когда приложение должно повторно повторно согласиться. Клиентская библиотека также генерирует правильные URL -адреса перенаправления и помогает реализовать обработчики перенаправления, которые обмениваются кодами авторизации на токены доступа.

    Клиентские библиотеки Google API для приложений на стороне сервера доступны для следующих языков:

    Предварительные условия

    Включить API для вашего проекта

    Любое приложение, которое вызывает Google API, необходимо включить эти API в API Console.

    Чтобы включить API для вашего проекта:

    1. Open the API Library в Google API Console.
    2. If prompted, select a project, or create a new one.
    3. API Library Перечисляет все доступные API, сгруппированные по семейству продуктов и популярности. Если API, который вы хотите включить, не виден в списке, используйте поиск, чтобы найти его, или нажмите « Просмотреть все в семействе продуктов».
    4. Выберите API, который вы хотите включить, затем нажмите кнопку «Включить» .
    5. If prompted, enable billing.
    6. If prompted, read and accept the API's Terms of Service.

    Создать учетные данные авторизации

    Любое приложение, которое использует OAuth 2.0 для доступа к API Google, должно иметь учетные данные авторизации, которые идентифицируют приложение на сервере Google OAuth 2.0. Следующие шаги объясняют, как создать учетные данные для вашего проекта. Затем ваши приложения могут использовать учетные данные для доступа к API, которые вы включили для этого проекта.

    1. Go to the Credentials page.
    2. Нажмите «Создать клиент» .
    3. Выберите тип приложения веб -приложения .
    4. Заполните форму и нажмите «Создать» . Приложения, которые используют языки и структуры, такие как PHP, Java, Python, Ruby и .net, должны указывать авторизованное перенаправление URI . Redirect URI - это конечные точки, на которые сервер OAuth 2.0 может отправлять ответы. Эти конечные точки должны придерживаться правил проверки Google .

      Для тестирования вы можете указать URI, которые относятся к локальной машине, такой как http://localhost:8080 . Имея это в виду, обратите внимание, что все примеры в этом документе используют http://localhost:8080 в качестве перенаправления URI.

      Мы рекомендуем вам разрабатывать конечные точки авторитета вашего приложения, чтобы ваше приложение не подвергало коды авторизации другим ресурсам на странице.

    После создания ваших учетных данных загрузите файл client_secret.json API Console. Безопасно хранить файл в месте, к которому может получить доступ только ваше приложение.

    Определите сферу доступа

    Области позволяют вашему приложению запросить только доступ к ресурсам, в котором ему нужны, а также позволяет пользователям контролировать объем доступа, который они предоставляют вашему приложению. Таким образом, может быть обратная связь между количеством запрашиваемых областей и вероятностью получения согласия пользователя.

    Прежде чем вы начнете реализовать авторизацию OAuth 2.0, мы рекомендуем вам определить, что вашему приложению потребуется разрешение на доступ.

    Мы также рекомендуем доступ к вашему запросу приложения к областям авторизации через процесс инкрементного авторизации , в котором ваше приложение запрашивает доступ к пользовательским данным в контексте. Эта лучшая практика помогает пользователям легче понять, почему вашему приложению нужен доступ, который он запрашивает.

    Документ API API OAuth 2.0 содержит полный список областей, которые вы можете использовать для доступа к API Google.

    Требования для конкретных языков

    Чтобы запустить любой из образцов кода в этом документе, вам понадобится учетная запись Google, доступ к Интернету и веб -браузер. Если вы используете одну из клиентских библиотек API, также см. Приведенные ниже требования к конкретному языку.

    PHP

    Чтобы запустить образцы кода PHP в этом документе, вам понадобится:

    • PHP 8.0 или более установлен с помощью интерфейса командной строки (CLI) и расширения JSON.
    • Инструмент управления зависимостями композитора .
    • Клиентская библиотека Google APIS для PHP:

      composer require google/apiclient:^2.15.0

    Смотрите клиентскую библиотеку Google APIS для PHP для получения дополнительной информации.

    Питон

    Чтобы запустить образцы кода Python в этом документе, вам понадобится:

    • Python 3.7 или более
    • Инструмент управления пакетами PIP .
    • Библиотека клиентов Google APIS для выпуска Python 2.0:
      pip install --upgrade google-api-python-client
    • google-auth , google-auth-oauthlib и google-auth-httplib2 для авторизации пользователя.
      pip install --upgrade google-auth google-auth-oauthlib google-auth-httplib2
    • Flask Python Web Application Framework.
      pip install --upgrade flask
    • requests http библиотеки.
      pip install --upgrade requests

    Просмотрите Google API Python Client Library Note, если вы не можете обновить Python и соответствующее руководство по миграции.

    Руби

    Чтобы запустить образцы кода Ruby в этом документе, вам понадобится:

    • Ruby 2,6 или более
    • Библиотека Google Auth для Ruby:

      gem install googleauth
    • Клиентские библиотеки для Drive и Calendar Google API:

      gem install google-apis-drive_v3 google-apis-calendar_v3
    • Синатра Рубинская веб -прикладная структура.

      gem install sinatra

    Node.js

    Чтобы запустить образцы кода node.js в этом документе, вам понадобится:

    • Техническое обслуживание LT, активное LTS или ток Node.js.
    • Google APIS Node.js Client:

      npm install googleapis crypto express express-session

    Http/rest

    Вам не нужно устанавливать какие -либо библиотеки, чтобы иметь возможность напрямую вызывать конечные точки OAuth 2.0.

    Получение токенов доступа OAuth 2.0

    Следующие шаги показывают, как ваше приложение взаимодействует с сервером Google OAuth 2.0, чтобы получить согласие пользователя на выполнение запроса API от имени пользователя. Ваше приложение должно иметь это согласие, прежде чем он сможет выполнить запрос Google API, который требует авторизации пользователя.

    Список ниже быстро суммирует эти шаги:

    1. Ваше приложение определяет необходимые ему разрешения.
    2. Ваше приложение перенаправляет пользователя в Google вместе со списком запрошенных разрешений.
    3. Пользователь решает, предоставить ли разрешения вашей заявке.
    4. Ваше приложение узнает, что решил пользователь.
    5. Если пользователь предоставил запрошенные разрешения, ваше приложение получает токены, необходимые для выполнения запросов API от имени пользователя.

    Шаг 1: Установите параметры авторизации

    Ваш первый шаг - создать запрос на авторизацию. Этот запрос устанавливает параметры, которые идентифицируют ваше приложение, и определяют разрешения, которые пользователю будет предложено предоставить вашей заявке.

    • Если вы используете клиентскую библиотеку Google для аутентификации и авторизации OAuth 2.0, вы создаете и настраиваете объект, который определяет эти параметры.
    • Если вы позвоните в конечную точку Google OAuth 2.0 напрямую, вы создадите URL и установите параметры на этом URL.

    Вкладки ниже определяют поддерживаемые параметры авторизации для приложений веб -сервера. Примеры для конкретного языка также показывают, как использовать клиентскую библиотеку или библиотеку авторизации для настройки объекта, который устанавливает эти параметры.

    PHP

    Следующий фрагмент кода создает объект Google\Client() , который определяет параметры в запросе авторизации.

    Этот объект использует информацию из вашего файла client_secret.json для идентификации вашего приложения. (См. Создание учетных данных авторизации для получения дополнительной информации об этом файле.) Объект также идентифицирует приложения, которые ваше приложение запрашивает разрешение на доступ, и URL -адрес для конечной точки авторитета вашего приложения, который будет обрабатывать ответ с сервера Google OAuth 2.0. Наконец, код устанавливает необязательные параметры access_type и include_granted_scopes .

    Например, этот код запрашивает только для чтения, автономный доступ к метаданным Google Drive пользователя и событиях календаря:

    use Google\Client;
    
    $client = new Client();
    
    // Required, call the setAuthConfig function to load authorization credentials from
    // client_secret.json file.
    $client->setAuthConfig('client_secret.json');
    
    // Required, to set the scope value, call the addScope function
    $client->addScope([Google\Service\Drive::DRIVE_METADATA_READONLY, Google\Service\Calendar::CALENDAR_READONLY]);
    
    // Required, call the setRedirectUri function to specify a valid redirect URI for the
    // provided client_id
    $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php');
    
    // Recommended, offline access will give you both an access and refresh token so that
    // your app can refresh the access token without user interaction.
    $client->setAccessType('offline');
    
    // Recommended, call the setState function. Using a state value can increase your assurance that
    // an incoming connection is the result of an authentication request.
    $client->setState($sample_passthrough_value);
    
    // Optional, if your application knows which user is trying to authenticate, it can use this
    // parameter to provide a hint to the Google Authentication Server.
    $client->setLoginHint('hint@example.com');
    
    // Optional, call the setPrompt function to set "consent" will prompt the user for consent
    $client->setPrompt('consent');
    
    // Optional, call the setIncludeGrantedScopes function with true to enable incremental
    // authorization
    $client->setIncludeGrantedScopes(true);

    Питон

    В следующем фрагменте кода используется модуль google-auth-oauthlib.flow для построения запроса авторизации.

    Код создает объект Flow , который идентифицирует ваше приложение, используя информацию из файла client_secret.json , который вы загрузили после создания учетных данных авторизации . Этот объект также идентифицирует применение, которые ваше приложение запрашивает разрешение на доступ к доступу, и URL -адрес для конечной точки авторитета вашего приложения, который будет обрабатывать ответ с сервера Google OAuth 2.0. Наконец, код устанавливает необязательные параметры access_type и include_granted_scopes .

    Например, этот код запрашивает только для чтения, автономный доступ к метаданным Google Drive пользователя и событиях календаря:

    import google.oauth2.credentials
    import google_auth_oauthlib.flow
    
    # Required, call the from_client_secrets_file method to retrieve the client ID from a
    # client_secret.json file. The client ID (from that file) and access scopes are required. (You can
    # also use the from_client_config method, which passes the client configuration as it originally
    # appeared in a client secrets file but doesn't access the file itself.)
    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file('client_secret.json',
        scopes=['https://www.googleapis.com/auth/drive.metadata.readonly',
                'https://www.googleapis.com/auth/calendar.readonly'])
    
    # Required, indicate where the API server will redirect the user after the user completes
    # the authorization flow. The redirect URI is required. The value must exactly
    # match one of the authorized redirect URIs for the OAuth 2.0 client, which you
    # configured in the API Console. If this value doesn't match an authorized URI,
    # you will get a 'redirect_uri_mismatch' error.
    flow.redirect_uri = 'https://www.example.com/oauth2callback'
    
    # Generate URL for request to Google's OAuth 2.0 server.
    # Use kwargs to set optional request parameters.
    authorization_url, state = flow.authorization_url(
        # Recommended, enable offline access so that you can refresh an access token without
        # re-prompting the user for permission. Recommended for web server apps.
        access_type='offline',
        # Optional, enable incremental authorization. Recommended as a best practice.
        include_granted_scopes='true',
        # Optional, if your application knows which user is trying to authenticate, it can use this
        # parameter to provide a hint to the Google Authentication Server.
        login_hint='hint@example.com',
        # Optional, set prompt to 'consent' will prompt the user for consent
        prompt='consent')

    Руби

    Используйте файл client_secrets.json, который вы создали для настройки клиентского объекта в вашем приложении. Когда вы настраиваете клиент -объект, вы указываете общие области, которые необходимо получить к вашему приложению, вместе с URL -адресом в конечную точку авторитета вашего приложения, которые будут обрабатывать ответ с сервера OAuth 2.0.

    Например, этот код запрашивает только для чтения, автономный доступ к метаданным Google Drive пользователя и событиях календаря:

    require 'googleauth'
    require 'googleauth/web_user_authorizer'
    require 'googleauth/stores/redis_token_store'
    
    require 'google/apis/drive_v3'
    require 'google/apis/calendar_v3'
    
    # Required, call the from_file method to retrieve the client ID from a
    # client_secret.json file.
    client_id = Google::Auth::ClientId.from_file('/path/to/client_secret.json')
    
    # Required, scope value 
    # Access scopes for two non-Sign-In scopes: Read-only Drive activity and Google Calendar.
    scope = ['Google::Apis::DriveV3::AUTH_DRIVE_METADATA_READONLY',
             'Google::Apis::CalendarV3::AUTH_CALENDAR_READONLY']
    
    # Required, Authorizers require a storage instance to manage long term persistence of
    # access and refresh tokens.
    token_store = Google::Auth::Stores::RedisTokenStore.new(redis: Redis.new)
    
    # Required, indicate where the API server will redirect the user after the user completes
    # the authorization flow. The redirect URI is required. The value must exactly
    # match one of the authorized redirect URIs for the OAuth 2.0 client, which you
    # configured in the API Console. If this value doesn't match an authorized URI,
    # you will get a 'redirect_uri_mismatch' error.
    callback_uri = '/oauth2callback'
    
    # To use OAuth2 authentication, we need access to a CLIENT_ID, CLIENT_SECRET, AND REDIRECT_URI
    # from the client_secret.json file. To get these credentials for your application, visit
    # https://console.cloud.google.com/apis/credentials.
    authorizer = Google::Auth::WebUserAuthorizer.new(client_id, scope,
                                                    token_store, callback_uri)

    В вашем приложении используется клиент -объект для выполнения операций OAuth 2.0, таких как создание URL -адресов запроса на авторизацию и применение токенов доступа к HTTP -запросам.

    Node.js

    Следующий фрагмент кода создает объект google.auth.OAuth2 , который определяет параметры в запросе авторизации.

    Этот объект использует информацию из вашего файла client_secret.json для идентификации вашего приложения. Чтобы попросить разрешения у пользователя для получения токена доступа, вы перенаправляете их на страницу согласия. Чтобы создать URL страницы согласия:

    const {google} = require('googleapis');
    const crypto = require('crypto');
    const express = require('express');
    const session = require('express-session');
    
    /**
     * To use OAuth2 authentication, we need access to a CLIENT_ID, CLIENT_SECRET, AND REDIRECT_URI
     * from the client_secret.json file. To get these credentials for your application, visit
     * https://console.cloud.google.com/apis/credentials.
     */
    const oauth2Client = new google.auth.OAuth2(
      YOUR_CLIENT_ID,
      YOUR_CLIENT_SECRET,
      YOUR_REDIRECT_URL
    );
    
    // Access scopes for two non-Sign-In scopes: Read-only Drive activity and Google Calendar.
    const scopes = [
      'https://www.googleapis.com/auth/drive.metadata.readonly',
      'https://www.googleapis.com/auth/calendar.readonly'
    ];
    
    // Generate a secure random state value.
    const state = crypto.randomBytes(32).toString('hex');
    
    // Store state in the session
    req.session.state = state;
    
    // Generate a url that asks permissions for the Drive activity and Google Calendar scope
    const authorizationUrl = oauth2Client.generateAuthUrl({
      // 'online' (default) or 'offline' (gets refresh_token)
      access_type: 'offline',
      /** Pass in the scopes array defined above.
        * Alternatively, if only one scope is needed, you can pass a scope URL as a string */
      scope: scopes,
      // Enable incremental authorization. Recommended as a best practice.
      include_granted_scopes: true,
      // Include the state parameter to reduce the risk of CSRF attacks.
      state: state
    });

    Важное примечание - refresh_token возвращается только на первом разрешении. Подробнее здесь .

    Http/rest

    Конечная точка Google OAuth 2.0 находится по адресу https://accounts.google.com/o/oauth2/v2/auth . Эта конечная точка доступна только по HTTPS. Простые http -соединения отказываются.

    Сервер авторизации Google поддерживает следующие параметры строки запроса для приложений веб -сервера:

    Параметры
    client_id Необходимый

    Идентификатор клиента для вашего приложения. Вы можете найти это значение в .

    redirect_uri Необходимый

    Определяет, где сервер API перенаправляет пользователя после того, как пользователь завершает поток авторизации. Значение должно точно соответствовать одному из авторизованных URI Redirect для клиента OAuth 2.0, который вы настроили в своем клиенте . Если это значение не соответствует авторизованному URI Redirect для предоставленного client_id , вы получите ошибку redirect_uri_mismatch .

    Обратите внимание, что схема http или https , корпус и Trailing Slash (' / ') должны соответствовать.

    response_type Необходимый

    Определяет, возвращает ли конечная точка Google OAuth 2.0 код авторизации.

    Установите значение параметра в code для приложений веб -сервера.

    scope Необходимый

    Список областей областей, которые определяют ресурсы, к которым может получить доступ к вашему приложению от имени пользователя. Эти значения информируют экран согласия, который Google отображает пользователю.

    Области позволяют вашему приложению запросить только доступ к ресурсам, в котором ему нужны, а также позволяет пользователям контролировать объем доступа, который они предоставляют вашему приложению. Таким образом, существует обратная связь между количеством запрашиваемых областей и вероятностью получения согласия пользователя.

    Мы рекомендуем, чтобы ваш запрос на заявку в приложении, когда это возможно, в контексте. Запрашивая доступ к пользовательским данным в контексте, посредством инкрементной авторизации , вы помогаете пользователям легче понять, зачем вашему приложению требуется доступ, о котором он запрашивает.

    access_type Рекомендуется

    Indicates whether your application can refresh access tokens when the user is not present at the browser. Valid parameter values are online , which is the default value, and offline .

    Set the value to offline if your application needs to refresh access tokens when the user is not present at the browser. This is the method of refreshing access tokens described later in this document. This value instructs the Google authorization server to return a refresh token and an access token the first time that your application exchanges an authorization code for tokens.

    state Рекомендуется

    Specifies any string value that your application uses to maintain state between your authorization request and the authorization server's response. The server returns the exact value that you send as a name=value pair in the URL query component ( ? ) of the redirect_uri after the user consents to or denies your application's access request.

    You can use this parameter for several purposes, such as directing the user to the correct resource in your application, sending nonces, and mitigating cross-site request forgery. Since your redirect_uri can be guessed, using a state value can increase your assurance that an incoming connection is the result of an authentication request. If you generate a random string or encode the hash of a cookie or another value that captures the client's state, you can validate the response to additionally ensure that the request and response originated in the same browser, providing protection against attacks such as cross-site request forgery . See the OpenID Connect documentation for an example of how to create and confirm a state token.

    include_granted_scopes Необязательный

    Enables applications to use incremental authorization to request access to additional scopes in context. If you set this parameter's value to true and the authorization request is granted, then the new access token will also cover any scopes to which the user previously granted the application access. See the incremental authorization section for examples.

    login_hint Необязательный

    If your application knows which user is trying to authenticate, it can use this parameter to provide a hint to the Google Authentication Server. The server uses the hint to simplify the login flow either by prefilling the email field in the sign-in form or by selecting the appropriate multi-login session.

    Set the parameter value to an email address or sub identifier, which is equivalent to the user's Google ID.

    prompt Необязательный

    A space-delimited, case-sensitive list of prompts to present the user. If you don't specify this parameter, the user will be prompted only the first time your project requests access. See Prompting re-consent for more information.

    Возможные значения:

    none Do not display any authentication or consent screens. Must not be specified with other values.
    consent Prompt the user for consent.
    select_account Prompt the user to select an account.

    Step 2: Redirect to Google's OAuth 2.0 server

    Redirect the user to Google's OAuth 2.0 server to initiate the authentication and authorization process. Typically, this occurs when your application first needs to access the user's data. In the case of incremental authorization , this step also occurs when your application first needs to access additional resources that it does not yet have permission to access.

    PHP

    1. Generate a URL to request access from Google's OAuth 2.0 server:
      $auth_url = $client->createAuthUrl();
    2. Redirect the user to $auth_url :
      header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));

    Питон

    This example shows how to redirect the user to the authorization URL using the Flask web application framework:

    return flask.redirect(authorization_url)

    Руби

    1. Generate a URL to request access from Google's OAuth 2.0 server:
      auth_uri = authorizer.get_authorization_url(request: request)
    2. Redirect the user to auth_uri .

    Node.js

    1. Use the generated URL authorizationUrl from Step 1 generateAuthUrl method to request access from Google's OAuth 2.0 server.
    2. Redirect the user to authorizationUrl .
      res.redirect(authorizationUrl);

    HTTP/REST

    Sample redirect to Google's authorization server

    An example URL is shown below, with line breaks and spaces for readability.

    https://accounts.google.com/o/oauth2/v2/auth?
     scope=https%3A//www.googleapis.com/auth/drive.metadata.readonly%20https%3A//www.googleapis.com/auth/calendar.readonly&
     access_type=offline&
     include_granted_scopes=true&
     response_type=code&
     state=state_parameter_passthrough_value&
     redirect_uri=https%3A//oauth2.example.com/code&
     client_id=client_id

    After you create the request URL, redirect the user to it.

    Google's OAuth 2.0 server authenticates the user and obtains consent from the user for your application to access the requested scopes. The response is sent back to your application using the redirect URL you specified.

    Step 3: Google prompts user for consent

    In this step, the user decides whether to grant your application the requested access. At this stage, Google displays a consent window that shows the name of your application and the Google API services that it is requesting permission to access with the user's authorization credentials and a summary of the scopes of access to be granted. The user can then consent to grant access to one or more scopes requested by your application or refuse the request.

    Your application doesn't need to do anything at this stage as it waits for the response from Google's OAuth 2.0 server indicating whether any access was granted. That response is explained in the following step.

    Ошибки

    Requests to Google's OAuth 2.0 authorization endpoint may display user-facing error messages instead of the expected authentication and authorization flows. Common error codes and suggested resolutions are listed below.

    admin_policy_enforced

    The Google Account is unable to authorize one or more scopes requested due to the policies of their Google Workspace administrator. See the Google Workspace Admin help article Control which third-party & internal apps access Google Workspace data for more information about how an administrator may restrict access to all scopes or sensitive and restricted scopes until access is explicitly granted to your OAuth client ID.

    disallowed_useragent

    The authorization endpoint is displayed inside an embedded user-agent disallowed by Google's OAuth 2.0 Policies .

    Андроид

    Android developers may encounter this error message when opening authorization requests in android.webkit.WebView . Developers should instead use Android libraries such as Google Sign-In for Android or OpenID Foundation's AppAuth for Android .

    Web developers may encounter this error when an Android app opens a general web link in an embedded user-agent and a user navigates to Google's OAuth 2.0 authorization endpoint from your site. Developers should allow general links to open in the default link handler of the operating system, which includes both Android App Links handlers or the default browser app. The Android Custom Tabs library is also a supported option.

    ios

    iOS and macOS developers may encounter this error when opening authorization requests in WKWebView . Developers should instead use iOS libraries such as Google Sign-In for iOS or OpenID Foundation's AppAuth for iOS .

    Web developers may encounter this error when an iOS or macOS app opens a general web link in an embedded user-agent and a user navigates to Google's OAuth 2.0 authorization endpoint from your site. Developers should allow general links to open in the default link handler of the operating system, which includes both Universal Links handlers or the default browser app. The SFSafariViewController library is also a supported option.

    org_internal

    The OAuth client ID in the request is part of a project limiting access to Google Accounts in a specific Google Cloud Organization . For more information about this configuration option see the User type section in the Setting up your OAuth consent screen help article.

    invalid_client

    The OAuth client secret is incorrect. Review the OAuth client configuration , including the client ID and secret used for this request.

    invalid_grant

    When refreshing an access token or using incremental authorization , the token may have expired or has been invalidated. Authenticate the user again and ask for user consent to obtain new tokens. If you are continuing to see this error, ensure that your application has been configured correctly and that you are using the correct tokens and parameters in your request. Otherwise, the user account may have been deleted or disabled.

    redirect_uri_mismatch

    The redirect_uri passed in the authorization request does not match an authorized redirect URI for the OAuth client ID. Review authorized redirect URIs in the .

    The redirect_uri parameter may refer to the OAuth out-of-band (OOB) flow that has been deprecated and is no longer supported. Refer to the migration guide to update your integration.

    invalid_request

    There was something wrong with the request you made. This could be due to a number of reasons:

    • The request was not properly formatted
    • The request was missing required parameters
    • The request uses an authorization method that Google doesn't support. Verify your OAuth integration uses a recommended integration method

    Step 4: Handle the OAuth 2.0 server response

    The OAuth 2.0 server responds to your application's access request by using the URL specified in the request.

    If the user approves the access request, then the response contains an authorization code. If the user does not approve the request, the response contains an error message. The authorization code or error message that is returned to the web server appears on the query string, as shown below:

    An error response:

    https://oauth2.example.com/auth?error=access_denied

    An authorization code response:

    https://oauth2.example.com/auth?code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7

    Sample OAuth 2.0 server response

    You can test this flow by clicking on the following sample URL, which requests read-only access to view metadata for files in your Google Drive and read-only access to view your Google Calendar events:

    https://accounts.google.com/o/oauth2/v2/auth?
     scope=https%3A//www.googleapis.com/auth/drive.metadata.readonly%20https%3A//www.googleapis.com/auth/calendar.readonly&
     access_type=offline&
     include_granted_scopes=true&
     response_type=code&
     state=state_parameter_passthrough_value&
     redirect_uri=https%3A//oauth2.example.com/code&
     client_id=client_id

    After completing the OAuth 2.0 flow, you should be redirected to http://localhost/oauth2callback , which will likely yield a 404 NOT FOUND error unless your local machine serves a file at that address. The next step provides more detail about the information returned in the URI when the user is redirected back to your application.

    Step 5: Exchange authorization code for refresh and access tokens

    After the web server receives the authorization code, it can exchange the authorization code for an access token.

    PHP

    To exchange an authorization code for an access token, use the fetchAccessTokenWithAuthCode method:

    $access_token = $client->fetchAccessTokenWithAuthCode($_GET['code']);

    Питон

    On your callback page, use the google-auth library to verify the authorization server response. Then, use the flow.fetch_token method to exchange the authorization code in that response for an access token:

    state = flask.session['state']
    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
        'client_secret.json',
        scopes=['https://www.googleapis.com/auth/drive.metadata.readonly'],
        state=state)
    flow.redirect_uri = flask.url_for('oauth2callback', _external=True)
    
    authorization_response = flask.request.url
    flow.fetch_token(authorization_response=authorization_response)
    
    # Store the credentials in the session.
    # ACTION ITEM for developers:
    #     Store user's access and refresh tokens in your data store if
    #     incorporating this code into your real app.
    credentials = flow.credentials
    flask.session['credentials'] = {
        'token': credentials.token,
        'refresh_token': credentials.refresh_token,
        'token_uri': credentials.token_uri,
        'client_id': credentials.client_id,
        'client_secret': credentials.client_secret,
        'granted_scopes': credentials.granted_scopes}

    Руби

    On your callback page, use the googleauth library to verify the authorization server response. Use the authorizer.handle_auth_callback_deferred method to save the authorization code and redirect back to the URL that originally requested authorization. This defers the exchange of the code by temporarily stashing the results in the user's session.

      target_url = Google::Auth::WebUserAuthorizer.handle_auth_callback_deferred(request)
      redirect target_url

    Node.js

    To exchange an authorization code for an access token, use the getToken method:

    const url = require('url');
    
    // Receive the callback from Google's OAuth 2.0 server.
    app.get('/oauth2callback', async (req, res) => {
      let q = url.parse(req.url, true).query;
    
      if (q.error) { // An error response e.g. error=access_denied
        console.log('Error:' + q.error);
      } else if (q.state !== req.session.state) { //check state value
        console.log('State mismatch. Possible CSRF attack');
        res.end('State mismatch. Possible CSRF attack');
      } else { // Get access and refresh tokens (if access_type is offline)
    
        let { tokens } = await oauth2Client.getToken(q.code);
        oauth2Client.setCredentials(tokens);
    });

    HTTP/REST

    To exchange an authorization code for an access token, call the https://oauth2.googleapis.com/token endpoint and set the following parameters:

    Поля
    client_id The client ID obtained from the .
    client_secret The client secret obtained from the .
    code The authorization code returned from the initial request.
    grant_type As defined in the OAuth 2.0 specification , this field's value must be set to authorization_code .
    redirect_uri One of the redirect URIs listed for your project in the for the given client_id .

    The following snippet shows a sample request:

    POST /token HTTP/1.1
    Host: oauth2.googleapis.com
    Content-Type: application/x-www-form-urlencoded
    
    code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7&
    client_id=your_client_id&
    client_secret=your_client_secret&
    redirect_uri=https%3A//oauth2.example.com/code&
    grant_type=authorization_code

    Google responds to this request by returning a JSON object that contains a short-lived access token and a refresh token. Note that the refresh token is only returned if your application set the access_type parameter to offline in the initial request to Google's authorization server .

    The response contains the following fields:

    Поля
    access_token The token that your application sends to authorize a Google API request.
    expires_in The remaining lifetime of the access token in seconds.
    refresh_token A token that you can use to obtain a new access token. Refresh tokens are valid until the user revokes access or the refresh token expires. Again, this field is only present in this response if you set the access_type parameter to offline in the initial request to Google's authorization server.
    refresh_token_expires_in The remaining lifetime of the refresh token in seconds. This value is only set when the user grants time-based access .
    scope The scopes of access granted by the access_token expressed as a list of space-delimited, case-sensitive strings.
    token_type The type of token returned. At this time, this field's value is always set to Bearer .

    The following snippet shows a sample response:

    {
      "access_token": "1/fFAGRNJru1FTz70BzhT3Zg",
      "expires_in": 3920,
      "token_type": "Bearer",
      "scope": "https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/calendar.readonly",
      "refresh_token": "1//xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI"
    }

    Ошибки

    When exchanging the authorization code for an access token you may encounter the following error instead of the expected response. Common error codes and suggested resolutions are listed below.

    invalid_grant

    The supplied authorization code is invalid or in the wrong format. Request a new code by restarting the OAuth process to prompt the user for consent again.

    Step 6: Check which scopes users granted

    When requesting multiple permissions (scopes), users may not grant your app access to all of them. Your app must verify which scopes were actually granted and gracefully handle situations where some permissions are denied, typically by disabling the features that rely on those denied scopes.

    Однако есть исключения. Google Workspace Enterprise apps with domain-wide delegation of authority , or apps marked as Trusted , bypass the granular permissions consent screen. For these apps, users won't see the granular permission consent screen. Instead, your app will either receive all requested scopes or none.

    For more detailed information, see How to handle granular permissions .

    PHP

    To check which scopes the user has granted, use the getGrantedScope() method:

    // Space-separated string of granted scopes if it exists, otherwise null.
    $granted_scopes = $client->getOAuth2Service()->getGrantedScope();
    
    // Determine which scopes user granted and build a dictionary
    $granted_scopes_dict = [
      'Drive' => str_contains($granted_scopes, Google\Service\Drive::DRIVE_METADATA_READONLY),
      'Calendar' => str_contains($granted_scopes, Google\Service\Calendar::CALENDAR_READONLY)
    ];

    Питон

    The returned credentials object has a granted_scopes property, which is a list of scopes the user has granted to your app.

    credentials = flow.credentials
    flask.session['credentials'] = {
        'token': credentials.token,
        'refresh_token': credentials.refresh_token,
        'token_uri': credentials.token_uri,
        'client_id': credentials.client_id,
        'client_secret': credentials.client_secret,
        'granted_scopes': credentials.granted_scopes}

    The following function checks which scopes the user has granted to your app.

    def check_granted_scopes(credentials):
      features = {}
      if 'https://www.googleapis.com/auth/drive.metadata.readonly' in credentials['granted_scopes']:
        features['drive'] = True
      else:
        features['drive'] = False
    
      if 'https://www.googleapis.com/auth/calendar.readonly' in credentials['granted_scopes']:
        features['calendar'] = True
      else:
        features['calendar'] = False
    
      return features

    Руби

    When requesting multiple scopes at once, check which scopes were granted through the scope property of the credentials object.

    # User authorized the request. Now, check which scopes were granted.
    if credentials.scope.include?(Google::Apis::DriveV3::AUTH_DRIVE_METADATA_READONLY)
      # User authorized read-only Drive activity permission.
      # Calling the APIs, etc
    else
      # User didn't authorize read-only Drive activity permission.
      # Update UX and application accordingly
    end
    
    # Check if user authorized Calendar read permission.
    if credentials.scope.include?(Google::Apis::CalendarV3::AUTH_CALENDAR_READONLY)
      # User authorized Calendar read permission.
      # Calling the APIs, etc.
    else
      # User didn't authorize Calendar read permission.
      # Update UX and application accordingly
    end

    Node.js

    When requesting multiple scopes at once, check which scopes were granted through the scope property of the tokens object.

    // User authorized the request. Now, check which scopes were granted.
    if (tokens.scope.includes('https://www.googleapis.com/auth/drive.metadata.readonly'))
    {
      // User authorized read-only Drive activity permission.
      // Calling the APIs, etc.
    }
    else
    {
      // User didn't authorize read-only Drive activity permission.
      // Update UX and application accordingly
    }
    
    // Check if user authorized Calendar read permission.
    if (tokens.scope.includes('https://www.googleapis.com/auth/calendar.readonly'))
    {
      // User authorized Calendar read permission.
      // Calling the APIs, etc.
    }
    else
    {
      // User didn't authorize Calendar read permission.
      // Update UX and application accordingly
    }

    HTTP/REST

    To check whether the user has granted your application access to a particular scope, exam the scope field in the access token response. The scopes of access granted by the access_token expressed as a list of space-delimited, case-sensitive strings.

    For example, the following sample access token response indicates that the user has granted your application access to the read-only Drive activity and Calendar events permissions:

      {
        "access_token": "1/fFAGRNJru1FTz70BzhT3Zg",
        "expires_in": 3920,
        "token_type": "Bearer",
        "scope": "https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/calendar.readonly",
        "refresh_token": "1//xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI"
      }

    Call Google APIs

    PHP

    Use the access token to call Google APIs by completing the following steps:

    1. If you need to apply an access token to a new Google\Client object — for example, if you stored the access token in a user session — use the setAccessToken method:
      $client->setAccessToken($access_token);
    2. Build a service object for the API that you want to call. You build a service object by providing an authorized Google\Client object to the constructor for the API you want to call. For example, to call the Drive API:
      $drive = new Google\Service\Drive($client);
    3. Make requests to the API service using the interface provided by the service object . For example, to list the files in the authenticated user's Google Drive:
      $files = $drive->files->listFiles(array());

    Питон

    After obtaining an access token, your application can use that token to authorize API requests on behalf of a given user account or service account. Use the user-specific authorization credentials to build a service object for the API that you want to call, and then use that object to make authorized API requests.

    1. Build a service object for the API that you want to call. You build a service object by calling the googleapiclient.discovery library's build method with the name and version of the API and the user credentials: For example, to call version 3 of the Drive API:
      from googleapiclient.discovery import build
      
      drive = build('drive', 'v2', credentials=credentials)
    2. Make requests to the API service using the interface provided by the service object . For example, to list the files in the authenticated user's Google Drive:
      files = drive.files().list().execute()

    Руби

    After obtaining an access token, your application can use that token to make API requests on behalf of a given user account or service account. Use the user-specific authorization credentials to build a service object for the API that you want to call, and then use that object to make authorized API requests.

    1. Build a service object for the API that you want to call. For example, to call version 3 of the Drive API:
      drive = Google::Apis::DriveV3::DriveService.new
    2. Set the credentials on the service:
      drive.authorization = credentials
    3. Make requests to the API service using the interface provided by the service object . For example, to list the files in the authenticated user's Google Drive:
      files = drive.list_files

    Alternately, authorization can be provided on a per-method basis by supplying the options parameter to a method:

    files = drive.list_files(options: { authorization: credentials })

    Node.js

    After obtaining an access token and setting it to the OAuth2 object, use the object to call Google APIs. Your application can use that token to authorize API requests on behalf of a given user account or service account. Build a service object for the API that you want to call. For example, the following code uses the Google Drive API to list filenames in the user's Drive.

    const { google } = require('googleapis');
    
    // Example of using Google Drive API to list filenames in user's Drive.
    const drive = google.drive('v3');
    drive.files.list({
      auth: oauth2Client,
      pageSize: 10,
      fields: 'nextPageToken, files(id, name)',
    }, (err1, res1) => {
      if (err1) return console.log('The API returned an error: ' + err1);
      const files = res1.data.files;
      if (files.length) {
        console.log('Files:');
        files.map((file) => {
          console.log(`${file.name} (${file.id})`);
        });
      } else {
        console.log('No files found.');
      }
    });

    HTTP/REST

    After your application obtains an access token, you can use the token to make calls to a Google API on behalf of a given user account if the scope(s) of access required by the API have been granted. To do this, include the access token in a request to the API by including either an access_token query parameter or an Authorization HTTP header Bearer value. When possible, the HTTP header is preferable, because query strings tend to be visible in server logs. In most cases you can use a client library to set up your calls to Google APIs (for example, when calling the Drive Files API ).

    You can try out all the Google APIs and view their scopes at the OAuth 2.0 Playground .

    HTTP GET examples

    A call to the drive.files endpoint (the Drive Files API) using the Authorization: Bearer HTTP header might look like the following. Note that you need to specify your own access token:

    GET /drive/v2/files HTTP/1.1
    Host: www.googleapis.com
    Authorization: Bearer access_token

    Here is a call to the same API for the authenticated user using the access_token query string parameter:

    GET https://www.googleapis.com/drive/v2/files?access_token=access_token

    curl examples

    You can test these commands with the curl command-line application. Here's an example that uses the HTTP header option (preferred):

    curl -H "Authorization: Bearer access_token" https://www.googleapis.com/drive/v2/files

    Or, alternatively, the query string parameter option:

    curl https://www.googleapis.com/drive/v2/files?access_token=access_token

    Complete example

    The following example prints a JSON-formatted list of files in a user's Google Drive after the user authenticates and gives consent for the application to access the user's Drive metadata.

    PHP

    To run this example:

    1. В API Console, add the URL of the local machine to the list of redirect URLs. For example, add http://localhost:8080 .
    2. Create a new directory and change to it. Например:
      mkdir ~/php-oauth2-example
      cd ~/php-oauth2-example
    3. Install the Google API Client Library for PHP using Composer :
      composer require google/apiclient:^2.15.0
    4. Create the files index.php and oauth2callback.php with the following content.
    5. Run the example with the PHP's built-in test web server:
      php -S localhost:8080 ~/php-oauth2-example

    index.php

    <?php
    require_once __DIR__.'/vendor/autoload.php';
    
    session_start();
    
    $client = new Google\Client();
    $client->setAuthConfig('client_secret.json');
    
    // User granted permission as an access token is in the session.
    if (isset($_SESSION['access_token']) && $_SESSION['access_token'])
    {
      $client->setAccessToken($_SESSION['access_token']);
      
      // Check if user granted Drive permission
      if ($_SESSION['granted_scopes_dict']['Drive']) {
        echo "Drive feature is enabled.";
        echo "</br>";
        $drive = new Drive($client);
        $files = array();
        $response = $drive->files->listFiles(array());
        foreach ($response->files as $file) {
            echo "File: " . $file->name . " (" . $file->id . ")";
            echo "</br>";
        }
      } else {
        echo "Drive feature is NOT enabled.";
        echo "</br>";
      }
    
       // Check if user granted Calendar permission
      if ($_SESSION['granted_scopes_dict']['Calendar']) {
        echo "Calendar feature is enabled.";
        echo "</br>";
      } else {
        echo "Calendar feature is NOT enabled.";
        echo "</br>";
      }
    }
    else
    {
      // Redirect users to outh2call.php which redirects users to Google OAuth 2.0
      $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php';
      header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
    }
    ?>

    oauth2callback.php

    <?php
    require_once __DIR__.'/vendor/autoload.php';
    
    session_start();
    
    $client = new Google\Client();
    
    // Required, call the setAuthConfig function to load authorization credentials from
    // client_secret.json file.
    $client->setAuthConfigFile('client_secret.json');
    $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST']. $_SERVER['PHP_SELF']);
    
    // Required, to set the scope value, call the addScope function.
    $client->addScope([Google\Service\Drive::DRIVE_METADATA_READONLY, Google\Service\Calendar::CALENDAR_READONLY]);
    
    // Enable incremental authorization. Recommended as a best practice.
    $client->setIncludeGrantedScopes(true);
    
    // Recommended, offline access will give you both an access and refresh token so that
    // your app can refresh the access token without user interaction.
    $client->setAccessType("offline");
    
    // Generate a URL for authorization as it doesn't contain code and error
    if (!isset($_GET['code']) && !isset($_GET['error']))
    {
      // Generate and set state value
      $state = bin2hex(random_bytes(16));
      $client->setState($state);
      $_SESSION['state'] = $state;
    
      // Generate a url that asks permissions.
      $auth_url = $client->createAuthUrl();
      header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
    }
    
    // User authorized the request and authorization code is returned to exchange access and
    // refresh tokens.
    if (isset($_GET['code']))
    {
      // Check the state value
      if (!isset($_GET['state']) || $_GET['state'] !== $_SESSION['state']) {
        die('State mismatch. Possible CSRF attack.');
      }
    
      // Get access and refresh tokens (if access_type is offline)
      $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
    
      /** Save access and refresh token to the session variables.
        * ACTION ITEM: In a production app, you likely want to save the
        *              refresh token in a secure persistent storage instead. */
      $_SESSION['access_token'] = $token;
      $_SESSION['refresh_token'] = $client->getRefreshToken();
      
      // Space-separated string of granted scopes if it exists, otherwise null.
      $granted_scopes = $client->getOAuth2Service()->getGrantedScope();
    
      // Determine which scopes user granted and build a dictionary
      $granted_scopes_dict = [
        'Drive' => str_contains($granted_scopes, Google\Service\Drive::DRIVE_METADATA_READONLY),
        'Calendar' => str_contains($granted_scopes, Google\Service\Calendar::CALENDAR_READONLY)
      ];
      $_SESSION['granted_scopes_dict'] = $granted_scopes_dict;
      
      $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/';
      header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
    }
    
    // An error response e.g. error=access_denied
    if (isset($_GET['error']))
    {
      echo "Error: ". $_GET['error'];
    }
    ?>

    Питон

    This example uses the Flask framework. It runs a web application at http://localhost:8080 that lets you test the OAuth 2.0 flow. If you go to that URL, you should see five links:

    • Call Drive API: This link points to a page that tries to execute a sample API request if users granted the permission. If necessary, it starts the authorization flow. If successful, the page displays the API response.
    • Mock page to call Calendar API: This link points to a maockpage that tries to execute a sample Calendar API request if users granted the permission. If necessary, it starts the authorization flow. If successful, the page displays the API response.
    • Test the auth flow directly: This link points to a page that tries to send the user through the authorization flow . The app requests permission to submit authorized API requests on the user's behalf.
    • Revoke current credentials: This link points to a page that revokes permissions that the user has already granted to the application.
    • Clear Flask session credentials: This link clears authorization credentials that are stored in the Flask session. This lets you see what would happen if a user who had already granted permission to your app tried to execute an API request in a new session. It also lets you see the API response your app would get if a user had revoked permissions granted to your app, and your app still tried to authorize a request with a revoked access token.
    # -*- coding: utf-8 -*-
    
    import os
    import flask
    import requests
    
    import google.oauth2.credentials
    import google_auth_oauthlib.flow
    import googleapiclient.discovery
    
    # This variable specifies the name of a file that contains the OAuth 2.0
    # information for this application, including its client_id and client_secret.
    CLIENT_SECRETS_FILE = "client_secret.json"
    
    # The OAuth 2.0 access scope allows for access to the
    # authenticated user's account and requires requests to use an SSL connection.
    SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly',
              'https://www.googleapis.com/auth/calendar.readonly']
    API_SERVICE_NAME = 'drive'
    API_VERSION = 'v2'
    
    app = flask.Flask(__name__)
    # Note: A secret key is included in the sample so that it works.
    # If you use this code in your application, replace this with a truly secret
    # key. See https://flask.palletsprojects.com/quickstart/#sessions.
    app.secret_key = 'REPLACE ME - this value is here as a placeholder.'
    
    @app.route('/')
    def index():
      return print_index_table()
    
    @app.route('/drive')
    def drive_api_request():
      if 'credentials' not in flask.session:
        return flask.redirect('authorize')
    
      features = flask.session['features']
    
      if features['drive']:
        # Load credentials from the session.
        credentials = google.oauth2.credentials.Credentials(
            **flask.session['credentials'])
    
        drive = googleapiclient.discovery.build(
            API_SERVICE_NAME, API_VERSION, credentials=credentials)
    
        files = drive.files().list().execute()
    
        # Save credentials back to session in case access token was refreshed.
        # ACTION ITEM: In a production app, you likely want to save these
        #              credentials in a persistent database instead.
        flask.session['credentials'] = credentials_to_dict(credentials)
    
        return flask.jsonify(**files)
      else:
        # User didn't authorize read-only Drive activity permission.
        # Update UX and application accordingly
        return '<p>Drive feature is not enabled.</p>'
    
    @app.route('/calendar')
        def calendar_api_request():
          if 'credentials' not in flask.session:
            return flask.redirect('authorize')
    
          features = flask.session['features']
    
          if features['calendar']:
            # User authorized Calendar read permission.
            # Calling the APIs, etc.
            return ('<p>User granted the Google Calendar read permission. '+
                    'This sample code does not include code to call Calendar</p>')
          else:
            # User didn't authorize Calendar read permission.
            # Update UX and application accordingly
            return '<p>Calendar feature is not enabled.</p>'
    
    @app.route('/authorize')
    def authorize():
      # Create flow instance to manage the OAuth 2.0 Authorization Grant Flow steps.
      flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
          CLIENT_SECRETS_FILE, scopes=SCOPES)
    
      # The URI created here must exactly match one of the authorized redirect URIs
      # for the OAuth 2.0 client, which you configured in the API Console. If this
      # value doesn't match an authorized URI, you will get a 'redirect_uri_mismatch'
      # error.
      flow.redirect_uri = flask.url_for('oauth2callback', _external=True)
    
      authorization_url, state = flow.authorization_url(
          # Enable offline access so that you can refresh an access token without
          # re-prompting the user for permission. Recommended for web server apps.
          access_type='offline',
          # Enable incremental authorization. Recommended as a best practice.
          include_granted_scopes='true')
    
      # Store the state so the callback can verify the auth server response.
      flask.session['state'] = state
    
      return flask.redirect(authorization_url)
    
    @app.route('/oauth2callback')
    def oauth2callback():
      # Specify the state when creating the flow in the callback so that it can
      # verified in the authorization server response.
      state = flask.session['state']
    
      flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
          CLIENT_SECRETS_FILE, scopes=SCOPES, state=state)
      flow.redirect_uri = flask.url_for('oauth2callback', _external=True)
    
      # Use the authorization server's response to fetch the OAuth 2.0 tokens.
      authorization_response = flask.request.url
      flow.fetch_token(authorization_response=authorization_response)
    
      # Store credentials in the session.
      # ACTION ITEM: In a production app, you likely want to save these
      #              credentials in a persistent database instead.
      credentials = flow.credentials
      
      credentials = credentials_to_dict(credentials)
      flask.session['credentials'] = credentials
    
      # Check which scopes user granted
      features = check_granted_scopes(credentials)
      flask.session['features'] = features
      return flask.redirect('/')
      
    
    @app.route('/revoke')
    def revoke():
      if 'credentials' not in flask.session:
        return ('You need to <a href="/authorize">authorize</a> before ' +
                'testing the code to revoke credentials.')
    
      credentials = google.oauth2.credentials.Credentials(
        **flask.session['credentials'])
    
      revoke = requests.post('https://oauth2.googleapis.com/revoke',
          params={'token': credentials.token},
          headers = {'content-type': 'application/x-www-form-urlencoded'})
    
      status_code = getattr(revoke, 'status_code')
      if status_code == 200:
        return('Credentials successfully revoked.' + print_index_table())
      else:
        return('An error occurred.' + print_index_table())
    
    @app.route('/clear')
    def clear_credentials():
      if 'credentials' in flask.session:
        del flask.session['credentials']
      return ('Credentials have been cleared.<br><br>' +
              print_index_table())
    
    def credentials_to_dict(credentials):
      return {'token': credentials.token,
              'refresh_token': credentials.refresh_token,
              'token_uri': credentials.token_uri,
              'client_id': credentials.client_id,
              'client_secret': credentials.client_secret,
              'granted_scopes': credentials.granted_scopes}
    
    def check_granted_scopes(credentials):
      features = {}
      if 'https://www.googleapis.com/auth/drive.metadata.readonly' in credentials['granted_scopes']:
        features['drive'] = True
      else:
        features['drive'] = False
    
      if 'https://www.googleapis.com/auth/calendar.readonly' in credentials['granted_scopes']:
        features['calendar'] = True
      else:
        features['calendar'] = False
    
      return features
    
    def print_index_table():
      return ('<table>' +
              '<tr><td><a href="/test">Test an API request</a></td>' +
              '<td>Submit an API request and see a formatted JSON response. ' +
              '    Go through the authorization flow if there are no stored ' +
              '    credentials for the user.</td></tr>' +
              '<tr><td><a href="/authorize">Test the auth flow directly</a></td>' +
              '<td>Go directly to the authorization flow. If there are stored ' +
              '    credentials, you still might not be prompted to reauthorize ' +
              '    the application.</td></tr>' +
              '<tr><td><a href="/revoke">Revoke current credentials</a></td>' +
              '<td>Revoke the access token associated with the current user ' +
              '    session. After revoking credentials, if you go to the test ' +
              '    page, you should see an <code>invalid_grant</code> error.' +
              '</td></tr>' +
              '<tr><td><a href="/clear">Clear Flask session credentials</a></td>' +
              '<td>Clear the access token currently stored in the user session. ' +
              '    After clearing the token, if you <a href="/test">test the ' +
              '    API request</a> again, you should go back to the auth flow.' +
              '</td></tr></table>')
    
    if __name__ == '__main__':
      # When running locally, disable OAuthlib's HTTPs verification.
      # ACTION ITEM for developers:
      #     When running in production *do not* leave this option enabled.
      os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
    
      # This disables the requested scopes and granted scopes check.
      # If users only grant partial request, the warning would not be thrown.
      os.environ['OAUTHLIB_RELAX_TOKEN_SCOPE'] = '1'
    
      # Specify a hostname and port that are set as a valid redirect URI
      # for your API project in the Google API Console.
      app.run('localhost', 8080, debug=True)

    Руби

    This example uses the Sinatra framework.

    require 'googleauth'
    require 'googleauth/web_user_authorizer'
    require 'googleauth/stores/redis_token_store'
    
    require 'google/apis/drive_v3'
    require 'google/apis/calendar_v3'
    
    require 'sinatra'
    
    configure do
      enable :sessions
    
      # Required, call the from_file method to retrieve the client ID from a
      # client_secret.json file.
      set :client_id, Google::Auth::ClientId.from_file('/path/to/client_secret.json')
    
      # Required, scope value
      # Access scopes for two non-Sign-In scopes: Read-only Drive activity and Google Calendar.
      scope = ['Google::Apis::DriveV3::AUTH_DRIVE_METADATA_READONLY',
               'Google::Apis::CalendarV3::AUTH_CALENDAR_READONLY']
    
      # Required, Authorizers require a storage instance to manage long term persistence of
      # access and refresh tokens.
      set :token_store, Google::Auth::Stores::RedisTokenStore.new(redis: Redis.new)
    
      # Required, indicate where the API server will redirect the user after the user completes
      # the authorization flow. The redirect URI is required. The value must exactly
      # match one of the authorized redirect URIs for the OAuth 2.0 client, which you
      # configured in the API Console. If this value doesn't match an authorized URI,
      # you will get a 'redirect_uri_mismatch' error.
      set :callback_uri, '/oauth2callback'
    
      # To use OAuth2 authentication, we need access to a CLIENT_ID, CLIENT_SECRET, AND REDIRECT_URI
      # from the client_secret.json file. To get these credentials for your application, visit
      # https://console.cloud.google.com/apis/credentials.
      set :authorizer, Google::Auth::WebUserAuthorizer.new(settings.client_id, settings.scope,
                              settings.token_store, callback_uri: settings.callback_uri)
    end
    
    get '/' do
      # NOTE: Assumes the user is already authenticated to the app
      user_id = request.session['user_id']
    
      # Fetch stored credentials for the user from the given request session.
      # nil if none present
      credentials = settings.authorizer.get_credentials(user_id, request)
    
      if credentials.nil?
        # Generate a url that asks the user to authorize requested scope(s).
        # Then, redirect user to the url.
        redirect settings.authorizer.get_authorization_url(request: request)
      end
      
      # User authorized the request. Now, check which scopes were granted.
      if credentials.scope.include?(Google::Apis::DriveV3::AUTH_DRIVE_METADATA_READONLY)
        # User authorized read-only Drive activity permission.
        # Example of using Google Drive API to list filenames in user's Drive.
        drive = Google::Apis::DriveV3::DriveService.new
        files = drive.list_files(options: { authorization: credentials })
        "<pre>#{JSON.pretty_generate(files.to_h)}</pre>"
      else
        # User didn't authorize read-only Drive activity permission.
        # Update UX and application accordingly
      end
    
      # Check if user authorized Calendar read permission.
      if credentials.scope.include?(Google::Apis::CalendarV3::AUTH_CALENDAR_READONLY)
        # User authorized Calendar read permission.
        # Calling the APIs, etc.
      else
        # User didn't authorize Calendar read permission.
        # Update UX and application accordingly
      end
    end
    
    # Receive the callback from Google's OAuth 2.0 server.
    get '/oauth2callback' do
      # Handle the result of the oauth callback. Defers the exchange of the code by
      # temporarily stashing the results in the user's session.
      target_url = Google::Auth::WebUserAuthorizer.handle_auth_callback_deferred(request)
      redirect target_url
    end

    Node.js

    To run this example:

    1. В API Console, add the URL of the local machine to the list of redirect URLs. For example, add http://localhost .
    2. Make sure you have maintenance LTS, active LTS, or current release of Node.js installed.
    3. Create a new directory and change to it. Например:
      mkdir ~/nodejs-oauth2-example
      cd ~/nodejs-oauth2-example
    4. Install the Google API Client Library for Node.js using npm :
      npm install googleapis
    5. Create the files main.js with the following content.
    6. Run the example:
      node .\main.js

    main.js

    const http = require('http');
    const https = require('https');
    const url = require('url');
    const { google } = require('googleapis');
    const crypto = require('crypto');
    const express = require('express');
    const session = require('express-session');
    
    /**
     * To use OAuth2 authentication, we need access to a CLIENT_ID, CLIENT_SECRET, AND REDIRECT_URI.
     * To get these credentials for your application, visit
     * https://console.cloud.google.com/apis/credentials.
     */
    const oauth2Client = new google.auth.OAuth2(
      YOUR_CLIENT_ID,
      YOUR_CLIENT_SECRET,
      YOUR_REDIRECT_URL
    );
    
    // Access scopes for two non-Sign-In scopes: Read-only Drive activity and Google Calendar.
    const scopes = [
      'https://www.googleapis.com/auth/drive.metadata.readonly',
      'https://www.googleapis.com/auth/calendar.readonly'
    ];
    
    /* Global variable that stores user credential in this code example.
     * ACTION ITEM for developers:
     *   Store user's refresh token in your data store if
     *   incorporating this code into your real app.
     *   For more information on handling refresh tokens,
     *   see https://github.com/googleapis/google-api-nodejs-client#handling-refresh-tokens
     */
    let userCredential = null;
    
    async function main() {
      const app = express();
    
      app.use(session({
        secret: 'your_secure_secret_key', // Replace with a strong secret
        resave: false,
        saveUninitialized: false,
      }));
    
      // Example on redirecting user to Google's OAuth 2.0 server.
      app.get('/', async (req, res) => {
        // Generate a secure random state value.
        const state = crypto.randomBytes(32).toString('hex');
        // Store state in the session
        req.session.state = state;
    
        // Generate a url that asks permissions for the Drive activity and Google Calendar scope
        const authorizationUrl = oauth2Client.generateAuthUrl({
          // 'online' (default) or 'offline' (gets refresh_token)
          access_type: 'offline',
          /** Pass in the scopes array defined above.
            * Alternatively, if only one scope is needed, you can pass a scope URL as a string */
          scope: scopes,
          // Enable incremental authorization. Recommended as a best practice.
          include_granted_scopes: true,
          // Include the state parameter to reduce the risk of CSRF attacks.
          state: state
        });
    
        res.redirect(authorizationUrl);
      });
    
      // Receive the callback from Google's OAuth 2.0 server.
      app.get('/oauth2callback', async (req, res) => {
        // Handle the OAuth 2.0 server response
        let q = url.parse(req.url, true).query;
    
        if (q.error) { // An error response e.g. error=access_denied
          console.log('Error:' + q.error);
        } else if (q.state !== req.session.state) { //check state value
          console.log('State mismatch. Possible CSRF attack');
          res.end('State mismatch. Possible CSRF attack');
        } else { // Get access and refresh tokens (if access_type is offline)
          let { tokens } = await oauth2Client.getToken(q.code);
          oauth2Client.setCredentials(tokens);
    
          /** Save credential to the global variable in case access token was refreshed.
            * ACTION ITEM: In a production app, you likely want to save the refresh token
            *              in a secure persistent database instead. */
          userCredential = tokens;
          
          // User authorized the request. Now, check which scopes were granted.
          if (tokens.scope.includes('https://www.googleapis.com/auth/drive.metadata.readonly'))
          {
            // User authorized read-only Drive activity permission.
            // Example of using Google Drive API to list filenames in user's Drive.
            const drive = google.drive('v3');
            drive.files.list({
              auth: oauth2Client,
              pageSize: 10,
              fields: 'nextPageToken, files(id, name)',
            }, (err1, res1) => {
              if (err1) return console.log('The API returned an error: ' + err1);
              const files = res1.data.files;
              if (files.length) {
                console.log('Files:');
                files.map((file) => {
                  console.log(`${file.name} (${file.id})`);
                });
              } else {
                console.log('No files found.');
              }
            });
          }
          else
          {
            // User didn't authorize read-only Drive activity permission.
            // Update UX and application accordingly
          }
    
          // Check if user authorized Calendar read permission.
          if (tokens.scope.includes('https://www.googleapis.com/auth/calendar.readonly'))
          {
            // User authorized Calendar read permission.
            // Calling the APIs, etc.
          }
          else
          {
            // User didn't authorize Calendar read permission.
            // Update UX and application accordingly
          }
        }
      });
    
      // Example on revoking a token
      app.get('/revoke', async (req, res) => {
        // Build the string for the POST request
        let postData = "token=" + userCredential.access_token;
    
        // Options for POST request to Google's OAuth 2.0 server to revoke a token
        let postOptions = {
          host: 'oauth2.googleapis.com',
          port: '443',
          path: '/revoke',
          method: 'POST',
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': Buffer.byteLength(postData)
          }
        };
    
        // Set up the request
        const postReq = https.request(postOptions, function (res) {
          res.setEncoding('utf8');
          res.on('data', d => {
            console.log('Response: ' + d);
          });
        });
    
        postReq.on('error', error => {
          console.log(error)
        });
    
        // Post the request with data
        postReq.write(postData);
        postReq.end();
      });
    
    
      const server = http.createServer(app);
      server.listen(8080);
    }
    main().catch(console.error);

    HTTP/REST

    This Python example uses the Flask framework and the Requests library to demonstrate the OAuth 2.0 web flow. We recommend using the Google API Client Library for Python for this flow. (The example in the Python tab does use the client library.)

    import json
    import flask
    import requests
    
    app = flask.Flask(__name__)
    
    # To get these credentials (CLIENT_ID CLIENT_SECRET) and for your application, visit
    # https://console.cloud.google.com/apis/credentials.
    CLIENT_ID = '123456789.apps.googleusercontent.com'
    CLIENT_SECRET = 'abc123'  # Read from a file or environmental variable in a real app
    
    # Access scopes for two non-Sign-In scopes: Read-only Drive activity and Google Calendar.
    SCOPE = 'https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/calendar.readonly'
    
    # Indicate where the API server will redirect the user after the user completes
    # the authorization flow. The redirect URI is required. The value must exactly
    # match one of the authorized redirect URIs for the OAuth 2.0 client, which you
    # configured in the API Console. If this value doesn't match an authorized URI,
    # you will get a 'redirect_uri_mismatch' error.
    REDIRECT_URI = 'http://example.com/oauth2callback'
    
    @app.route('/')
    def index():
      if 'credentials' not in flask.session:
        return flask.redirect(flask.url_for('oauth2callback'))
    
      credentials = json.loads(flask.session['credentials'])
    
      if credentials['expires_in'] <= 0:
        return flask.redirect(flask.url_for('oauth2callback'))
      else: 
        # User authorized the request. Now, check which scopes were granted.
        if 'https://www.googleapis.com/auth/drive.metadata.readonly' in credentials['scope']:
          # User authorized read-only Drive activity permission.
          # Example of using Google Drive API to list filenames in user's Drive.
          headers = {'Authorization': 'Bearer {}'.format(credentials['access_token'])}
          req_uri = 'https://www.googleapis.com/drive/v2/files'
          r = requests.get(req_uri, headers=headers).text
        else:
          # User didn't authorize read-only Drive activity permission.
          # Update UX and application accordingly
          r = 'User did not authorize Drive permission.'
    
        # Check if user authorized Calendar read permission.
        if 'https://www.googleapis.com/auth/calendar.readonly' in credentials['scope']:
          # User authorized Calendar read permission.
          # Calling the APIs, etc.
          r += 'User authorized Calendar permission.'
        else:
          # User didn't authorize Calendar read permission.
          # Update UX and application accordingly
          r += 'User did not authorize Calendar permission.'
    
      return r
    
    @app.route('/oauth2callback')
    def oauth2callback():
      if 'code' not in flask.request.args:
        state = str(uuid.uuid4())
        flask.session['state'] = state
        # Generate a url that asks permissions for the Drive activity
        # and Google Calendar scope. Then, redirect user to the url.
        auth_uri = ('https://accounts.google.com/o/oauth2/v2/auth?response_type=code'
                    '&client_id={}&redirect_uri={}&scope={}&state={}').format(CLIENT_ID, REDIRECT_URI,
                                                                              SCOPE, state)
        return flask.redirect(auth_uri)
      else:
        if 'state' not in flask.request.args or flask.request.args['state'] != flask.session['state']:
          return 'State mismatch. Possible CSRF attack.', 400
    
        auth_code = flask.request.args.get('code')
        data = {'code': auth_code,
                'client_id': CLIENT_ID,
                'client_secret': CLIENT_SECRET,
                'redirect_uri': REDIRECT_URI,
                'grant_type': 'authorization_code'}
    
        # Exchange authorization code for access and refresh tokens (if access_type is offline)
        r = requests.post('https://oauth2.googleapis.com/token', data=data)
        flask.session['credentials'] = r.text
        return flask.redirect(flask.url_for('index'))
    
    if __name__ == '__main__':
      import uuid
      app.secret_key = str(uuid.uuid4())
      app.debug = False
      app.run()

    Redirect URI validation rules

    Google applies the following validation rules to redirect URIs in order to help developers keep their applications secure. Your redirect URIs must adhere to these rules. See RFC 3986 section 3 for the definition of domain, host, path, query, scheme and userinfo, mentioned below.

    Validation rules
    Схема

    Redirect URIs must use the HTTPS scheme, not plain HTTP. Localhost URIs (including localhost IP address URIs) are exempt from this rule.

    Хозяин

    Hosts cannot be raw IP addresses. Localhost IP addresses are exempted from this rule.

    Домен
  • Host TLDs ( Top Level Domains ) must belong to the public suffix list .
  • Host domains cannot be “googleusercontent.com” .
  • Redirect URIs cannot contain URL shortener domains (eg goo.gl ) unless the app owns the domain. Furthermore, if an app that owns a shortener domain chooses to redirect to that domain, that redirect URI must either contain “/google-callback/” in its path or end with “/google-callback” .
  • Userinfo

    Redirect URIs cannot contain the userinfo subcomponent.

    Путь

    Redirect URIs cannot contain a path traversal (also called directory backtracking), which is represented by an “/..” or “\..” or their URL encoding.

    Запрос

    Redirect URIs cannot contain open redirects .

    Фрагмент

    Redirect URIs cannot contain the fragment component.

    Персонажи Redirect URIs cannot contain certain characters including:
    • Wildcard characters ( '*' )
    • Non-printable ASCII characters
    • Invalid percent encodings (any percent encoding that does not follow URL-encoding form of a percent sign followed by two hexadecimal digits)
    • Null characters (an encoded NULL character, eg, %00 , %C0%80 )

    Incremental authorization

    In the OAuth 2.0 protocol, your app requests authorization to access resources, which are identified by scopes. It is considered a best user-experience practice to request authorization for resources at the time you need them. To enable that practice, Google's authorization server supports incremental authorization. This feature lets you request scopes as they are needed and, if the user grants permission for the new scope, returns an authorization code that may be exchanged for a token containing all scopes the user has granted the project.

    For example, an app that lets people sample music tracks and create mixes might need very few resources at sign-in time, perhaps nothing more than the name of the person signing in. However, saving a completed mix would require access to their Google Drive. Most people would find it natural if they only were asked for access to their Google Drive at the time the app actually needed it.

    In this case, at sign-in time the app might request the openid and profile scopes to perform basic sign-in, and then later request the https://www.googleapis.com/auth/drive.file scope at the time of the first request to save a mix.

    To implement incremental authorization, you complete the normal flow for requesting an access token but make sure that the authorization request includes previously granted scopes. This approach allows your app to avoid having to manage multiple access tokens.

    The following rules apply to an access token obtained from an incremental authorization:

    • The token can be used to access resources corresponding to any of the scopes rolled into the new, combined authorization.
    • When you use the refresh token for the combined authorization to obtain an access token, the access token represents the combined authorization and can be used for any of the scope values included in the response.
    • The combined authorization includes all scopes that the user granted to the API project even if the grants were requested from different clients. For example, if a user granted access to one scope using an application's desktop client and then granted another scope to the same application via a mobile client, the combined authorization would include both scopes.
    • If you revoke a token that represents a combined authorization, access to all of that authorization's scopes on behalf of the associated user are revoked simultaneously.

    The language-specific code samples in Step 1: Set authorization parameters and the sample HTTP/REST redirect URL in Step 2: Redirect to Google's OAuth 2.0 server all use incremental authorization. The code samples below also show the code that you need to add to use incremental authorization.

    PHP

    $client->setIncludeGrantedScopes(true);

    Питон

    In Python, set the include_granted_scopes keyword argument to true to ensure that an authorization request includes previously granted scopes. It is very possible that include_granted_scopes will not be the only keyword argument that you set, as shown in the example below.

    authorization_url, state = flow.authorization_url(
        # Enable offline access so that you can refresh an access token without
        # re-prompting the user for permission. Recommended for web server apps.
        access_type='offline',
        # Enable incremental authorization. Recommended as a best practice.
        include_granted_scopes='true')

    Руби

    auth_client.update!(
      :additional_parameters => {"include_granted_scopes" => "true"}
    )

    Node.js

    const authorizationUrl = oauth2Client.generateAuthUrl({
      // 'online' (default) or 'offline' (gets refresh_token)
      access_type: 'offline',
      /** Pass in the scopes array defined above.
        * Alternatively, if only one scope is needed, you can pass a scope URL as a string */
      scope: scopes,
      // Enable incremental authorization. Recommended as a best practice.
      include_granted_scopes: true
    });

    HTTP/REST

    GET https://accounts.google.com/o/oauth2/v2/auth?
      client_id=your_client_id&
      response_type=code&
      state=state_parameter_passthrough_value&
      scope=https%3A//www.googleapis.com/auth/drive.metadata.readonly%20https%3A//www.googleapis.com/auth/calendar.readonly&
      redirect_uri=https%3A//oauth2.example.com/code&
      prompt=consent&
      include_granted_scopes=true

    Refreshing an access token (offline access)

    Access tokens periodically expire and become invalid credentials for a related API request. You can refresh an access token without prompting the user for permission (including when the user is not present) if you requested offline access to the scopes associated with the token.

    • If you use a Google API Client Library, the client object refreshes the access token as needed as long as you configure that object for offline access.
    • If you are not using a client library, you need to set the access_type HTTP query parameter to offline when redirecting the user to Google's OAuth 2.0 server . In that case, Google's authorization server returns a refresh token when you exchange an authorization code for an access token. Then, if the access token expires (or at any other time), you can use a refresh token to obtain a new access token.

    Requesting offline access is a requirement for any application that needs to access a Google API when the user is not present. For example, an app that performs backup services or executes actions at predetermined times needs to be able to refresh its access token when the user is not present. The default style of access is called online .

    Server-side web applications, installed applications, and devices all obtain refresh tokens during the authorization process. Refresh tokens are not typically used in client-side (JavaScript) web applications.

    PHP

    If your application needs offline access to a Google API, set the API client's access type to offline :

    $client->setAccessType("offline");

    After a user grants offline access to the requested scopes, you can continue to use the API client to access Google APIs on the user's behalf when the user is offline. The client object will refresh the access token as needed.

    Питон

    In Python, set the access_type keyword argument to offline to ensure that you will be able to refresh the access token without having to re-prompt the user for permission. It is very possible that access_type will not be the only keyword argument that you set, as shown in the example below.

    authorization_url, state = flow.authorization_url(
        # Enable offline access so that you can refresh an access token without
        # re-prompting the user for permission. Recommended for web server apps.
        access_type='offline',
        # Enable incremental authorization. Recommended as a best practice.
        include_granted_scopes='true')

    After a user grants offline access to the requested scopes, you can continue to use the API client to access Google APIs on the user's behalf when the user is offline. The client object will refresh the access token as needed.

    Руби

    If your application needs offline access to a Google API, set the API client's access type to offline :

    auth_client.update!(
      :additional_parameters => {"access_type" => "offline"}
    )

    After a user grants offline access to the requested scopes, you can continue to use the API client to access Google APIs on the user's behalf when the user is offline. The client object will refresh the access token as needed.

    Node.js

    If your application needs offline access to a Google API, set the API client's access type to offline :

    const authorizationUrl = oauth2Client.generateAuthUrl({
      // 'online' (default) or 'offline' (gets refresh_token)
      access_type: 'offline',
      /** Pass in the scopes array defined above.
        * Alternatively, if only one scope is needed, you can pass a scope URL as a string */
      scope: scopes,
      // Enable incremental authorization. Recommended as a best practice.
      include_granted_scopes: true
    });

    After a user grants offline access to the requested scopes, you can continue to use the API client to access Google APIs on the user's behalf when the user is offline. The client object will refresh the access token as needed.

    Access tokens expire. This library will automatically use a refresh token to obtain a new access token if it is about to expire. An easy way to make sure you always store the most recent tokens is to use the tokens event:

    oauth2Client.on('tokens', (tokens) => {
      if (tokens.refresh_token) {
        // store the refresh_token in your secure persistent database
        console.log(tokens.refresh_token);
      }
      console.log(tokens.access_token);
    });

    This tokens event only occurs in the first authorization, and you need to have set your access_type to offline when calling the generateAuthUrl method to receive the refresh token. If you have already given your app the requisiste permissions without setting the appropriate constraints for receiving a refresh token, you will need to re-authorize the application to receive a fresh refresh token.

    To set the refresh_token at a later time, you can use the setCredentials method:

    oauth2Client.setCredentials({
      refresh_token: `STORED_REFRESH_TOKEN`
    });

    Once the client has a refresh token, access tokens will be acquired and refreshed automatically in the next call to the API.

    HTTP/REST

    To refresh an access token, your application sends an HTTPS POST request to Google's authorization server ( https://oauth2.googleapis.com/token ) that includes the following parameters:

    Поля
    client_id The client ID obtained from the API Console.
    client_secret The client secret obtained from the API Console.
    grant_type As defined in the OAuth 2.0 specification , this field's value must be set to refresh_token .
    refresh_token The refresh token returned from the authorization code exchange.

    The following snippet shows a sample request:

    POST /token HTTP/1.1
    Host: oauth2.googleapis.com
    Content-Type: application/x-www-form-urlencoded
    
    client_id=your_client_id&
    client_secret=your_client_secret&
    refresh_token=refresh_token&
    grant_type=refresh_token

    As long as the user has not revoked the access granted to the application, the token server returns a JSON object that contains a new access token. The following snippet shows a sample response:

    {
      "access_token": "1/fFAGRNJru1FTz70BzhT3Zg",
      "expires_in": 3920,
      "scope": "https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/calendar.readonly",
      "token_type": "Bearer"
    }

    Note that there are limits on the number of refresh tokens that will be issued; one limit per client/user combination, and another per user across all clients. You should save refresh tokens in long-term storage and continue to use them as long as they remain valid. If your application requests too many refresh tokens, it may run into these limits, in which case older refresh tokens will stop working.

    Revoking a token

    In some cases a user may wish to revoke access given to an application. A user can revoke access by visiting Account Settings . See the Remove site or app access section of the Third-party sites & apps with access to your account support document for more information.

    It is also possible for an application to programmatically revoke the access given to it. Programmatic revocation is important in instances where a user unsubscribes, removes an application, or the API resources required by an app have significantly changed. In other words, part of the removal process can include an API request to ensure the permissions previously granted to the application are removed.

    PHP

    To programmatically revoke a token, call revokeToken() :

    $client->revokeToken();

    Питон

    To programmatically revoke a token, make a request to https://oauth2.googleapis.com/revoke that includes the token as a parameter and sets the Content-Type header:

    requests.post('https://oauth2.googleapis.com/revoke',
        params={'token': credentials.token},
        headers = {'content-type': 'application/x-www-form-urlencoded'})

    Руби

    To programmatically revoke a token, make an HTTP request to the oauth2.revoke endpoint:

    uri = URI('https://oauth2.googleapis.com/revoke')
    response = Net::HTTP.post_form(uri, 'token' => auth_client.access_token)

    The token can be an access token or a refresh token. If the token is an access token and it has a corresponding refresh token, the refresh token will also be revoked.

    If the revocation is successfully processed, then the status code of the response is 200 . For error conditions, a status code 400 is returned along with an error code.

    Node.js

    To programmatically revoke a token, make an HTTPS POST request to /revoke endpoint:

    const https = require('https');
    
    // Build the string for the POST request
    let postData = "token=" + userCredential.access_token;
    
    // Options for POST request to Google's OAuth 2.0 server to revoke a token
    let postOptions = {
      host: 'oauth2.googleapis.com',
      port: '443',
      path: '/revoke',
      method: 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': Buffer.byteLength(postData)
      }
    };
    
    // Set up the request
    const postReq = https.request(postOptions, function (res) {
      res.setEncoding('utf8');
      res.on('data', d => {
        console.log('Response: ' + d);
      });
    });
    
    postReq.on('error', error => {
      console.log(error)
    });
    
    // Post the request with data
    postReq.write(postData);
    postReq.end();

    The token parameter can be an access token or a refresh token. If the token is an access token and it has a corresponding refresh token, the refresh token will also be revoked.

    If the revocation is successfully processed, then the status code of the response is 200 . For error conditions, a status code 400 is returned along with an error code.

    HTTP/REST

    To programmatically revoke a token, your application makes a request to https://oauth2.googleapis.com/revoke and includes the token as a parameter:

    curl -d -X -POST --header "Content-type:application/x-www-form-urlencoded" \
            https://oauth2.googleapis.com/revoke?token={token}

    The token can be an access token or a refresh token. If the token is an access token and it has a corresponding refresh token, the refresh token will also be revoked.

    If the revocation is successfully processed, then the HTTP status code of the response is 200 . For error conditions, an HTTP status code 400 is returned along with an error code.

    Time-based access

    Time-based access allows a user to grant your app access to their data for a limited duration to complete an action. Time-based access is available in select Google products during the consent flow, giving users the option to grant access for a limited period of time. An example is the Data Portability API which enables a one-time transfer of data.

    When a user grants your application time-based access, the refresh token will expire after the specified duration. Note that refresh tokens may be invalidated earlier under specific circumstances; see these cases for details. The refresh_token_expires_in field returned in the authorization code exchange response represents the time remaining until the refresh token expires in such cases.

    Implementing Cross-Account Protection

    An additional step you should take to protect your users' accounts is implementing Cross-Account Protection by utilizing Google's Cross-Account Protection Service. This service lets you subscribe to security event notifications which provide information to your application about major changes to the user account. You can then use the information to take action depending on how you decide to respond to events.

    Some examples of the event types sent to your app by Google's Cross-Account Protection Service are:

    • https://schemas.openid.net/secevent/risc/event-type/sessions-revoked
    • https://schemas.openid.net/secevent/oauth/event-type/token-revoked
    • https://schemas.openid.net/secevent/risc/event-type/account-disabled

    See the Protect user accounts with Cross-Account Protection page for more information on how to implement Cross Account Protection and for the full list of available events.

    ,

    This document explains how web server applications use Google API Client Libraries or Google OAuth 2.0 endpoints 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.

    This OAuth 2.0 flow is specifically for user authorization. It is designed for applications that can store confidential information and maintain state. A properly authorized web server application can access an API while the user interacts with the application or after the user has left the application.

    Web server applications frequently also use service accounts to authorize API requests, particularly when calling Cloud APIs to access project-based data rather than user-specific data. Web server applications can use service accounts in conjunction with user authorization.

    Клиентские библиотеки

    The language-specific examples on this page use Google API Client Libraries to implement OAuth 2.0 authorization. To run the code samples, you must first install the client library for your language.

    When you use a Google API Client Library to handle your application's OAuth 2.0 flow, the client library performs many actions that the application would otherwise need to handle on its own. For example, it determines when the application can use or refresh stored access tokens as well as when the application must reacquire consent. The client library also generates correct redirect URLs and helps to implement redirect handlers that exchange authorization codes for access tokens.

    Google API Client Libraries for server-side applications are available for the following languages:

    Предварительные условия

    Enable APIs for your project

    Any application that calls Google APIs needs to enable those APIs in the API Console.

    To enable an API for your project:

    1. Open the API Library в Google API Console.
    2. If prompted, select a project, or create a new one.
    3. API Library lists all available APIs, grouped by product family and popularity. If the API you want to enable isn't visible in the list, use search to find it, or click View All in the product family it belongs to.
    4. Select the API you want to enable, then click the Enable button.
    5. If prompted, enable billing.
    6. If prompted, read and accept the API's Terms of Service.

    Create authorization credentials

    Any application that uses OAuth 2.0 to access Google APIs must have authorization credentials that identify the application to Google's OAuth 2.0 server. The following steps explain how to create credentials for your project. Your applications can then use the credentials to access APIs that you have enabled for that project.

    1. Go to the Credentials page.
    2. Click Create Client .
    3. Select the Web application application type.
    4. Fill in the form and click Create . Applications that use languages and frameworks like PHP, Java, Python, Ruby, and .NET must specify authorized redirect URIs . The redirect URIs are the endpoints to which the OAuth 2.0 server can send responses. These endpoints must adhere to Google's validation rules .

      For testing, you can specify URIs that refer to the local machine, such as http://localhost:8080 . With that in mind, please note that all of the examples in this document use http://localhost:8080 as the redirect URI.

      We recommend that you design your app's auth endpoints so that your application does not expose authorization codes to other resources on the page.

    After creating your credentials, download the client_secret.json file from the API Console. Securely store the file in a location that only your application can access.

    Identify access scopes

    Scopes enable your application to only request access to the resources that it needs while also enabling users to control the amount of access that they grant to your application. Thus, there may be an inverse relationship between the number of scopes requested and the likelihood of obtaining user consent.

    Before you start implementing OAuth 2.0 authorization, we recommend that you identify the scopes that your app will need permission to access.

    We also recommend that your application request access to authorization scopes via an incremental authorization process, in which your application requests access to user data in context. This best practice helps users to more easily understand why your application needs the access it is requesting.

    The OAuth 2.0 API Scopes document contains a full list of scopes that you might use to access Google APIs.

    Language-specific requirements

    To run any of the code samples in this document, you'll need a Google account, access to the Internet, and a web browser. If you are using one of the API client libraries, also see the language-specific requirements below.

    PHP

    To run the PHP code samples in this document, you'll need:

    • PHP 8.0 or greater with the command-line interface (CLI) and JSON extension installed.
    • The Composer dependency management tool.
    • The Google APIs Client Library for PHP:

      composer require google/apiclient:^2.15.0

    See Google APIs Client Library for PHP for more information.

    Питон

    To run the Python code samples in this document, you'll need:

    • Python 3.7 or greater
    • The pip package management tool.
    • The Google APIs Client Library for Python 2.0 release:
      pip install --upgrade google-api-python-client
    • The google-auth , google-auth-oauthlib , and google-auth-httplib2 for user authorization.
      pip install --upgrade google-auth google-auth-oauthlib google-auth-httplib2
    • The Flask Python web application framework.
      pip install --upgrade flask
    • The requests HTTP library.
      pip install --upgrade requests

    Review the Google API Python client library release note if you aren't able to upgrade python and associated migration guide.

    Руби

    To run the Ruby code samples in this document, you'll need:

    • Ruby 2.6 or greater
    • The Google Auth Library for Ruby:

      gem install googleauth
    • The client libraries for Drive and Calendar Google APIs:

      gem install google-apis-drive_v3 google-apis-calendar_v3
    • The Sinatra Ruby web application framework.

      gem install sinatra

    Node.js

    To run the Node.js code samples in this document, you'll need:

    • The maintenance LTS, active LTS, or current release of Node.js.
    • The Google APIs Node.js Client:

      npm install googleapis crypto express express-session

    HTTP/REST

    You do not need to install any libraries to be able to directly call the OAuth 2.0 endpoints.

    Obtaining OAuth 2.0 access tokens

    The following steps show how your application interacts with Google's OAuth 2.0 server to obtain a user's consent to perform an API request on the user's behalf. Your application must have that consent before it can execute a Google API request that requires user authorization.

    The list below quickly summarizes these steps:

    1. Your application identifies the permissions it needs.
    2. Your application redirects the user to Google along with the list of requested permissions.
    3. The user decides whether to grant the permissions to your application.
    4. Your application finds out what the user decided.
    5. If the user granted the requested permissions, your application retrieves tokens needed to make API requests on the user's behalf.

    Step 1: Set authorization parameters

    Your first step is to create the authorization request. That request sets parameters that identify your application and define the permissions that the user will be asked to grant to your application.

    • If you use a Google client library for OAuth 2.0 authentication and authorization, you create and configure an object that defines these parameters.
    • If you call the Google OAuth 2.0 endpoint directly, you'll generate a URL and set the parameters on that URL.

    The tabs below define the supported authorization parameters for web server applications. The language-specific examples also show how to use a client library or authorization library to configure an object that sets those parameters.

    PHP

    The following code snippet creates a Google\Client() object, which defines the parameters in the authorization request.

    That object uses information from your client_secret.json file to identify your application. (See creating authorization credentials for more about that file.) The object also identifies the scopes that your application is requesting permission to access and the URL to your application's auth endpoint, which will handle the response from Google's OAuth 2.0 server. Finally, the code sets the optional access_type and include_granted_scopes parameters.

    For example, this code requests read-only, offline access to a user's Google Drive metadata and Calendar events:

    use Google\Client;
    
    $client = new Client();
    
    // Required, call the setAuthConfig function to load authorization credentials from
    // client_secret.json file.
    $client->setAuthConfig('client_secret.json');
    
    // Required, to set the scope value, call the addScope function
    $client->addScope([Google\Service\Drive::DRIVE_METADATA_READONLY, Google\Service\Calendar::CALENDAR_READONLY]);
    
    // Required, call the setRedirectUri function to specify a valid redirect URI for the
    // provided client_id
    $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php');
    
    // Recommended, offline access will give you both an access and refresh token so that
    // your app can refresh the access token without user interaction.
    $client->setAccessType('offline');
    
    // Recommended, call the setState function. Using a state value can increase your assurance that
    // an incoming connection is the result of an authentication request.
    $client->setState($sample_passthrough_value);
    
    // Optional, if your application knows which user is trying to authenticate, it can use this
    // parameter to provide a hint to the Google Authentication Server.
    $client->setLoginHint('hint@example.com');
    
    // Optional, call the setPrompt function to set "consent" will prompt the user for consent
    $client->setPrompt('consent');
    
    // Optional, call the setIncludeGrantedScopes function with true to enable incremental
    // authorization
    $client->setIncludeGrantedScopes(true);

    Питон

    The following code snippet uses the google-auth-oauthlib.flow module to construct the authorization request.

    The code constructs a Flow object, which identifies your application using information from the client_secret.json file that you downloaded after creating authorization credentials . That object also identifies the scopes that your application is requesting permission to access and the URL to your application's auth endpoint, which will handle the response from Google's OAuth 2.0 server. Finally, the code sets the optional access_type and include_granted_scopes parameters.

    For example, this code requests read-only, offline access to a user's Google Drive metadata and Calendar events:

    import google.oauth2.credentials
    import google_auth_oauthlib.flow
    
    # Required, call the from_client_secrets_file method to retrieve the client ID from a
    # client_secret.json file. The client ID (from that file) and access scopes are required. (You can
    # also use the from_client_config method, which passes the client configuration as it originally
    # appeared in a client secrets file but doesn't access the file itself.)
    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file('client_secret.json',
        scopes=['https://www.googleapis.com/auth/drive.metadata.readonly',
                'https://www.googleapis.com/auth/calendar.readonly'])
    
    # Required, indicate where the API server will redirect the user after the user completes
    # the authorization flow. The redirect URI is required. The value must exactly
    # match one of the authorized redirect URIs for the OAuth 2.0 client, which you
    # configured in the API Console. If this value doesn't match an authorized URI,
    # you will get a 'redirect_uri_mismatch' error.
    flow.redirect_uri = 'https://www.example.com/oauth2callback'
    
    # Generate URL for request to Google's OAuth 2.0 server.
    # Use kwargs to set optional request parameters.
    authorization_url, state = flow.authorization_url(
        # Recommended, enable offline access so that you can refresh an access token without
        # re-prompting the user for permission. Recommended for web server apps.
        access_type='offline',
        # Optional, enable incremental authorization. Recommended as a best practice.
        include_granted_scopes='true',
        # Optional, if your application knows which user is trying to authenticate, it can use this
        # parameter to provide a hint to the Google Authentication Server.
        login_hint='hint@example.com',
        # Optional, set prompt to 'consent' will prompt the user for consent
        prompt='consent')

    Руби

    Use the client_secrets.json file that you created to configure a client object in your application. When you configure a client object, you specify the scopes your application needs to access, along with the URL to your application's auth endpoint, which will handle the response from the OAuth 2.0 server.

    For example, this code requests read-only, offline access to a user's Google Drive metadata and Calendar events:

    require 'googleauth'
    require 'googleauth/web_user_authorizer'
    require 'googleauth/stores/redis_token_store'
    
    require 'google/apis/drive_v3'
    require 'google/apis/calendar_v3'
    
    # Required, call the from_file method to retrieve the client ID from a
    # client_secret.json file.
    client_id = Google::Auth::ClientId.from_file('/path/to/client_secret.json')
    
    # Required, scope value 
    # Access scopes for two non-Sign-In scopes: Read-only Drive activity and Google Calendar.
    scope = ['Google::Apis::DriveV3::AUTH_DRIVE_METADATA_READONLY',
             'Google::Apis::CalendarV3::AUTH_CALENDAR_READONLY']
    
    # Required, Authorizers require a storage instance to manage long term persistence of
    # access and refresh tokens.
    token_store = Google::Auth::Stores::RedisTokenStore.new(redis: Redis.new)
    
    # Required, indicate where the API server will redirect the user after the user completes
    # the authorization flow. The redirect URI is required. The value must exactly
    # match one of the authorized redirect URIs for the OAuth 2.0 client, which you
    # configured in the API Console. If this value doesn't match an authorized URI,
    # you will get a 'redirect_uri_mismatch' error.
    callback_uri = '/oauth2callback'
    
    # To use OAuth2 authentication, we need access to a CLIENT_ID, CLIENT_SECRET, AND REDIRECT_URI
    # from the client_secret.json file. To get these credentials for your application, visit
    # https://console.cloud.google.com/apis/credentials.
    authorizer = Google::Auth::WebUserAuthorizer.new(client_id, scope,
                                                    token_store, callback_uri)

    Your application uses the client object to perform OAuth 2.0 operations, such as generating authorization request URLs and applying access tokens to HTTP requests.

    Node.js

    The following code snippet creates a google.auth.OAuth2 object, which defines the parameters in the authorization request.

    That object uses information from your client_secret.json file to identify your application. To ask for permissions from a user to retrieve an access token, you redirect them to a consent page. To create a consent page URL:

    const {google} = require('googleapis');
    const crypto = require('crypto');
    const express = require('express');
    const session = require('express-session');
    
    /**
     * To use OAuth2 authentication, we need access to a CLIENT_ID, CLIENT_SECRET, AND REDIRECT_URI
     * from the client_secret.json file. To get these credentials for your application, visit
     * https://console.cloud.google.com/apis/credentials.
     */
    const oauth2Client = new google.auth.OAuth2(
      YOUR_CLIENT_ID,
      YOUR_CLIENT_SECRET,
      YOUR_REDIRECT_URL
    );
    
    // Access scopes for two non-Sign-In scopes: Read-only Drive activity and Google Calendar.
    const scopes = [
      'https://www.googleapis.com/auth/drive.metadata.readonly',
      'https://www.googleapis.com/auth/calendar.readonly'
    ];
    
    // Generate a secure random state value.
    const state = crypto.randomBytes(32).toString('hex');
    
    // Store state in the session
    req.session.state = state;
    
    // Generate a url that asks permissions for the Drive activity and Google Calendar scope
    const authorizationUrl = oauth2Client.generateAuthUrl({
      // 'online' (default) or 'offline' (gets refresh_token)
      access_type: 'offline',
      /** Pass in the scopes array defined above.
        * Alternatively, if only one scope is needed, you can pass a scope URL as a string */
      scope: scopes,
      // Enable incremental authorization. Recommended as a best practice.
      include_granted_scopes: true,
      // Include the state parameter to reduce the risk of CSRF attacks.
      state: state
    });

    Important Note - The refresh_token is only returned on the first authorization. More details here .

    HTTP/REST

    Google's OAuth 2.0 endpoint is at https://accounts.google.com/o/oauth2/v2/auth . This endpoint is accessible only over HTTPS. Plain HTTP connections are refused.

    The Google authorization server supports the following query string parameters for web server applications:

    Параметры
    client_id Необходимый

    The client ID for your application. You can find this value in the .

    redirect_uri Необходимый

    Determines where the API server redirects the user after the user completes the authorization flow. The value must exactly match one of the authorized redirect URIs for the OAuth 2.0 client, which you configured in your client's . If this value doesn't match an authorized redirect URI for the provided client_id you will get a redirect_uri_mismatch error.

    Note that the http or https scheme, case, and trailing slash (' / ') must all match.

    response_type Необходимый

    Determines whether the Google OAuth 2.0 endpoint returns an authorization code.

    Set the parameter value to code for web server applications.

    scope Необходимый

    A space-delimited list of scopes that identify the resources that your application could access on the user's behalf. These values inform the consent screen that Google displays to the user.

    Scopes enable your application to only request access to the resources that it needs while also enabling users to control the amount of access that they grant to your application. Thus, there is an inverse relationship between the number of scopes requested and the likelihood of obtaining user consent.

    We recommend that your application request access to authorization scopes in context whenever possible. By requesting access to user data in context, via incremental authorization , you help users to more easily understand why your application needs the access it is requesting.

    access_type Рекомендуется

    Indicates whether your application can refresh access tokens when the user is not present at the browser. Valid parameter values are online , which is the default value, and offline .

    Set the value to offline if your application needs to refresh access tokens when the user is not present at the browser. This is the method of refreshing access tokens described later in this document. This value instructs the Google authorization server to return a refresh token and an access token the first time that your application exchanges an authorization code for tokens.

    state Рекомендуется

    Specifies any string value that your application uses to maintain state between your authorization request and the authorization server's response. The server returns the exact value that you send as a name=value pair in the URL query component ( ? ) of the redirect_uri after the user consents to or denies your application's access request.

    You can use this parameter for several purposes, such as directing the user to the correct resource in your application, sending nonces, and mitigating cross-site request forgery. Since your redirect_uri can be guessed, using a state value can increase your assurance that an incoming connection is the result of an authentication request. If you generate a random string or encode the hash of a cookie or another value that captures the client's state, you can validate the response to additionally ensure that the request and response originated in the same browser, providing protection against attacks such as cross-site request forgery . See the OpenID Connect documentation for an example of how to create and confirm a state token.

    include_granted_scopes Необязательный

    Enables applications to use incremental authorization to request access to additional scopes in context. If you set this parameter's value to true and the authorization request is granted, then the new access token will also cover any scopes to which the user previously granted the application access. See the incremental authorization section for examples.

    login_hint Необязательный

    If your application knows which user is trying to authenticate, it can use this parameter to provide a hint to the Google Authentication Server. The server uses the hint to simplify the login flow either by prefilling the email field in the sign-in form or by selecting the appropriate multi-login session.

    Set the parameter value to an email address or sub identifier, which is equivalent to the user's Google ID.

    prompt Необязательный

    A space-delimited, case-sensitive list of prompts to present the user. If you don't specify this parameter, the user will be prompted only the first time your project requests access. See Prompting re-consent for more information.

    Возможные значения:

    none Do not display any authentication or consent screens. Must not be specified with other values.
    consent Prompt the user for consent.
    select_account Prompt the user to select an account.

    Step 2: Redirect to Google's OAuth 2.0 server

    Redirect the user to Google's OAuth 2.0 server to initiate the authentication and authorization process. Typically, this occurs when your application first needs to access the user's data. In the case of incremental authorization , this step also occurs when your application first needs to access additional resources that it does not yet have permission to access.

    PHP

    1. Generate a URL to request access from Google's OAuth 2.0 server:
      $auth_url = $client->createAuthUrl();
    2. Redirect the user to $auth_url :
      header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));

    Питон

    This example shows how to redirect the user to the authorization URL using the Flask web application framework:

    return flask.redirect(authorization_url)

    Руби

    1. Generate a URL to request access from Google's OAuth 2.0 server:
      auth_uri = authorizer.get_authorization_url(request: request)
    2. Redirect the user to auth_uri .

    Node.js

    1. Use the generated URL authorizationUrl from Step 1 generateAuthUrl method to request access from Google's OAuth 2.0 server.
    2. Redirect the user to authorizationUrl .
      res.redirect(authorizationUrl);

    HTTP/REST

    Sample redirect to Google's authorization server

    An example URL is shown below, with line breaks and spaces for readability.

    https://accounts.google.com/o/oauth2/v2/auth?
     scope=https%3A//www.googleapis.com/auth/drive.metadata.readonly%20https%3A//www.googleapis.com/auth/calendar.readonly&
     access_type=offline&
     include_granted_scopes=true&
     response_type=code&
     state=state_parameter_passthrough_value&
     redirect_uri=https%3A//oauth2.example.com/code&
     client_id=client_id

    After you create the request URL, redirect the user to it.

    Google's OAuth 2.0 server authenticates the user and obtains consent from the user for your application to access the requested scopes. The response is sent back to your application using the redirect URL you specified.

    Step 3: Google prompts user for consent

    In this step, the user decides whether to grant your application the requested access. At this stage, Google displays a consent window that shows the name of your application and the Google API services that it is requesting permission to access with the user's authorization credentials and a summary of the scopes of access to be granted. The user can then consent to grant access to one or more scopes requested by your application or refuse the request.

    Your application doesn't need to do anything at this stage as it waits for the response from Google's OAuth 2.0 server indicating whether any access was granted. That response is explained in the following step.

    Ошибки

    Requests to Google's OAuth 2.0 authorization endpoint may display user-facing error messages instead of the expected authentication and authorization flows. Common error codes and suggested resolutions are listed below.

    admin_policy_enforced

    The Google Account is unable to authorize one or more scopes requested due to the policies of their Google Workspace administrator. See the Google Workspace Admin help article Control which third-party & internal apps access Google Workspace data for more information about how an administrator may restrict access to all scopes or sensitive and restricted scopes until access is explicitly granted to your OAuth client ID.

    disallowed_useragent

    The authorization endpoint is displayed inside an embedded user-agent disallowed by Google's OAuth 2.0 Policies .

    Андроид

    Android developers may encounter this error message when opening authorization requests in android.webkit.WebView . Developers should instead use Android libraries such as Google Sign-In for Android or OpenID Foundation's AppAuth for Android .

    Web developers may encounter this error when an Android app opens a general web link in an embedded user-agent and a user navigates to Google's OAuth 2.0 authorization endpoint from your site. Developers should allow general links to open in the default link handler of the operating system, which includes both Android App Links handlers or the default browser app. The Android Custom Tabs library is also a supported option.

    ios

    iOS and macOS developers may encounter this error when opening authorization requests in WKWebView . Developers should instead use iOS libraries such as Google Sign-In for iOS or OpenID Foundation's AppAuth for iOS .

    Web developers may encounter this error when an iOS or macOS app opens a general web link in an embedded user-agent and a user navigates to Google's OAuth 2.0 authorization endpoint from your site. Developers should allow general links to open in the default link handler of the operating system, which includes both Universal Links handlers or the default browser app. The SFSafariViewController library is also a supported option.

    org_internal

    The OAuth client ID in the request is part of a project limiting access to Google Accounts in a specific Google Cloud Organization . For more information about this configuration option see the User type section in the Setting up your OAuth consent screen help article.

    invalid_client

    The OAuth client secret is incorrect. Review the OAuth client configuration , including the client ID and secret used for this request.

    invalid_grant

    When refreshing an access token or using incremental authorization , the token may have expired or has been invalidated. Authenticate the user again and ask for user consent to obtain new tokens. If you are continuing to see this error, ensure that your application has been configured correctly and that you are using the correct tokens and parameters in your request. Otherwise, the user account may have been deleted or disabled.

    redirect_uri_mismatch

    The redirect_uri passed in the authorization request does not match an authorized redirect URI for the OAuth client ID. Review authorized redirect URIs in the .

    The redirect_uri parameter may refer to the OAuth out-of-band (OOB) flow that has been deprecated and is no longer supported. Refer to the migration guide to update your integration.

    invalid_request

    There was something wrong with the request you made. This could be due to a number of reasons:

    • The request was not properly formatted
    • The request was missing required parameters
    • The request uses an authorization method that Google doesn't support. Verify your OAuth integration uses a recommended integration method

    Step 4: Handle the OAuth 2.0 server response

    The OAuth 2.0 server responds to your application's access request by using the URL specified in the request.

    If the user approves the access request, then the response contains an authorization code. If the user does not approve the request, the response contains an error message. The authorization code or error message that is returned to the web server appears on the query string, as shown below:

    An error response:

    https://oauth2.example.com/auth?error=access_denied

    An authorization code response:

    https://oauth2.example.com/auth?code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7

    Sample OAuth 2.0 server response

    You can test this flow by clicking on the following sample URL, which requests read-only access to view metadata for files in your Google Drive and read-only access to view your Google Calendar events:

    https://accounts.google.com/o/oauth2/v2/auth?
     scope=https%3A//www.googleapis.com/auth/drive.metadata.readonly%20https%3A//www.googleapis.com/auth/calendar.readonly&
     access_type=offline&
     include_granted_scopes=true&
     response_type=code&
     state=state_parameter_passthrough_value&
     redirect_uri=https%3A//oauth2.example.com/code&
     client_id=client_id

    After completing the OAuth 2.0 flow, you should be redirected to http://localhost/oauth2callback , which will likely yield a 404 NOT FOUND error unless your local machine serves a file at that address. The next step provides more detail about the information returned in the URI when the user is redirected back to your application.

    Step 5: Exchange authorization code for refresh and access tokens

    After the web server receives the authorization code, it can exchange the authorization code for an access token.

    PHP

    To exchange an authorization code for an access token, use the fetchAccessTokenWithAuthCode method:

    $access_token = $client->fetchAccessTokenWithAuthCode($_GET['code']);

    Питон

    On your callback page, use the google-auth library to verify the authorization server response. Then, use the flow.fetch_token method to exchange the authorization code in that response for an access token:

    state = flask.session['state']
    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
        'client_secret.json',
        scopes=['https://www.googleapis.com/auth/drive.metadata.readonly'],
        state=state)
    flow.redirect_uri = flask.url_for('oauth2callback', _external=True)
    
    authorization_response = flask.request.url
    flow.fetch_token(authorization_response=authorization_response)
    
    # Store the credentials in the session.
    # ACTION ITEM for developers:
    #     Store user's access and refresh tokens in your data store if
    #     incorporating this code into your real app.
    credentials = flow.credentials
    flask.session['credentials'] = {
        'token': credentials.token,
        'refresh_token': credentials.refresh_token,
        'token_uri': credentials.token_uri,
        'client_id': credentials.client_id,
        'client_secret': credentials.client_secret,
        'granted_scopes': credentials.granted_scopes}

    Руби

    On your callback page, use the googleauth library to verify the authorization server response. Use the authorizer.handle_auth_callback_deferred method to save the authorization code and redirect back to the URL that originally requested authorization. This defers the exchange of the code by temporarily stashing the results in the user's session.

      target_url = Google::Auth::WebUserAuthorizer.handle_auth_callback_deferred(request)
      redirect target_url

    Node.js

    To exchange an authorization code for an access token, use the getToken method:

    const url = require('url');
    
    // Receive the callback from Google's OAuth 2.0 server.
    app.get('/oauth2callback', async (req, res) => {
      let q = url.parse(req.url, true).query;
    
      if (q.error) { // An error response e.g. error=access_denied
        console.log('Error:' + q.error);
      } else if (q.state !== req.session.state) { //check state value
        console.log('State mismatch. Possible CSRF attack');
        res.end('State mismatch. Possible CSRF attack');
      } else { // Get access and refresh tokens (if access_type is offline)
    
        let { tokens } = await oauth2Client.getToken(q.code);
        oauth2Client.setCredentials(tokens);
    });

    HTTP/REST

    To exchange an authorization code for an access token, call the https://oauth2.googleapis.com/token endpoint and set the following parameters:

    Поля
    client_id The client ID obtained from the .
    client_secret The client secret obtained from the .
    code The authorization code returned from the initial request.
    grant_type As defined in the OAuth 2.0 specification , this field's value must be set to authorization_code .
    redirect_uri One of the redirect URIs listed for your project in the for the given client_id .

    The following snippet shows a sample request:

    POST /token HTTP/1.1
    Host: oauth2.googleapis.com
    Content-Type: application/x-www-form-urlencoded
    
    code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7&
    client_id=your_client_id&
    client_secret=your_client_secret&
    redirect_uri=https%3A//oauth2.example.com/code&
    grant_type=authorization_code

    Google responds to this request by returning a JSON object that contains a short-lived access token and a refresh token. Note that the refresh token is only returned if your application set the access_type parameter to offline in the initial request to Google's authorization server .

    The response contains the following fields:

    Поля
    access_token The token that your application sends to authorize a Google API request.
    expires_in The remaining lifetime of the access token in seconds.
    refresh_token A token that you can use to obtain a new access token. Refresh tokens are valid until the user revokes access or the refresh token expires. Again, this field is only present in this response if you set the access_type parameter to offline in the initial request to Google's authorization server.
    refresh_token_expires_in The remaining lifetime of the refresh token in seconds. This value is only set when the user grants time-based access .
    scope The scopes of access granted by the access_token expressed as a list of space-delimited, case-sensitive strings.
    token_type The type of token returned. At this time, this field's value is always set to Bearer .

    The following snippet shows a sample response:

    {
      "access_token": "1/fFAGRNJru1FTz70BzhT3Zg",
      "expires_in": 3920,
      "token_type": "Bearer",
      "scope": "https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/calendar.readonly",
      "refresh_token": "1//xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI"
    }

    Ошибки

    When exchanging the authorization code for an access token you may encounter the following error instead of the expected response. Common error codes and suggested resolutions are listed below.

    invalid_grant

    The supplied authorization code is invalid or in the wrong format. Request a new code by restarting the OAuth process to prompt the user for consent again.

    Step 6: Check which scopes users granted

    When requesting multiple permissions (scopes), users may not grant your app access to all of them. Your app must verify which scopes were actually granted and gracefully handle situations where some permissions are denied, typically by disabling the features that rely on those denied scopes.

    Однако есть исключения. Google Workspace Enterprise apps with domain-wide delegation of authority , or apps marked as Trusted , bypass the granular permissions consent screen. For these apps, users won't see the granular permission consent screen. Instead, your app will either receive all requested scopes or none.

    For more detailed information, see How to handle granular permissions .

    PHP

    To check which scopes the user has granted, use the getGrantedScope() method:

    // Space-separated string of granted scopes if it exists, otherwise null.
    $granted_scopes = $client->getOAuth2Service()->getGrantedScope();
    
    // Determine which scopes user granted and build a dictionary
    $granted_scopes_dict = [
      'Drive' => str_contains($granted_scopes, Google\Service\Drive::DRIVE_METADATA_READONLY),
      'Calendar' => str_contains($granted_scopes, Google\Service\Calendar::CALENDAR_READONLY)
    ];

    Питон

    The returned credentials object has a granted_scopes property, which is a list of scopes the user has granted to your app.

    credentials = flow.credentials
    flask.session['credentials'] = {
        'token': credentials.token,
        'refresh_token': credentials.refresh_token,
        'token_uri': credentials.token_uri,
        'client_id': credentials.client_id,
        'client_secret': credentials.client_secret,
        'granted_scopes': credentials.granted_scopes}

    The following function checks which scopes the user has granted to your app.

    def check_granted_scopes(credentials):
      features = {}
      if 'https://www.googleapis.com/auth/drive.metadata.readonly' in credentials['granted_scopes']:
        features['drive'] = True
      else:
        features['drive'] = False
    
      if 'https://www.googleapis.com/auth/calendar.readonly' in credentials['granted_scopes']:
        features['calendar'] = True
      else:
        features['calendar'] = False
    
      return features

    Руби

    When requesting multiple scopes at once, check which scopes were granted through the scope property of the credentials object.

    # User authorized the request. Now, check which scopes were granted.
    if credentials.scope.include?(Google::Apis::DriveV3::AUTH_DRIVE_METADATA_READONLY)
      # User authorized read-only Drive activity permission.
      # Calling the APIs, etc
    else
      # User didn't authorize read-only Drive activity permission.
      # Update UX and application accordingly
    end
    
    # Check if user authorized Calendar read permission.
    if credentials.scope.include?(Google::Apis::CalendarV3::AUTH_CALENDAR_READONLY)
      # User authorized Calendar read permission.
      # Calling the APIs, etc.
    else
      # User didn't authorize Calendar read permission.
      # Update UX and application accordingly
    end

    Node.js

    When requesting multiple scopes at once, check which scopes were granted through the scope property of the tokens object.

    // User authorized the request. Now, check which scopes were granted.
    if (tokens.scope.includes('https://www.googleapis.com/auth/drive.metadata.readonly'))
    {
      // User authorized read-only Drive activity permission.
      // Calling the APIs, etc.
    }
    else
    {
      // User didn't authorize read-only Drive activity permission.
      // Update UX and application accordingly
    }
    
    // Check if user authorized Calendar read permission.
    if (tokens.scope.includes('https://www.googleapis.com/auth/calendar.readonly'))
    {
      // User authorized Calendar read permission.
      // Calling the APIs, etc.
    }
    else
    {
      // User didn't authorize Calendar read permission.
      // Update UX and application accordingly
    }

    HTTP/REST

    To check whether the user has granted your application access to a particular scope, exam the scope field in the access token response. The scopes of access granted by the access_token expressed as a list of space-delimited, case-sensitive strings.

    For example, the following sample access token response indicates that the user has granted your application access to the read-only Drive activity and Calendar events permissions:

      {
        "access_token": "1/fFAGRNJru1FTz70BzhT3Zg",
        "expires_in": 3920,
        "token_type": "Bearer",
        "scope": "https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/calendar.readonly",
        "refresh_token": "1//xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI"
      }

    Call Google APIs

    PHP

    Use the access token to call Google APIs by completing the following steps:

    1. If you need to apply an access token to a new Google\Client object — for example, if you stored the access token in a user session — use the setAccessToken method:
      $client->setAccessToken($access_token);
    2. Build a service object for the API that you want to call. You build a service object by providing an authorized Google\Client object to the constructor for the API you want to call. For example, to call the Drive API:
      $drive = new Google\Service\Drive($client);
    3. Make requests to the API service using the interface provided by the service object . For example, to list the files in the authenticated user's Google Drive:
      $files = $drive->files->listFiles(array());

    Питон

    After obtaining an access token, your application can use that token to authorize API requests on behalf of a given user account or service account. Use the user-specific authorization credentials to build a service object for the API that you want to call, and then use that object to make authorized API requests.

    1. Build a service object for the API that you want to call. You build a service object by calling the googleapiclient.discovery library's build method with the name and version of the API and the user credentials: For example, to call version 3 of the Drive API:
      from googleapiclient.discovery import build
      
      drive = build('drive', 'v2', credentials=credentials)
    2. Make requests to the API service using the interface provided by the service object . For example, to list the files in the authenticated user's Google Drive:
      files = drive.files().list().execute()

    Руби

    After obtaining an access token, your application can use that token to make API requests on behalf of a given user account or service account. Use the user-specific authorization credentials to build a service object for the API that you want to call, and then use that object to make authorized API requests.

    1. Build a service object for the API that you want to call. For example, to call version 3 of the Drive API:
      drive = Google::Apis::DriveV3::DriveService.new
    2. Set the credentials on the service:
      drive.authorization = credentials
    3. Make requests to the API service using the interface provided by the service object . For example, to list the files in the authenticated user's Google Drive:
      files = drive.list_files

    Alternately, authorization can be provided on a per-method basis by supplying the options parameter to a method:

    files = drive.list_files(options: { authorization: credentials })

    Node.js

    After obtaining an access token and setting it to the OAuth2 object, use the object to call Google APIs. Your application can use that token to authorize API requests on behalf of a given user account or service account. Build a service object for the API that you want to call. For example, the following code uses the Google Drive API to list filenames in the user's Drive.

    const { google } = require('googleapis');
    
    // Example of using Google Drive API to list filenames in user's Drive.
    const drive = google.drive('v3');
    drive.files.list({
      auth: oauth2Client,
      pageSize: 10,
      fields: 'nextPageToken, files(id, name)',
    }, (err1, res1) => {
      if (err1) return console.log('The API returned an error: ' + err1);
      const files = res1.data.files;
      if (files.length) {
        console.log('Files:');
        files.map((file) => {
          console.log(`${file.name} (${file.id})`);
        });
      } else {
        console.log('No files found.');
      }
    });

    HTTP/REST

    After your application obtains an access token, you can use the token to make calls to a Google API on behalf of a given user account if the scope(s) of access required by the API have been granted. To do this, include the access token in a request to the API by including either an access_token query parameter or an Authorization HTTP header Bearer value. When possible, the HTTP header is preferable, because query strings tend to be visible in server logs. In most cases you can use a client library to set up your calls to Google APIs (for example, when calling the Drive Files API ).

    You can try out all the Google APIs and view their scopes at the OAuth 2.0 Playground .

    HTTP GET examples

    A call to the drive.files endpoint (the Drive Files API) using the Authorization: Bearer HTTP header might look like the following. Note that you need to specify your own access token:

    GET /drive/v2/files HTTP/1.1
    Host: www.googleapis.com
    Authorization: Bearer access_token

    Here is a call to the same API for the authenticated user using the access_token query string parameter:

    GET https://www.googleapis.com/drive/v2/files?access_token=access_token

    curl examples

    You can test these commands with the curl command-line application. Here's an example that uses the HTTP header option (preferred):

    curl -H "Authorization: Bearer access_token" https://www.googleapis.com/drive/v2/files

    Or, alternatively, the query string parameter option:

    curl https://www.googleapis.com/drive/v2/files?access_token=access_token

    Complete example

    The following example prints a JSON-formatted list of files in a user's Google Drive after the user authenticates and gives consent for the application to access the user's Drive metadata.

    PHP

    To run this example:

    1. В API Console, add the URL of the local machine to the list of redirect URLs. For example, add http://localhost:8080 .
    2. Create a new directory and change to it. Например:
      mkdir ~/php-oauth2-example
      cd ~/php-oauth2-example
    3. Install the Google API Client Library for PHP using Composer :
      composer require google/apiclient:^2.15.0
    4. Create the files index.php and oauth2callback.php with the following content.
    5. Run the example with the PHP's built-in test web server:
      php -S localhost:8080 ~/php-oauth2-example

    index.php

    <?php
    require_once __DIR__.'/vendor/autoload.php';
    
    session_start();
    
    $client = new Google\Client();
    $client->setAuthConfig('client_secret.json');
    
    // User granted permission as an access token is in the session.
    if (isset($_SESSION['access_token']) && $_SESSION['access_token'])
    {
      $client->setAccessToken($_SESSION['access_token']);
      
      // Check if user granted Drive permission
      if ($_SESSION['granted_scopes_dict']['Drive']) {
        echo "Drive feature is enabled.";
        echo "</br>";
        $drive = new Drive($client);
        $files = array();
        $response = $drive->files->listFiles(array());
        foreach ($response->files as $file) {
            echo "File: " . $file->name . " (" . $file->id . ")";
            echo "</br>";
        }
      } else {
        echo "Drive feature is NOT enabled.";
        echo "</br>";
      }
    
       // Check if user granted Calendar permission
      if ($_SESSION['granted_scopes_dict']['Calendar']) {
        echo "Calendar feature is enabled.";
        echo "</br>";
      } else {
        echo "Calendar feature is NOT enabled.";
        echo "</br>";
      }
    }
    else
    {
      // Redirect users to outh2call.php which redirects users to Google OAuth 2.0
      $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php';
      header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
    }
    ?>

    oauth2callback.php

    <?php
    require_once __DIR__.'/vendor/autoload.php';
    
    session_start();
    
    $client = new Google\Client();
    
    // Required, call the setAuthConfig function to load authorization credentials from
    // client_secret.json file.
    $client->setAuthConfigFile('client_secret.json');
    $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST']. $_SERVER['PHP_SELF']);
    
    // Required, to set the scope value, call the addScope function.
    $client->addScope([Google\Service\Drive::DRIVE_METADATA_READONLY, Google\Service\Calendar::CALENDAR_READONLY]);
    
    // Enable incremental authorization. Recommended as a best practice.
    $client->setIncludeGrantedScopes(true);
    
    // Recommended, offline access will give you both an access and refresh token so that
    // your app can refresh the access token without user interaction.
    $client->setAccessType("offline");
    
    // Generate a URL for authorization as it doesn't contain code and error
    if (!isset($_GET['code']) && !isset($_GET['error']))
    {
      // Generate and set state value
      $state = bin2hex(random_bytes(16));
      $client->setState($state);
      $_SESSION['state'] = $state;
    
      // Generate a url that asks permissions.
      $auth_url = $client->createAuthUrl();
      header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
    }
    
    // User authorized the request and authorization code is returned to exchange access and
    // refresh tokens.
    if (isset($_GET['code']))
    {
      // Check the state value
      if (!isset($_GET['state']) || $_GET['state'] !== $_SESSION['state']) {
        die('State mismatch. Possible CSRF attack.');
      }
    
      // Get access and refresh tokens (if access_type is offline)
      $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
    
      /** Save access and refresh token to the session variables.
        * ACTION ITEM: In a production app, you likely want to save the
        *              refresh token in a secure persistent storage instead. */
      $_SESSION['access_token'] = $token;
      $_SESSION['refresh_token'] = $client->getRefreshToken();
      
      // Space-separated string of granted scopes if it exists, otherwise null.
      $granted_scopes = $client->getOAuth2Service()->getGrantedScope();
    
      // Determine which scopes user granted and build a dictionary
      $granted_scopes_dict = [
        'Drive' => str_contains($granted_scopes, Google\Service\Drive::DRIVE_METADATA_READONLY),
        'Calendar' => str_contains($granted_scopes, Google\Service\Calendar::CALENDAR_READONLY)
      ];
      $_SESSION['granted_scopes_dict'] = $granted_scopes_dict;
      
      $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/';
      header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
    }
    
    // An error response e.g. error=access_denied
    if (isset($_GET['error']))
    {
      echo "Error: ". $_GET['error'];
    }
    ?>

    Питон

    This example uses the Flask framework. It runs a web application at http://localhost:8080 that lets you test the OAuth 2.0 flow. If you go to that URL, you should see five links:

    • Call Drive API: This link points to a page that tries to execute a sample API request if users granted the permission. If necessary, it starts the authorization flow. If successful, the page displays the API response.
    • Mock page to call Calendar API: This link points to a maockpage that tries to execute a sample Calendar API request if users granted the permission. If necessary, it starts the authorization flow. If successful, the page displays the API response.
    • Test the auth flow directly: This link points to a page that tries to send the user through the authorization flow . The app requests permission to submit authorized API requests on the user's behalf.
    • Revoke current credentials: This link points to a page that revokes permissions that the user has already granted to the application.
    • Clear Flask session credentials: This link clears authorization credentials that are stored in the Flask session. This lets you see what would happen if a user who had already granted permission to your app tried to execute an API request in a new session. It also lets you see the API response your app would get if a user had revoked permissions granted to your app, and your app still tried to authorize a request with a revoked access token.
    # -*- coding: utf-8 -*-
    
    import os
    import flask
    import requests
    
    import google.oauth2.credentials
    import google_auth_oauthlib.flow
    import googleapiclient.discovery
    
    # This variable specifies the name of a file that contains the OAuth 2.0
    # information for this application, including its client_id and client_secret.
    CLIENT_SECRETS_FILE = "client_secret.json"
    
    # The OAuth 2.0 access scope allows for access to the
    # authenticated user's account and requires requests to use an SSL connection.
    SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly',
              'https://www.googleapis.com/auth/calendar.readonly']
    API_SERVICE_NAME = 'drive'
    API_VERSION = 'v2'
    
    app = flask.Flask(__name__)
    # Note: A secret key is included in the sample so that it works.
    # If you use this code in your application, replace this with a truly secret
    # key. See https://flask.palletsprojects.com/quickstart/#sessions.
    app.secret_key = 'REPLACE ME - this value is here as a placeholder.'
    
    @app.route('/')
    def index():
      return print_index_table()
    
    @app.route('/drive')
    def drive_api_request():
      if 'credentials' not in flask.session:
        return flask.redirect('authorize')
    
      features = flask.session['features']
    
      if features['drive']:
        # Load credentials from the session.
        credentials = google.oauth2.credentials.Credentials(
            **flask.session['credentials'])
    
        drive = googleapiclient.discovery.build(
            API_SERVICE_NAME, API_VERSION, credentials=credentials)
    
        files = drive.files().list().execute()
    
        # Save credentials back to session in case access token was refreshed.
        # ACTION ITEM: In a production app, you likely want to save these
        #              credentials in a persistent database instead.
        flask.session['credentials'] = credentials_to_dict(credentials)
    
        return flask.jsonify(**files)
      else:
        # User didn't authorize read-only Drive activity permission.
        # Update UX and application accordingly
        return '<p>Drive feature is not enabled.</p>'
    
    @app.route('/calendar')
        def calendar_api_request():
          if 'credentials' not in flask.session:
            return flask.redirect('authorize')
    
          features = flask.session['features']
    
          if features['calendar']:
            # User authorized Calendar read permission.
            # Calling the APIs, etc.
            return ('<p>User granted the Google Calendar read permission. '+
                    'This sample code does not include code to call Calendar</p>')
          else:
            # User didn't authorize Calendar read permission.
            # Update UX and application accordingly
            return '<p>Calendar feature is not enabled.</p>'
    
    @app.route('/authorize')
    def authorize():
      # Create flow instance to manage the OAuth 2.0 Authorization Grant Flow steps.
      flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
          CLIENT_SECRETS_FILE, scopes=SCOPES)
    
      # The URI created here must exactly match one of the authorized redirect URIs
      # for the OAuth 2.0 client, which you configured in the API Console. If this
      # value doesn't match an authorized URI, you will get a 'redirect_uri_mismatch'
      # error.
      flow.redirect_uri = flask.url_for('oauth2callback', _external=True)
    
      authorization_url, state = flow.authorization_url(
          # Enable offline access so that you can refresh an access token without
          # re-prompting the user for permission. Recommended for web server apps.
          access_type='offline',
          # Enable incremental authorization. Recommended as a best practice.
          include_granted_scopes='true')
    
      # Store the state so the callback can verify the auth server response.
      flask.session['state'] = state
    
      return flask.redirect(authorization_url)
    
    @app.route('/oauth2callback')
    def oauth2callback():
      # Specify the state when creating the flow in the callback so that it can
      # verified in the authorization server response.
      state = flask.session['state']
    
      flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
          CLIENT_SECRETS_FILE, scopes=SCOPES, state=state)
      flow.redirect_uri = flask.url_for('oauth2callback', _external=True)
    
      # Use the authorization server's response to fetch the OAuth 2.0 tokens.
      authorization_response = flask.request.url
      flow.fetch_token(authorization_response=authorization_response)
    
      # Store credentials in the session.
      # ACTION ITEM: In a production app, you likely want to save these
      #              credentials in a persistent database instead.
      credentials = flow.credentials
      
      credentials = credentials_to_dict(credentials)
      flask.session['credentials'] = credentials
    
      # Check which scopes user granted
      features = check_granted_scopes(credentials)
      flask.session['features'] = features
      return flask.redirect('/')
      
    
    @app.route('/revoke')
    def revoke():
      if 'credentials' not in flask.session:
        return ('You need to <a href="/authorize">authorize</a> before ' +
                'testing the code to revoke credentials.')
    
      credentials = google.oauth2.credentials.Credentials(
        **flask.session['credentials'])
    
      revoke = requests.post('https://oauth2.googleapis.com/revoke',
          params={'token': credentials.token},
          headers = {'content-type': 'application/x-www-form-urlencoded'})
    
      status_code = getattr(revoke, 'status_code')
      if status_code == 200:
        return('Credentials successfully revoked.' + print_index_table())
      else:
        return('An error occurred.' + print_index_table())
    
    @app.route('/clear')
    def clear_credentials():
      if 'credentials' in flask.session:
        del flask.session['credentials']
      return ('Credentials have been cleared.<br><br>' +
              print_index_table())
    
    def credentials_to_dict(credentials):
      return {'token': credentials.token,
              'refresh_token': credentials.refresh_token,
              'token_uri': credentials.token_uri,
              'client_id': credentials.client_id,
              'client_secret': credentials.client_secret,
              'granted_scopes': credentials.granted_scopes}
    
    def check_granted_scopes(credentials):
      features = {}
      if 'https://www.googleapis.com/auth/drive.metadata.readonly' in credentials['granted_scopes']:
        features['drive'] = True
      else:
        features['drive'] = False
    
      if 'https://www.googleapis.com/auth/calendar.readonly' in credentials['granted_scopes']:
        features['calendar'] = True
      else:
        features['calendar'] = False
    
      return features
    
    def print_index_table():
      return ('<table>' +
              '<tr><td><a href="/test">Test an API request</a></td>' +
              '<td>Submit an API request and see a formatted JSON response. ' +
              '    Go through the authorization flow if there are no stored ' +
              '    credentials for the user.</td></tr>' +
              '<tr><td><a href="/authorize">Test the auth flow directly</a></td>' +
              '<td>Go directly to the authorization flow. If there are stored ' +
              '    credentials, you still might not be prompted to reauthorize ' +
              '    the application.</td></tr>' +
              '<tr><td><a href="/revoke">Revoke current credentials</a></td>' +
              '<td>Revoke the access token associated with the current user ' +
              '    session. After revoking credentials, if you go to the test ' +
              '    page, you should see an <code>invalid_grant</code> error.' +
              '</td></tr>' +
              '<tr><td><a href="/clear">Clear Flask session credentials</a></td>' +
              '<td>Clear the access token currently stored in the user session. ' +
              '    After clearing the token, if you <a href="/test">test the ' +
              '    API request</a> again, you should go back to the auth flow.' +
              '</td></tr></table>')
    
    if __name__ == '__main__':
      # When running locally, disable OAuthlib's HTTPs verification.
      # ACTION ITEM for developers:
      #     When running in production *do not* leave this option enabled.
      os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
    
      # This disables the requested scopes and granted scopes check.
      # If users only grant partial request, the warning would not be thrown.
      os.environ['OAUTHLIB_RELAX_TOKEN_SCOPE'] = '1'
    
      # Specify a hostname and port that are set as a valid redirect URI
      # for your API project in the Google API Console.
      app.run('localhost', 8080, debug=True)

    Руби

    This example uses the Sinatra framework.

    require 'googleauth'
    require 'googleauth/web_user_authorizer'
    require 'googleauth/stores/redis_token_store'
    
    require 'google/apis/drive_v3'
    require 'google/apis/calendar_v3'
    
    require 'sinatra'
    
    configure do
      enable :sessions
    
      # Required, call the from_file method to retrieve the client ID from a
      # client_secret.json file.
      set :client_id, Google::Auth::ClientId.from_file('/path/to/client_secret.json')
    
      # Required, scope value
      # Access scopes for two non-Sign-In scopes: Read-only Drive activity and Google Calendar.
      scope = ['Google::Apis::DriveV3::AUTH_DRIVE_METADATA_READONLY',
               'Google::Apis::CalendarV3::AUTH_CALENDAR_READONLY']
    
      # Required, Authorizers require a storage instance to manage long term persistence of
      # access and refresh tokens.
      set :token_store, Google::Auth::Stores::RedisTokenStore.new(redis: Redis.new)
    
      # Required, indicate where the API server will redirect the user after the user completes
      # the authorization flow. The redirect URI is required. The value must exactly
      # match one of the authorized redirect URIs for the OAuth 2.0 client, which you
      # configured in the API Console. If this value doesn't match an authorized URI,
      # you will get a 'redirect_uri_mismatch' error.
      set :callback_uri, '/oauth2callback'
    
      # To use OAuth2 authentication, we need access to a CLIENT_ID, CLIENT_SECRET, AND REDIRECT_URI
      # from the client_secret.json file. To get these credentials for your application, visit
      # https://console.cloud.google.com/apis/credentials.
      set :authorizer, Google::Auth::WebUserAuthorizer.new(settings.client_id, settings.scope,
                              settings.token_store, callback_uri: settings.callback_uri)
    end
    
    get '/' do
      # NOTE: Assumes the user is already authenticated to the app
      user_id = request.session['user_id']
    
      # Fetch stored credentials for the user from the given request session.
      # nil if none present
      credentials = settings.authorizer.get_credentials(user_id, request)
    
      if credentials.nil?
        # Generate a url that asks the user to authorize requested scope(s).
        # Then, redirect user to the url.
        redirect settings.authorizer.get_authorization_url(request: request)
      end
      
      # User authorized the request. Now, check which scopes were granted.
      if credentials.scope.include?(Google::Apis::DriveV3::AUTH_DRIVE_METADATA_READONLY)
        # User authorized read-only Drive activity permission.
        # Example of using Google Drive API to list filenames in user's Drive.
        drive = Google::Apis::DriveV3::DriveService.new
        files = drive.list_files(options: { authorization: credentials })
        "<pre>#{JSON.pretty_generate(files.to_h)}</pre>"
      else
        # User didn't authorize read-only Drive activity permission.
        # Update UX and application accordingly
      end
    
      # Check if user authorized Calendar read permission.
      if credentials.scope.include?(Google::Apis::CalendarV3::AUTH_CALENDAR_READONLY)
        # User authorized Calendar read permission.
        # Calling the APIs, etc.
      else
        # User didn't authorize Calendar read permission.
        # Update UX and application accordingly
      end
    end
    
    # Receive the callback from Google's OAuth 2.0 server.
    get '/oauth2callback' do
      # Handle the result of the oauth callback. Defers the exchange of the code by
      # temporarily stashing the results in the user's session.
      target_url = Google::Auth::WebUserAuthorizer.handle_auth_callback_deferred(request)
      redirect target_url
    end

    Node.js

    To run this example:

    1. В API Console, add the URL of the local machine to the list of redirect URLs. For example, add http://localhost .
    2. Make sure you have maintenance LTS, active LTS, or current release of Node.js installed.
    3. Create a new directory and change to it. Например:
      mkdir ~/nodejs-oauth2-example
      cd ~/nodejs-oauth2-example
    4. Install the Google API Client Library for Node.js using npm :
      npm install googleapis
    5. Create the files main.js with the following content.
    6. Run the example:
      node .\main.js

    main.js

    const http = require('http');
    const https = require('https');
    const url = require('url');
    const { google } = require('googleapis');
    const crypto = require('crypto');
    const express = require('express');
    const session = require('express-session');
    
    /**
     * To use OAuth2 authentication, we need access to a CLIENT_ID, CLIENT_SECRET, AND REDIRECT_URI.
     * To get these credentials for your application, visit
     * https://console.cloud.google.com/apis/credentials.
     */
    const oauth2Client = new google.auth.OAuth2(
      YOUR_CLIENT_ID,
      YOUR_CLIENT_SECRET,
      YOUR_REDIRECT_URL
    );
    
    // Access scopes for two non-Sign-In scopes: Read-only Drive activity and Google Calendar.
    const scopes = [
      'https://www.googleapis.com/auth/drive.metadata.readonly',
      'https://www.googleapis.com/auth/calendar.readonly'
    ];
    
    /* Global variable that stores user credential in this code example.
     * ACTION ITEM for developers:
     *   Store user's refresh token in your data store if
     *   incorporating this code into your real app.
     *   For more information on handling refresh tokens,
     *   see https://github.com/googleapis/google-api-nodejs-client#handling-refresh-tokens
     */
    let userCredential = null;
    
    async function main() {
      const app = express();
    
      app.use(session({
        secret: 'your_secure_secret_key', // Replace with a strong secret
        resave: false,
        saveUninitialized: false,
      }));
    
      // Example on redirecting user to Google's OAuth 2.0 server.
      app.get('/', async (req, res) => {
        // Generate a secure random state value.
        const state = crypto.randomBytes(32).toString('hex');
        // Store state in the session
        req.session.state = state;
    
        // Generate a url that asks permissions for the Drive activity and Google Calendar scope
        const authorizationUrl = oauth2Client.generateAuthUrl({
          // 'online' (default) or 'offline' (gets refresh_token)
          access_type: 'offline',
          /** Pass in the scopes array defined above.
            * Alternatively, if only one scope is needed, you can pass a scope URL as a string */
          scope: scopes,
          // Enable incremental authorization. Recommended as a best practice.
          include_granted_scopes: true,
          // Include the state parameter to reduce the risk of CSRF attacks.
          state: state
        });
    
        res.redirect(authorizationUrl);
      });
    
      // Receive the callback from Google's OAuth 2.0 server.
      app.get('/oauth2callback', async (req, res) => {
        // Handle the OAuth 2.0 server response
        let q = url.parse(req.url, true).query;
    
        if (q.error) { // An error response e.g. error=access_denied
          console.log('Error:' + q.error);
        } else if (q.state !== req.session.state) { //check state value
          console.log('State mismatch. Possible CSRF attack');
          res.end('State mismatch. Possible CSRF attack');
        } else { // Get access and refresh tokens (if access_type is offline)
          let { tokens } = await oauth2Client.getToken(q.code);
          oauth2Client.setCredentials(tokens);
    
          /** Save credential to the global variable in case access token was refreshed.
            * ACTION ITEM: In a production app, you likely want to save the refresh token
            *              in a secure persistent database instead. */
          userCredential = tokens;
          
          // User authorized the request. Now, check which scopes were granted.
          if (tokens.scope.includes('https://www.googleapis.com/auth/drive.metadata.readonly'))
          {
            // User authorized read-only Drive activity permission.
            // Example of using Google Drive API to list filenames in user's Drive.
            const drive = google.drive('v3');
            drive.files.list({
              auth: oauth2Client,
              pageSize: 10,
              fields: 'nextPageToken, files(id, name)',
            }, (err1, res1) => {
              if (err1) return console.log('The API returned an error: ' + err1);
              const files = res1.data.files;
              if (files.length) {
                console.log('Files:');
                files.map((file) => {
                  console.log(`${file.name} (${file.id})`);
                });
              } else {
                console.log('No files found.');
              }
            });
          }
          else
          {
            // User didn't authorize read-only Drive activity permission.
            // Update UX and application accordingly
          }
    
          // Check if user authorized Calendar read permission.
          if (tokens.scope.includes('https://www.googleapis.com/auth/calendar.readonly'))
          {
            // User authorized Calendar read permission.
            // Calling the APIs, etc.
          }
          else
          {
            // User didn't authorize Calendar read permission.
            // Update UX and application accordingly
          }
        }
      });
    
      // Example on revoking a token
      app.get('/revoke', async (req, res) => {
        // Build the string for the POST request
        let postData = "token=" + userCredential.access_token;
    
        // Options for POST request to Google's OAuth 2.0 server to revoke a token
        let postOptions = {
          host: 'oauth2.googleapis.com',
          port: '443',
          path: '/revoke',
          method: 'POST',
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': Buffer.byteLength(postData)
          }
        };
    
        // Set up the request
        const postReq = https.request(postOptions, function (res) {
          res.setEncoding('utf8');
          res.on('data', d => {
            console.log('Response: ' + d);
          });
        });
    
        postReq.on('error', error => {
          console.log(error)
        });
    
        // Post the request with data
        postReq.write(postData);
        postReq.end();
      });
    
    
      const server = http.createServer(app);
      server.listen(8080);
    }
    main().catch(console.error);

    HTTP/REST

    This Python example uses the Flask framework and the Requests library to demonstrate the OAuth 2.0 web flow. We recommend using the Google API Client Library for Python for this flow. (The example in the Python tab does use the client library.)

    import json
    import flask
    import requests
    
    app = flask.Flask(__name__)
    
    # To get these credentials (CLIENT_ID CLIENT_SECRET) and for your application, visit
    # https://console.cloud.google.com/apis/credentials.
    CLIENT_ID = '123456789.apps.googleusercontent.com'
    CLIENT_SECRET = 'abc123'  # Read from a file or environmental variable in a real app
    
    # Access scopes for two non-Sign-In scopes: Read-only Drive activity and Google Calendar.
    SCOPE = 'https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/calendar.readonly'
    
    # Indicate where the API server will redirect the user after the user completes
    # the authorization flow. The redirect URI is required. The value must exactly
    # match one of the authorized redirect URIs for the OAuth 2.0 client, which you
    # configured in the API Console. If this value doesn't match an authorized URI,
    # you will get a 'redirect_uri_mismatch' error.
    REDIRECT_URI = 'http://example.com/oauth2callback'
    
    @app.route('/')
    def index():
      if 'credentials' not in flask.session:
        return flask.redirect(flask.url_for('oauth2callback'))
    
      credentials = json.loads(flask.session['credentials'])
    
      if credentials['expires_in'] <= 0:
        return flask.redirect(flask.url_for('oauth2callback'))
      else: 
        # User authorized the request. Now, check which scopes were granted.
        if 'https://www.googleapis.com/auth/drive.metadata.readonly' in credentials['scope']:
          # User authorized read-only Drive activity permission.
          # Example of using Google Drive API to list filenames in user's Drive.
          headers = {'Authorization': 'Bearer {}'.format(credentials['access_token'])}
          req_uri = 'https://www.googleapis.com/drive/v2/files'
          r = requests.get(req_uri, headers=headers).text
        else:
          # User didn't authorize read-only Drive activity permission.
          # Update UX and application accordingly
          r = 'User did not authorize Drive permission.'
    
        # Check if user authorized Calendar read permission.
        if 'https://www.googleapis.com/auth/calendar.readonly' in credentials['scope']:
          # User authorized Calendar read permission.
          # Calling the APIs, etc.
          r += 'User authorized Calendar permission.'
        else:
          # User didn't authorize Calendar read permission.
          # Update UX and application accordingly
          r += 'User did not authorize Calendar permission.'
    
      return r
    
    @app.route('/oauth2callback')
    def oauth2callback():
      if 'code' not in flask.request.args:
        state = str(uuid.uuid4())
        flask.session['state'] = state
        # Generate a url that asks permissions for the Drive activity
        # and Google Calendar scope. Then, redirect user to the url.
        auth_uri = ('https://accounts.google.com/o/oauth2/v2/auth?response_type=code'
                    '&client_id={}&redirect_uri={}&scope={}&state={}').format(CLIENT_ID, REDIRECT_URI,
                                                                              SCOPE, state)
        return flask.redirect(auth_uri)
      else:
        if 'state' not in flask.request.args or flask.request.args['state'] != flask.session['state']:
          return 'State mismatch. Possible CSRF attack.', 400
    
        auth_code = flask.request.args.get('code')
        data = {'code': auth_code,
                'client_id': CLIENT_ID,
                'client_secret': CLIENT_SECRET,
                'redirect_uri': REDIRECT_URI,
                'grant_type': 'authorization_code'}
    
        # Exchange authorization code for access and refresh tokens (if access_type is offline)
        r = requests.post('https://oauth2.googleapis.com/token', data=data)
        flask.session['credentials'] = r.text
        return flask.redirect(flask.url_for('index'))
    
    if __name__ == '__main__':
      import uuid
      app.secret_key = str(uuid.uuid4())
      app.debug = False
      app.run()

    Redirect URI validation rules

    Google applies the following validation rules to redirect URIs in order to help developers keep their applications secure. Your redirect URIs must adhere to these rules. See RFC 3986 section 3 for the definition of domain, host, path, query, scheme and userinfo, mentioned below.

    Validation rules
    Схема

    Redirect URIs must use the HTTPS scheme, not plain HTTP. Localhost URIs (including localhost IP address URIs) are exempt from this rule.

    Хозяин

    Hosts cannot be raw IP addresses. Localhost IP addresses are exempted from this rule.

    Домен
  • Host TLDs ( Top Level Domains ) must belong to the public suffix list .
  • Host domains cannot be “googleusercontent.com” .
  • Redirect URIs cannot contain URL shortener domains (eg goo.gl ) unless the app owns the domain. Furthermore, if an app that owns a shortener domain chooses to redirect to that domain, that redirect URI must either contain “/google-callback/” in its path or end with “/google-callback” .
  • Userinfo

    Redirect URIs cannot contain the userinfo subcomponent.

    Путь

    Redirect URIs cannot contain a path traversal (also called directory backtracking), which is represented by an “/..” or “\..” or their URL encoding.

    Запрос

    Redirect URIs cannot contain open redirects .

    Фрагмент

    Redirect URIs cannot contain the fragment component.

    Персонажи Redirect URIs cannot contain certain characters including:
    • Wildcard characters ( '*' )
    • Non-printable ASCII characters
    • Invalid percent encodings (any percent encoding that does not follow URL-encoding form of a percent sign followed by two hexadecimal digits)
    • Null characters (an encoded NULL character, eg, %00 , %C0%80 )

    Incremental authorization

    In the OAuth 2.0 protocol, your app requests authorization to access resources, which are identified by scopes. It is considered a best user-experience practice to request authorization for resources at the time you need them. To enable that practice, Google's authorization server supports incremental authorization. This feature lets you request scopes as they are needed and, if the user grants permission for the new scope, returns an authorization code that may be exchanged for a token containing all scopes the user has granted the project.

    For example, an app that lets people sample music tracks and create mixes might need very few resources at sign-in time, perhaps nothing more than the name of the person signing in. However, saving a completed mix would require access to their Google Drive. Most people would find it natural if they only were asked for access to their Google Drive at the time the app actually needed it.

    In this case, at sign-in time the app might request the openid and profile scopes to perform basic sign-in, and then later request the https://www.googleapis.com/auth/drive.file scope at the time of the first request to save a mix.

    To implement incremental authorization, you complete the normal flow for requesting an access token but make sure that the authorization request includes previously granted scopes. This approach allows your app to avoid having to manage multiple access tokens.

    The following rules apply to an access token obtained from an incremental authorization:

    • The token can be used to access resources corresponding to any of the scopes rolled into the new, combined authorization.
    • When you use the refresh token for the combined authorization to obtain an access token, the access token represents the combined authorization and can be used for any of the scope values included in the response.
    • The combined authorization includes all scopes that the user granted to the API project even if the grants were requested from different clients. For example, if a user granted access to one scope using an application's desktop client and then granted another scope to the same application via a mobile client, the combined authorization would include both scopes.
    • If you revoke a token that represents a combined authorization, access to all of that authorization's scopes on behalf of the associated user are revoked simultaneously.

    The language-specific code samples in Step 1: Set authorization parameters and the sample HTTP/REST redirect URL in Step 2: Redirect to Google's OAuth 2.0 server all use incremental authorization. The code samples below also show the code that you need to add to use incremental authorization.

    PHP

    $client->setIncludeGrantedScopes(true);

    Питон

    In Python, set the include_granted_scopes keyword argument to true to ensure that an authorization request includes previously granted scopes. It is very possible that include_granted_scopes will not be the only keyword argument that you set, as shown in the example below.

    authorization_url, state = flow.authorization_url(
        # Enable offline access so that you can refresh an access token without
        # re-prompting the user for permission. Recommended for web server apps.
        access_type='offline',
        # Enable incremental authorization. Recommended as a best practice.
        include_granted_scopes='true')

    Руби

    auth_client.update!(
      :additional_parameters => {"include_granted_scopes" => "true"}
    )

    Node.js

    const authorizationUrl = oauth2Client.generateAuthUrl({
      // 'online' (default) or 'offline' (gets refresh_token)
      access_type: 'offline',
      /** Pass in the scopes array defined above.
        * Alternatively, if only one scope is needed, you can pass a scope URL as a string */
      scope: scopes,
      // Enable incremental authorization. Recommended as a best practice.
      include_granted_scopes: true
    });

    HTTP/REST

    GET https://accounts.google.com/o/oauth2/v2/auth?
      client_id=your_client_id&
      response_type=code&
      state=state_parameter_passthrough_value&
      scope=https%3A//www.googleapis.com/auth/drive.metadata.readonly%20https%3A//www.googleapis.com/auth/calendar.readonly&
      redirect_uri=https%3A//oauth2.example.com/code&
      prompt=consent&
      include_granted_scopes=true

    Refreshing an access token (offline access)

    Access tokens periodically expire and become invalid credentials for a related API request. You can refresh an access token without prompting the user for permission (including when the user is not present) if you requested offline access to the scopes associated with the token.

    • If you use a Google API Client Library, the client object refreshes the access token as needed as long as you configure that object for offline access.
    • If you are not using a client library, you need to set the access_type HTTP query parameter to offline when redirecting the user to Google's OAuth 2.0 server . In that case, Google's authorization server returns a refresh token when you exchange an authorization code for an access token. Then, if the access token expires (or at any other time), you can use a refresh token to obtain a new access token.

    Requesting offline access is a requirement for any application that needs to access a Google API when the user is not present. For example, an app that performs backup services or executes actions at predetermined times needs to be able to refresh its access token when the user is not present. The default style of access is called online .

    Server-side web applications, installed applications, and devices all obtain refresh tokens during the authorization process. Refresh tokens are not typically used in client-side (JavaScript) web applications.

    PHP

    If your application needs offline access to a Google API, set the API client's access type to offline :

    $client->setAccessType("offline");

    After a user grants offline access to the requested scopes, you can continue to use the API client to access Google APIs on the user's behalf when the user is offline. The client object will refresh the access token as needed.

    Питон

    In Python, set the access_type keyword argument to offline to ensure that you will be able to refresh the access token without having to re-prompt the user for permission. It is very possible that access_type will not be the only keyword argument that you set, as shown in the example below.

    authorization_url, state = flow.authorization_url(
        # Enable offline access so that you can refresh an access token without
        # re-prompting the user for permission. Recommended for web server apps.
        access_type='offline',
        # Enable incremental authorization. Recommended as a best practice.
        include_granted_scopes='true')

    After a user grants offline access to the requested scopes, you can continue to use the API client to access Google APIs on the user's behalf when the user is offline. The client object will refresh the access token as needed.

    Руби

    If your application needs offline access to a Google API, set the API client's access type to offline :

    auth_client.update!(
      :additional_parameters => {"access_type" => "offline"}
    )

    After a user grants offline access to the requested scopes, you can continue to use the API client to access Google APIs on the user's behalf when the user is offline. The client object will refresh the access token as needed.

    Node.js

    If your application needs offline access to a Google API, set the API client's access type to offline :

    const authorizationUrl = oauth2Client.generateAuthUrl({
      // 'online' (default) or 'offline' (gets refresh_token)
      access_type: 'offline',
      /** Pass in the scopes array defined above.
        * Alternatively, if only one scope is needed, you can pass a scope URL as a string */
      scope: scopes,
      // Enable incremental authorization. Recommended as a best practice.
      include_granted_scopes: true
    });

    After a user grants offline access to the requested scopes, you can continue to use the API client to access Google APIs on the user's behalf when the user is offline. The client object will refresh the access token as needed.

    Access tokens expire. This library will automatically use a refresh token to obtain a new access token if it is about to expire. An easy way to make sure you always store the most recent tokens is to use the tokens event:

    oauth2Client.on('tokens', (tokens) => {
      if (tokens.refresh_token) {
        // store the refresh_token in your secure persistent database
        console.log(tokens.refresh_token);
      }
      console.log(tokens.access_token);
    });

    This tokens event only occurs in the first authorization, and you need to have set your access_type to offline when calling the generateAuthUrl method to receive the refresh token. If you have already given your app the requisiste permissions without setting the appropriate constraints for receiving a refresh token, you will need to re-authorize the application to receive a fresh refresh token.

    To set the refresh_token at a later time, you can use the setCredentials method:

    oauth2Client.setCredentials({
      refresh_token: `STORED_REFRESH_TOKEN`
    });

    Once the client has a refresh token, access tokens will be acquired and refreshed automatically in the next call to the API.

    HTTP/REST

    To refresh an access token, your application sends an HTTPS POST request to Google's authorization server ( https://oauth2.googleapis.com/token ) that includes the following parameters:

    Поля
    client_id The client ID obtained from the API Console.
    client_secret The client secret obtained from the API Console.
    grant_type As defined in the OAuth 2.0 specification , this field's value must be set to refresh_token .
    refresh_token The refresh token returned from the authorization code exchange.

    The following snippet shows a sample request:

    POST /token HTTP/1.1
    Host: oauth2.googleapis.com
    Content-Type: application/x-www-form-urlencoded
    
    client_id=your_client_id&
    client_secret=your_client_secret&
    refresh_token=refresh_token&
    grant_type=refresh_token

    As long as the user has not revoked the access granted to the application, the token server returns a JSON object that contains a new access token. The following snippet shows a sample response:

    {
      "access_token": "1/fFAGRNJru1FTz70BzhT3Zg",
      "expires_in": 3920,
      "scope": "https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/calendar.readonly",
      "token_type": "Bearer"
    }

    Note that there are limits on the number of refresh tokens that will be issued; one limit per client/user combination, and another per user across all clients. You should save refresh tokens in long-term storage and continue to use them as long as they remain valid. If your application requests too many refresh tokens, it may run into these limits, in which case older refresh tokens will stop working.

    Revoking a token

    In some cases a user may wish to revoke access given to an application. A user can revoke access by visiting Account Settings . See the Remove site or app access section of the Third-party sites & apps with access to your account support document for more information.

    It is also possible for an application to programmatically revoke the access given to it. Programmatic revocation is important in instances where a user unsubscribes, removes an application, or the API resources required by an app have significantly changed. In other words, part of the removal process can include an API request to ensure the permissions previously granted to the application are removed.

    PHP

    To programmatically revoke a token, call revokeToken() :

    $client->revokeToken();

    Питон

    To programmatically revoke a token, make a request to https://oauth2.googleapis.com/revoke that includes the token as a parameter and sets the Content-Type header:

    requests.post('https://oauth2.googleapis.com/revoke',
        params={'token': credentials.token},
        headers = {'content-type': 'application/x-www-form-urlencoded'})

    Руби

    To programmatically revoke a token, make an HTTP request to the oauth2.revoke endpoint:

    uri = URI('https://oauth2.googleapis.com/revoke')
    response = Net::HTTP.post_form(uri, 'token' => auth_client.access_token)

    The token can be an access token or a refresh token. If the token is an access token and it has a corresponding refresh token, the refresh token will also be revoked.

    If the revocation is successfully processed, then the status code of the response is 200 . For error conditions, a status code 400 is returned along with an error code.

    Node.js

    To programmatically revoke a token, make an HTTPS POST request to /revoke endpoint:

    const https = require('https');
    
    // Build the string for the POST request
    let postData = "token=" + userCredential.access_token;
    
    // Options for POST request to Google's OAuth 2.0 server to revoke a token
    let postOptions = {
      host: 'oauth2.googleapis.com',
      port: '443',
      path: '/revoke',
      method: 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': Buffer.byteLength(postData)
      }
    };
    
    // Set up the request
    const postReq = https.request(postOptions, function (res) {
      res.setEncoding('utf8');
      res.on('data', d => {
        console.log('Response: ' + d);
      });
    });
    
    postReq.on('error', error => {
      console.log(error)
    });
    
    // Post the request with data
    postReq.write(postData);
    postReq.end();

    The token parameter can be an access token or a refresh token. If the token is an access token and it has a corresponding refresh token, the refresh token will also be revoked.

    If the revocation is successfully processed, then the status code of the response is 200 . For error conditions, a status code 400 is returned along with an error code.

    HTTP/REST

    To programmatically revoke a token, your application makes a request to https://oauth2.googleapis.com/revoke and includes the token as a parameter:

    curl -d -X -POST --header "Content-type:application/x-www-form-urlencoded" \
            https://oauth2.googleapis.com/revoke?token={token}

    The token can be an access token or a refresh token. If the token is an access token and it has a corresponding refresh token, the refresh token will also be revoked.

    If the revocation is successfully processed, then the HTTP status code of the response is 200 . For error conditions, an HTTP status code 400 is returned along with an error code.

    Time-based access

    Time-based access allows a user to grant your app access to their data for a limited duration to complete an action. Time-based access is available in select Google products during the consent flow, giving users the option to grant access for a limited period of time. An example is the Data Portability API which enables a one-time transfer of data.

    When a user grants your application time-based access, the refresh token will expire after the specified duration. Note that refresh tokens may be invalidated earlier under specific circumstances; see these cases for details. The refresh_token_expires_in field returned in the authorization code exchange response represents the time remaining until the refresh token expires in such cases.

    Implementing Cross-Account Protection

    An additional step you should take to protect your users' accounts is implementing Cross-Account Protection by utilizing Google's Cross-Account Protection Service. This service lets you subscribe to security event notifications which provide information to your application about major changes to the user account. You can then use the information to take action depending on how you decide to respond to events.

    Some examples of the event types sent to your app by Google's Cross-Account Protection Service are:

    • https://schemas.openid.net/secevent/risc/event-type/sessions-revoked
    • https://schemas.openid.net/secevent/oauth/event-type/token-revoked
    • https://schemas.openid.net/secevent/risc/event-type/account-disabled

    See the Protect user accounts with Cross-Account Protection page for more information on how to implement Cross Account Protection and for the full list of available events.

    ,

    This document explains how web server applications use Google API Client Libraries or Google OAuth 2.0 endpoints 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.

    This OAuth 2.0 flow is specifically for user authorization. It is designed for applications that can store confidential information and maintain state. A properly authorized web server application can access an API while the user interacts with the application or after the user has left the application.

    Web server applications frequently also use service accounts to authorize API requests, particularly when calling Cloud APIs to access project-based data rather than user-specific data. Web server applications can use service accounts in conjunction with user authorization.

    Клиентские библиотеки

    The language-specific examples on this page use Google API Client Libraries to implement OAuth 2.0 authorization. To run the code samples, you must first install the client library for your language.

    When you use a Google API Client Library to handle your application's OAuth 2.0 flow, the client library performs many actions that the application would otherwise need to handle on its own. For example, it determines when the application can use or refresh stored access tokens as well as when the application must reacquire consent. The client library also generates correct redirect URLs and helps to implement redirect handlers that exchange authorization codes for access tokens.

    Google API Client Libraries for server-side applications are available for the following languages:

    Предварительные условия

    Enable APIs for your project

    Any application that calls Google APIs needs to enable those APIs in the API Console.

    To enable an API for your project:

    1. Open the API Library в Google API Console.
    2. If prompted, select a project, or create a new one.
    3. API Library lists all available APIs, grouped by product family and popularity. If the API you want to enable isn't visible in the list, use search to find it, or click View All in the product family it belongs to.
    4. Select the API you want to enable, then click the Enable button.
    5. If prompted, enable billing.
    6. If prompted, read and accept the API's Terms of Service.

    Create authorization credentials

    Any application that uses OAuth 2.0 to access Google APIs must have authorization credentials that identify the application to Google's OAuth 2.0 server. The following steps explain how to create credentials for your project. Your applications can then use the credentials to access APIs that you have enabled for that project.

    1. Go to the Credentials page.
    2. Click Create Client .
    3. Select the Web application application type.
    4. Fill in the form and click Create . Applications that use languages and frameworks like PHP, Java, Python, Ruby, and .NET must specify authorized redirect URIs . The redirect URIs are the endpoints to which the OAuth 2.0 server can send responses. These endpoints must adhere to Google's validation rules .

      For testing, you can specify URIs that refer to the local machine, such as http://localhost:8080 . With that in mind, please note that all of the examples in this document use http://localhost:8080 as the redirect URI.

      We recommend that you design your app's auth endpoints so that your application does not expose authorization codes to other resources on the page.

    After creating your credentials, download the client_secret.json file from the API Console. Securely store the file in a location that only your application can access.

    Identify access scopes

    Scopes enable your application to only request access to the resources that it needs while also enabling users to control the amount of access that they grant to your application. Thus, there may be an inverse relationship between the number of scopes requested and the likelihood of obtaining user consent.

    Before you start implementing OAuth 2.0 authorization, we recommend that you identify the scopes that your app will need permission to access.

    We also recommend that your application request access to authorization scopes via an incremental authorization process, in which your application requests access to user data in context. This best practice helps users to more easily understand why your application needs the access it is requesting.

    The OAuth 2.0 API Scopes document contains a full list of scopes that you might use to access Google APIs.

    Language-specific requirements

    To run any of the code samples in this document, you'll need a Google account, access to the Internet, and a web browser. If you are using one of the API client libraries, also see the language-specific requirements below.

    PHP

    To run the PHP code samples in this document, you'll need:

    • PHP 8.0 or greater with the command-line interface (CLI) and JSON extension installed.
    • The Composer dependency management tool.
    • The Google APIs Client Library for PHP:

      composer require google/apiclient:^2.15.0

    See Google APIs Client Library for PHP for more information.

    Питон

    To run the Python code samples in this document, you'll need:

    • Python 3.7 or greater
    • The pip package management tool.
    • The Google APIs Client Library for Python 2.0 release:
      pip install --upgrade google-api-python-client
    • The google-auth , google-auth-oauthlib , and google-auth-httplib2 for user authorization.
      pip install --upgrade google-auth google-auth-oauthlib google-auth-httplib2
    • The Flask Python web application framework.
      pip install --upgrade flask
    • The requests HTTP library.
      pip install --upgrade requests

    Review the Google API Python client library release note if you aren't able to upgrade python and associated migration guide.

    Руби

    To run the Ruby code samples in this document, you'll need:

    • Ruby 2.6 or greater
    • The Google Auth Library for Ruby:

      gem install googleauth
    • The client libraries for Drive and Calendar Google APIs:

      gem install google-apis-drive_v3 google-apis-calendar_v3
    • The Sinatra Ruby web application framework.

      gem install sinatra

    Node.js

    To run the Node.js code samples in this document, you'll need:

    • The maintenance LTS, active LTS, or current release of Node.js.
    • The Google APIs Node.js Client:

      npm install googleapis crypto express express-session

    HTTP/REST

    You do not need to install any libraries to be able to directly call the OAuth 2.0 endpoints.

    Obtaining OAuth 2.0 access tokens

    The following steps show how your application interacts with Google's OAuth 2.0 server to obtain a user's consent to perform an API request on the user's behalf. Your application must have that consent before it can execute a Google API request that requires user authorization.

    The list below quickly summarizes these steps:

    1. Your application identifies the permissions it needs.
    2. Your application redirects the user to Google along with the list of requested permissions.
    3. The user decides whether to grant the permissions to your application.
    4. Your application finds out what the user decided.
    5. If the user granted the requested permissions, your application retrieves tokens needed to make API requests on the user's behalf.

    Step 1: Set authorization parameters

    Your first step is to create the authorization request. That request sets parameters that identify your application and define the permissions that the user will be asked to grant to your application.

    • If you use a Google client library for OAuth 2.0 authentication and authorization, you create and configure an object that defines these parameters.
    • If you call the Google OAuth 2.0 endpoint directly, you'll generate a URL and set the parameters on that URL.

    The tabs below define the supported authorization parameters for web server applications. The language-specific examples also show how to use a client library or authorization library to configure an object that sets those parameters.

    PHP

    The following code snippet creates a Google\Client() object, which defines the parameters in the authorization request.

    That object uses information from your client_secret.json file to identify your application. (See creating authorization credentials for more about that file.) The object also identifies the scopes that your application is requesting permission to access and the URL to your application's auth endpoint, which will handle the response from Google's OAuth 2.0 server. Finally, the code sets the optional access_type and include_granted_scopes parameters.

    For example, this code requests read-only, offline access to a user's Google Drive metadata and Calendar events:

    use Google\Client;
    
    $client = new Client();
    
    // Required, call the setAuthConfig function to load authorization credentials from
    // client_secret.json file.
    $client->setAuthConfig('client_secret.json');
    
    // Required, to set the scope value, call the addScope function
    $client->addScope([Google\Service\Drive::DRIVE_METADATA_READONLY, Google\Service\Calendar::CALENDAR_READONLY]);
    
    // Required, call the setRedirectUri function to specify a valid redirect URI for the
    // provided client_id
    $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php');
    
    // Recommended, offline access will give you both an access and refresh token so that
    // your app can refresh the access token without user interaction.
    $client->setAccessType('offline');
    
    // Recommended, call the setState function. Using a state value can increase your assurance that
    // an incoming connection is the result of an authentication request.
    $client->setState($sample_passthrough_value);
    
    // Optional, if your application knows which user is trying to authenticate, it can use this
    // parameter to provide a hint to the Google Authentication Server.
    $client->setLoginHint('hint@example.com');
    
    // Optional, call the setPrompt function to set "consent" will prompt the user for consent
    $client->setPrompt('consent');
    
    // Optional, call the setIncludeGrantedScopes function with true to enable incremental
    // authorization
    $client->setIncludeGrantedScopes(true);

    Питон

    The following code snippet uses the google-auth-oauthlib.flow module to construct the authorization request.

    The code constructs a Flow object, which identifies your application using information from the client_secret.json file that you downloaded after creating authorization credentials . That object also identifies the scopes that your application is requesting permission to access and the URL to your application's auth endpoint, which will handle the response from Google's OAuth 2.0 server. Finally, the code sets the optional access_type and include_granted_scopes parameters.

    For example, this code requests read-only, offline access to a user's Google Drive metadata and Calendar events:

    import google.oauth2.credentials
    import google_auth_oauthlib.flow
    
    # Required, call the from_client_secrets_file method to retrieve the client ID from a
    # client_secret.json file. The client ID (from that file) and access scopes are required. (You can
    # also use the from_client_config method, which passes the client configuration as it originally
    # appeared in a client secrets file but doesn't access the file itself.)
    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file('client_secret.json',
        scopes=['https://www.googleapis.com/auth/drive.metadata.readonly',
                'https://www.googleapis.com/auth/calendar.readonly'])
    
    # Required, indicate where the API server will redirect the user after the user completes
    # the authorization flow. The redirect URI is required. The value must exactly
    # match one of the authorized redirect URIs for the OAuth 2.0 client, which you
    # configured in the API Console. If this value doesn't match an authorized URI,
    # you will get a 'redirect_uri_mismatch' error.
    flow.redirect_uri = 'https://www.example.com/oauth2callback'
    
    # Generate URL for request to Google's OAuth 2.0 server.
    # Use kwargs to set optional request parameters.
    authorization_url, state = flow.authorization_url(
        # Recommended, enable offline access so that you can refresh an access token without
        # re-prompting the user for permission. Recommended for web server apps.
        access_type='offline',
        # Optional, enable incremental authorization. Recommended as a best practice.
        include_granted_scopes='true',
        # Optional, if your application knows which user is trying to authenticate, it can use this
        # parameter to provide a hint to the Google Authentication Server.
        login_hint='hint@example.com',
        # Optional, set prompt to 'consent' will prompt the user for consent
        prompt='consent')

    Руби

    Use the client_secrets.json file that you created to configure a client object in your application. When you configure a client object, you specify the scopes your application needs to access, along with the URL to your application's auth endpoint, which will handle the response from the OAuth 2.0 server.

    For example, this code requests read-only, offline access to a user's Google Drive metadata and Calendar events:

    require 'googleauth'
    require 'googleauth/web_user_authorizer'
    require 'googleauth/stores/redis_token_store'
    
    require 'google/apis/drive_v3'
    require 'google/apis/calendar_v3'
    
    # Required, call the from_file method to retrieve the client ID from a
    # client_secret.json file.
    client_id = Google::Auth::ClientId.from_file('/path/to/client_secret.json')
    
    # Required, scope value 
    # Access scopes for two non-Sign-In scopes: Read-only Drive activity and Google Calendar.
    scope = ['Google::Apis::DriveV3::AUTH_DRIVE_METADATA_READONLY',
             'Google::Apis::CalendarV3::AUTH_CALENDAR_READONLY']
    
    # Required, Authorizers require a storage instance to manage long term persistence of
    # access and refresh tokens.
    token_store = Google::Auth::Stores::RedisTokenStore.new(redis: Redis.new)
    
    # Required, indicate where the API server will redirect the user after the user completes
    # the authorization flow. The redirect URI is required. The value must exactly
    # match one of the authorized redirect URIs for the OAuth 2.0 client, which you
    # configured in the API Console. If this value doesn't match an authorized URI,
    # you will get a 'redirect_uri_mismatch' error.
    callback_uri = '/oauth2callback'
    
    # To use OAuth2 authentication, we need access to a CLIENT_ID, CLIENT_SECRET, AND REDIRECT_URI
    # from the client_secret.json file. To get these credentials for your application, visit
    # https://console.cloud.google.com/apis/credentials.
    authorizer = Google::Auth::WebUserAuthorizer.new(client_id, scope,
                                                    token_store, callback_uri)

    Your application uses the client object to perform OAuth 2.0 operations, such as generating authorization request URLs and applying access tokens to HTTP requests.

    Node.js

    The following code snippet creates a google.auth.OAuth2 object, which defines the parameters in the authorization request.

    That object uses information from your client_secret.json file to identify your application. To ask for permissions from a user to retrieve an access token, you redirect them to a consent page. To create a consent page URL:

    const {google} = require('googleapis');
    const crypto = require('crypto');
    const express = require('express');
    const session = require('express-session');
    
    /**
     * To use OAuth2 authentication, we need access to a CLIENT_ID, CLIENT_SECRET, AND REDIRECT_URI
     * from the client_secret.json file. To get these credentials for your application, visit
     * https://console.cloud.google.com/apis/credentials.
     */
    const oauth2Client = new google.auth.OAuth2(
      YOUR_CLIENT_ID,
      YOUR_CLIENT_SECRET,
      YOUR_REDIRECT_URL
    );
    
    // Access scopes for two non-Sign-In scopes: Read-only Drive activity and Google Calendar.
    const scopes = [
      'https://www.googleapis.com/auth/drive.metadata.readonly',
      'https://www.googleapis.com/auth/calendar.readonly'
    ];
    
    // Generate a secure random state value.
    const state = crypto.randomBytes(32).toString('hex');
    
    // Store state in the session
    req.session.state = state;
    
    // Generate a url that asks permissions for the Drive activity and Google Calendar scope
    const authorizationUrl = oauth2Client.generateAuthUrl({
      // 'online' (default) or 'offline' (gets refresh_token)
      access_type: 'offline',
      /** Pass in the scopes array defined above.
        * Alternatively, if only one scope is needed, you can pass a scope URL as a string */
      scope: scopes,
      // Enable incremental authorization. Recommended as a best practice.
      include_granted_scopes: true,
      // Include the state parameter to reduce the risk of CSRF attacks.
      state: state
    });

    Important Note - The refresh_token is only returned on the first authorization. More details here .

    HTTP/REST

    Google's OAuth 2.0 endpoint is at https://accounts.google.com/o/oauth2/v2/auth . This endpoint is accessible only over HTTPS. Plain HTTP connections are refused.

    The Google authorization server supports the following query string parameters for web server applications:

    Параметры
    client_id Необходимый

    The client ID for your application. You can find this value in the .

    redirect_uri Необходимый

    Determines where the API server redirects the user after the user completes the authorization flow. The value must exactly match one of the authorized redirect URIs for the OAuth 2.0 client, which you configured in your client's . If this value doesn't match an authorized redirect URI for the provided client_id you will get a redirect_uri_mismatch error.

    Note that the http or https scheme, case, and trailing slash (' / ') must all match.

    response_type Необходимый

    Determines whether the Google OAuth 2.0 endpoint returns an authorization code.

    Set the parameter value to code for web server applications.

    scope Необходимый

    A space-delimited list of scopes that identify the resources that your application could access on the user's behalf. These values inform the consent screen that Google displays to the user.

    Scopes enable your application to only request access to the resources that it needs while also enabling users to control the amount of access that they grant to your application. Thus, there is an inverse relationship between the number of scopes requested and the likelihood of obtaining user consent.

    We recommend that your application request access to authorization scopes in context whenever possible. By requesting access to user data in context, via incremental authorization , you help users to more easily understand why your application needs the access it is requesting.

    access_type Рекомендуется

    Indicates whether your application can refresh access tokens when the user is not present at the browser. Valid parameter values are online , which is the default value, and offline .

    Set the value to offline if your application needs to refresh access tokens when the user is not present at the browser. This is the method of refreshing access tokens described later in this document. This value instructs the Google authorization server to return a refresh token and an access token the first time that your application exchanges an authorization code for tokens.

    state Рекомендуется

    Specifies any string value that your application uses to maintain state between your authorization request and the authorization server's response. The server returns the exact value that you send as a name=value pair in the URL query component ( ? ) of the redirect_uri after the user consents to or denies your application's access request.

    You can use this parameter for several purposes, such as directing the user to the correct resource in your application, sending nonces, and mitigating cross-site request forgery. Since your redirect_uri can be guessed, using a state value can increase your assurance that an incoming connection is the result of an authentication request. If you generate a random string or encode the hash of a cookie or another value that captures the client's state, you can validate the response to additionally ensure that the request and response originated in the same browser, providing protection against attacks such as cross-site request forgery . See the OpenID Connect documentation for an example of how to create and confirm a state token.

    include_granted_scopes Необязательный

    Enables applications to use incremental authorization to request access to additional scopes in context. If you set this parameter's value to true and the authorization request is granted, then the new access token will also cover any scopes to which the user previously granted the application access. See the incremental authorization section for examples.

    login_hint Необязательный

    If your application knows which user is trying to authenticate, it can use this parameter to provide a hint to the Google Authentication Server. The server uses the hint to simplify the login flow either by prefilling the email field in the sign-in form or by selecting the appropriate multi-login session.

    Set the parameter value to an email address or sub identifier, which is equivalent to the user's Google ID.

    prompt Необязательный

    A space-delimited, case-sensitive list of prompts to present the user. If you don't specify this parameter, the user will be prompted only the first time your project requests access. See Prompting re-consent for more information.

    Возможные значения:

    none Do not display any authentication or consent screens. Must not be specified with other values.
    consent Prompt the user for consent.
    select_account Prompt the user to select an account.

    Step 2: Redirect to Google's OAuth 2.0 server

    Redirect the user to Google's OAuth 2.0 server to initiate the authentication and authorization process. Typically, this occurs when your application first needs to access the user's data. In the case of incremental authorization , this step also occurs when your application first needs to access additional resources that it does not yet have permission to access.

    PHP

    1. Generate a URL to request access from Google's OAuth 2.0 server:
      $auth_url = $client->createAuthUrl();
    2. Redirect the user to $auth_url :
      header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));

    Питон

    This example shows how to redirect the user to the authorization URL using the Flask web application framework:

    return flask.redirect(authorization_url)

    Руби

    1. Generate a URL to request access from Google's OAuth 2.0 server:
      auth_uri = authorizer.get_authorization_url(request: request)
    2. Redirect the user to auth_uri .

    Node.js

    1. Use the generated URL authorizationUrl from Step 1 generateAuthUrl method to request access from Google's OAuth 2.0 server.
    2. Redirect the user to authorizationUrl .
      res.redirect(authorizationUrl);

    HTTP/REST

    Sample redirect to Google's authorization server

    An example URL is shown below, with line breaks and spaces for readability.

    https://accounts.google.com/o/oauth2/v2/auth?
     scope=https%3A//www.googleapis.com/auth/drive.metadata.readonly%20https%3A//www.googleapis.com/auth/calendar.readonly&
     access_type=offline&
     include_granted_scopes=true&
     response_type=code&
     state=state_parameter_passthrough_value&
     redirect_uri=https%3A//oauth2.example.com/code&
     client_id=client_id

    After you create the request URL, redirect the user to it.

    Google's OAuth 2.0 server authenticates the user and obtains consent from the user for your application to access the requested scopes. The response is sent back to your application using the redirect URL you specified.

    Step 3: Google prompts user for consent

    In this step, the user decides whether to grant your application the requested access. At this stage, Google displays a consent window that shows the name of your application and the Google API services that it is requesting permission to access with the user's authorization credentials and a summary of the scopes of access to be granted. The user can then consent to grant access to one or more scopes requested by your application or refuse the request.

    Your application doesn't need to do anything at this stage as it waits for the response from Google's OAuth 2.0 server indicating whether any access was granted. That response is explained in the following step.

    Ошибки

    Requests to Google's OAuth 2.0 authorization endpoint may display user-facing error messages instead of the expected authentication and authorization flows. Common error codes and suggested resolutions are listed below.

    admin_policy_enforced

    The Google Account is unable to authorize one or more scopes requested due to the policies of their Google Workspace administrator. See the Google Workspace Admin help article Control which third-party & internal apps access Google Workspace data for more information about how an administrator may restrict access to all scopes or sensitive and restricted scopes until access is explicitly granted to your OAuth client ID.

    disallowed_useragent

    The authorization endpoint is displayed inside an embedded user-agent disallowed by Google's OAuth 2.0 Policies .

    Андроид

    Android developers may encounter this error message when opening authorization requests in android.webkit.WebView . Developers should instead use Android libraries such as Google Sign-In for Android or OpenID Foundation's AppAuth for Android .

    Web developers may encounter this error when an Android app opens a general web link in an embedded user-agent and a user navigates to Google's OAuth 2.0 authorization endpoint from your site. Developers should allow general links to open in the default link handler of the operating system, which includes both Android App Links handlers or the default browser app. The Android Custom Tabs library is also a supported option.

    ios

    iOS and macOS developers may encounter this error when opening authorization requests in WKWebView . Developers should instead use iOS libraries such as Google Sign-In for iOS or OpenID Foundation's AppAuth for iOS .

    Web developers may encounter this error when an iOS or macOS app opens a general web link in an embedded user-agent and a user navigates to Google's OAuth 2.0 authorization endpoint from your site. Developers should allow general links to open in the default link handler of the operating system, which includes both Universal Links handlers or the default browser app. The SFSafariViewController library is also a supported option.

    org_internal

    The OAuth client ID in the request is part of a project limiting access to Google Accounts in a specific Google Cloud Organization . For more information about this configuration option see the User type section in the Setting up your OAuth consent screen help article.

    invalid_client

    The OAuth client secret is incorrect. Review the OAuth client configuration , including the client ID and secret used for this request.

    invalid_grant

    When refreshing an access token or using incremental authorization , the token may have expired or has been invalidated. Authenticate the user again and ask for user consent to obtain new tokens. If you are continuing to see this error, ensure that your application has been configured correctly and that you are using the correct tokens and parameters in your request. Otherwise, the user account may have been deleted or disabled.

    redirect_uri_mismatch

    The redirect_uri passed in the authorization request does not match an authorized redirect URI for the OAuth client ID. Review authorized redirect URIs in the .

    The redirect_uri parameter may refer to the OAuth out-of-band (OOB) flow that has been deprecated and is no longer supported. Refer to the migration guide to update your integration.

    invalid_request

    There was something wrong with the request you made. This could be due to a number of reasons:

    • The request was not properly formatted
    • The request was missing required parameters
    • The request uses an authorization method that Google doesn't support. Verify your OAuth integration uses a recommended integration method

    Step 4: Handle the OAuth 2.0 server response

    The OAuth 2.0 server responds to your application's access request by using the URL specified in the request.

    If the user approves the access request, then the response contains an authorization code. If the user does not approve the request, the response contains an error message. The authorization code or error message that is returned to the web server appears on the query string, as shown below:

    An error response:

    https://oauth2.example.com/auth?error=access_denied

    An authorization code response:

    https://oauth2.example.com/auth?code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7

    Sample OAuth 2.0 server response

    You can test this flow by clicking on the following sample URL, which requests read-only access to view metadata for files in your Google Drive and read-only access to view your Google Calendar events:

    https://accounts.google.com/o/oauth2/v2/auth?
     scope=https%3A//www.googleapis.com/auth/drive.metadata.readonly%20https%3A//www.googleapis.com/auth/calendar.readonly&
     access_type=offline&
     include_granted_scopes=true&
     response_type=code&
     state=state_parameter_passthrough_value&
     redirect_uri=https%3A//oauth2.example.com/code&
     client_id=client_id

    After completing the OAuth 2.0 flow, you should be redirected to http://localhost/oauth2callback , which will likely yield a 404 NOT FOUND error unless your local machine serves a file at that address. The next step provides more detail about the information returned in the URI when the user is redirected back to your application.

    Step 5: Exchange authorization code for refresh and access tokens

    After the web server receives the authorization code, it can exchange the authorization code for an access token.

    PHP

    To exchange an authorization code for an access token, use the fetchAccessTokenWithAuthCode method:

    $access_token = $client->fetchAccessTokenWithAuthCode($_GET['code']);

    Питон

    On your callback page, use the google-auth library to verify the authorization server response. Then, use the flow.fetch_token method to exchange the authorization code in that response for an access token:

    state = flask.session['state']
    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
        'client_secret.json',
        scopes=['https://www.googleapis.com/auth/drive.metadata.readonly'],
        state=state)
    flow.redirect_uri = flask.url_for('oauth2callback', _external=True)
    
    authorization_response = flask.request.url
    flow.fetch_token(authorization_response=authorization_response)
    
    # Store the credentials in the session.
    # ACTION ITEM for developers:
    #     Store user's access and refresh tokens in your data store if
    #     incorporating this code into your real app.
    credentials = flow.credentials
    flask.session['credentials'] = {
        'token': credentials.token,
        'refresh_token': credentials.refresh_token,
        'token_uri': credentials.token_uri,
        'client_id': credentials.client_id,
        'client_secret': credentials.client_secret,
        'granted_scopes': credentials.granted_scopes}

    Руби

    On your callback page, use the googleauth library to verify the authorization server response. Use the authorizer.handle_auth_callback_deferred method to save the authorization code and redirect back to the URL that originally requested authorization. This defers the exchange of the code by temporarily stashing the results in the user's session.

      target_url = Google::Auth::WebUserAuthorizer.handle_auth_callback_deferred(request)
      redirect target_url

    Node.js

    To exchange an authorization code for an access token, use the getToken method:

    const url = require('url');
    
    // Receive the callback from Google's OAuth 2.0 server.
    app.get('/oauth2callback', async (req, res) => {
      let q = url.parse(req.url, true).query;
    
      if (q.error) { // An error response e.g. error=access_denied
        console.log('Error:' + q.error);
      } else if (q.state !== req.session.state) { //check state value
        console.log('State mismatch. Possible CSRF attack');
        res.end('State mismatch. Possible CSRF attack');
      } else { // Get access and refresh tokens (if access_type is offline)
    
        let { tokens } = await oauth2Client.getToken(q.code);
        oauth2Client.setCredentials(tokens);
    });

    HTTP/REST

    To exchange an authorization code for an access token, call the https://oauth2.googleapis.com/token endpoint and set the following parameters:

    Поля
    client_id The client ID obtained from the .
    client_secret The client secret obtained from the .
    code The authorization code returned from the initial request.
    grant_type As defined in the OAuth 2.0 specification , this field's value must be set to authorization_code .
    redirect_uri One of the redirect URIs listed for your project in the for the given client_id .

    The following snippet shows a sample request:

    POST /token HTTP/1.1
    Host: oauth2.googleapis.com
    Content-Type: application/x-www-form-urlencoded
    
    code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7&
    client_id=your_client_id&
    client_secret=your_client_secret&
    redirect_uri=https%3A//oauth2.example.com/code&
    grant_type=authorization_code

    Google responds to this request by returning a JSON object that contains a short-lived access token and a refresh token. Note that the refresh token is only returned if your application set the access_type parameter to offline in the initial request to Google's authorization server .

    The response contains the following fields:

    Поля
    access_token The token that your application sends to authorize a Google API request.
    expires_in The remaining lifetime of the access token in seconds.
    refresh_token A token that you can use to obtain a new access token. Refresh tokens are valid until the user revokes access or the refresh token expires. Again, this field is only present in this response if you set the access_type parameter to offline in the initial request to Google's authorization server.
    refresh_token_expires_in The remaining lifetime of the refresh token in seconds. This value is only set when the user grants time-based access .
    scope The scopes of access granted by the access_token expressed as a list of space-delimited, case-sensitive strings.
    token_type The type of token returned. At this time, this field's value is always set to Bearer .

    The following snippet shows a sample response:

    {
      "access_token": "1/fFAGRNJru1FTz70BzhT3Zg",
      "expires_in": 3920,
      "token_type": "Bearer",
      "scope": "https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/calendar.readonly",
      "refresh_token": "1//xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI"
    }

    Ошибки

    When exchanging the authorization code for an access token you may encounter the following error instead of the expected response. Common error codes and suggested resolutions are listed below.

    invalid_grant

    The supplied authorization code is invalid or in the wrong format. Request a new code by restarting the OAuth process to prompt the user for consent again.

    Step 6: Check which scopes users granted

    When requesting multiple permissions (scopes), users may not grant your app access to all of them. Your app must verify which scopes were actually granted and gracefully handle situations where some permissions are denied, typically by disabling the features that rely on those denied scopes.

    Однако есть исключения. Google Workspace Enterprise apps with domain-wide delegation of authority , or apps marked as Trusted , bypass the granular permissions consent screen. For these apps, users won't see the granular permission consent screen. Instead, your app will either receive all requested scopes or none.

    For more detailed information, see How to handle granular permissions .

    PHP

    To check which scopes the user has granted, use the getGrantedScope() method:

    // Space-separated string of granted scopes if it exists, otherwise null.
    $granted_scopes = $client->getOAuth2Service()->getGrantedScope();
    
    // Determine which scopes user granted and build a dictionary
    $granted_scopes_dict = [
      'Drive' => str_contains($granted_scopes, Google\Service\Drive::DRIVE_METADATA_READONLY),
      'Calendar' => str_contains($granted_scopes, Google\Service\Calendar::CALENDAR_READONLY)
    ];

    Питон

    The returned credentials object has a granted_scopes property, which is a list of scopes the user has granted to your app.

    credentials = flow.credentials
    flask.session['credentials'] = {
        'token': credentials.token,
        'refresh_token': credentials.refresh_token,
        'token_uri': credentials.token_uri,
        'client_id': credentials.client_id,
        'client_secret': credentials.client_secret,
        'granted_scopes': credentials.granted_scopes}

    The following function checks which scopes the user has granted to your app.

    def check_granted_scopes(credentials):
      features = {}
      if 'https://www.googleapis.com/auth/drive.metadata.readonly' in credentials['granted_scopes']:
        features['drive'] = True
      else:
        features['drive'] = False
    
      if 'https://www.googleapis.com/auth/calendar.readonly' in credentials['granted_scopes']:
        features['calendar'] = True
      else:
        features['calendar'] = False
    
      return features

    Руби

    When requesting multiple scopes at once, check which scopes were granted through the scope property of the credentials object.

    # User authorized the request. Now, check which scopes were granted.
    if credentials.scope.include?(Google::Apis::DriveV3::AUTH_DRIVE_METADATA_READONLY)
      # User authorized read-only Drive activity permission.
      # Calling the APIs, etc
    else
      # User didn't authorize read-only Drive activity permission.
      # Update UX and application accordingly
    end
    
    # Check if user authorized Calendar read permission.
    if credentials.scope.include?(Google::Apis::CalendarV3::AUTH_CALENDAR_READONLY)
      # User authorized Calendar read permission.
      # Calling the APIs, etc.
    else
      # User didn't authorize Calendar read permission.
      # Update UX and application accordingly
    end

    Node.js

    When requesting multiple scopes at once, check which scopes were granted through the scope property of the tokens object.

    // User authorized the request. Now, check which scopes were granted.
    if (tokens.scope.includes('https://www.googleapis.com/auth/drive.metadata.readonly'))
    {
      // User authorized read-only Drive activity permission.
      // Calling the APIs, etc.
    }
    else
    {
      // User didn't authorize read-only Drive activity permission.
      // Update UX and application accordingly
    }
    
    // Check if user authorized Calendar read permission.
    if (tokens.scope.includes('https://www.googleapis.com/auth/calendar.readonly'))
    {
      // User authorized Calendar read permission.
      // Calling the APIs, etc.
    }
    else
    {
      // User didn't authorize Calendar read permission.
      // Update UX and application accordingly
    }

    HTTP/REST

    To check whether the user has granted your application access to a particular scope, exam the scope field in the access token response. The scopes of access granted by the access_token expressed as a list of space-delimited, case-sensitive strings.

    For example, the following sample access token response indicates that the user has granted your application access to the read-only Drive activity and Calendar events permissions:

      {
        "access_token": "1/fFAGRNJru1FTz70BzhT3Zg",
        "expires_in": 3920,
        "token_type": "Bearer",
        "scope": "https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/calendar.readonly",
        "refresh_token": "1//xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI"
      }

    Call Google APIs

    PHP

    Use the access token to call Google APIs by completing the following steps:

    1. If you need to apply an access token to a new Google\Client object — for example, if you stored the access token in a user session — use the setAccessToken method:
      $client->setAccessToken($access_token);
    2. Build a service object for the API that you want to call. You build a service object by providing an authorized Google\Client object to the constructor for the API you want to call. For example, to call the Drive API:
      $drive = new Google\Service\Drive($client);
    3. Make requests to the API service using the interface provided by the service object . For example, to list the files in the authenticated user's Google Drive:
      $files = $drive->files->listFiles(array());

    Питон

    After obtaining an access token, your application can use that token to authorize API requests on behalf of a given user account or service account. Use the user-specific authorization credentials to build a service object for the API that you want to call, and then use that object to make authorized API requests.

    1. Build a service object for the API that you want to call. You build a service object by calling the googleapiclient.discovery library's build method with the name and version of the API and the user credentials: For example, to call version 3 of the Drive API:
      from googleapiclient.discovery import build
      
      drive = build('drive', 'v2', credentials=credentials)
    2. Make requests to the API service using the interface provided by the service object . For example, to list the files in the authenticated user's Google Drive:
      files = drive.files().list().execute()

    Руби

    After obtaining an access token, your application can use that token to make API requests on behalf of a given user account or service account. Use the user-specific authorization credentials to build a service object for the API that you want to call, and then use that object to make authorized API requests.

    1. Build a service object for the API that you want to call. For example, to call version 3 of the Drive API:
      drive = Google::Apis::DriveV3::DriveService.new
    2. Set the credentials on the service:
      drive.authorization = credentials
    3. Make requests to the API service using the interface provided by the service object . For example, to list the files in the authenticated user's Google Drive:
      files = drive.list_files

    Alternately, authorization can be provided on a per-method basis by supplying the options parameter to a method:

    files = drive.list_files(options: { authorization: credentials })

    Node.js

    After obtaining an access token and setting it to the OAuth2 object, use the object to call Google APIs. Your application can use that token to authorize API requests on behalf of a given user account or service account. Build a service object for the API that you want to call. For example, the following code uses the Google Drive API to list filenames in the user's Drive.

    const { google } = require('googleapis');
    
    // Example of using Google Drive API to list filenames in user's Drive.
    const drive = google.drive('v3');
    drive.files.list({
      auth: oauth2Client,
      pageSize: 10,
      fields: 'nextPageToken, files(id, name)',
    }, (err1, res1) => {
      if (err1) return console.log('The API returned an error: ' + err1);
      const files = res1.data.files;
      if (files.length) {
        console.log('Files:');
        files.map((file) => {
          console.log(`${file.name} (${file.id})`);
        });
      } else {
        console.log('No files found.');
      }
    });

    HTTP/REST

    After your application obtains an access token, you can use the token to make calls to a Google API on behalf of a given user account if the scope(s) of access required by the API have been granted. To do this, include the access token in a request to the API by including either an access_token query parameter or an Authorization HTTP header Bearer value. When possible, the HTTP header is preferable, because query strings tend to be visible in server logs. In most cases you can use a client library to set up your calls to Google APIs (for example, when calling the Drive Files API ).

    You can try out all the Google APIs and view their scopes at the OAuth 2.0 Playground .

    HTTP GET examples

    A call to the drive.files endpoint (the Drive Files API) using the Authorization: Bearer HTTP header might look like the following. Note that you need to specify your own access token:

    GET /drive/v2/files HTTP/1.1
    Host: www.googleapis.com
    Authorization: Bearer access_token

    Here is a call to the same API for the authenticated user using the access_token query string parameter:

    GET https://www.googleapis.com/drive/v2/files?access_token=access_token

    curl examples

    You can test these commands with the curl command-line application. Here's an example that uses the HTTP header option (preferred):

    curl -H "Authorization: Bearer access_token" https://www.googleapis.com/drive/v2/files

    Or, alternatively, the query string parameter option:

    curl https://www.googleapis.com/drive/v2/files?access_token=access_token

    Complete example

    The following example prints a JSON-formatted list of files in a user's Google Drive after the user authenticates and gives consent for the application to access the user's Drive metadata.

    PHP

    To run this example:

    1. В API Console, add the URL of the local machine to the list of redirect URLs. For example, add http://localhost:8080 .
    2. Create a new directory and change to it. Например:
      mkdir ~/php-oauth2-example
      cd ~/php-oauth2-example
    3. Install the Google API Client Library for PHP using Composer :
      composer require google/apiclient:^2.15.0
    4. Create the files index.php and oauth2callback.php with the following content.
    5. Run the example with the PHP's built-in test web server:
      php -S localhost:8080 ~/php-oauth2-example

    index.php

    <?php
    require_once __DIR__.'/vendor/autoload.php';
    
    session_start();
    
    $client = new Google\Client();
    $client->setAuthConfig('client_secret.json');
    
    // User granted permission as an access token is in the session.
    if (isset($_SESSION['access_token']) && $_SESSION['access_token'])
    {
      $client->setAccessToken($_SESSION['access_token']);
      
      // Check if user granted Drive permission
      if ($_SESSION['granted_scopes_dict']['Drive']) {
        echo "Drive feature is enabled.";
        echo "</br>";
        $drive = new Drive($client);
        $files = array();
        $response = $drive->files->listFiles(array());
        foreach ($response->files as $file) {
            echo "File: " . $file->name . " (" . $file->id . ")";
            echo "</br>";
        }
      } else {
        echo "Drive feature is NOT enabled.";
        echo "</br>";
      }
    
       // Check if user granted Calendar permission
      if ($_SESSION['granted_scopes_dict']['Calendar']) {
        echo "Calendar feature is enabled.";
        echo "</br>";
      } else {
        echo "Calendar feature is NOT enabled.";
        echo "</br>";
      }
    }
    else
    {
      // Redirect users to outh2call.php which redirects users to Google OAuth 2.0
      $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php';
      header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
    }
    ?>

    oauth2callback.php

    <?php
    require_once __DIR__.'/vendor/autoload.php';
    
    session_start();
    
    $client = new Google\Client();
    
    // Required, call the setAuthConfig function to load authorization credentials from
    // client_secret.json file.
    $client->setAuthConfigFile('client_secret.json');
    $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST']. $_SERVER['PHP_SELF']);
    
    // Required, to set the scope value, call the addScope function.
    $client->addScope([Google\Service\Drive::DRIVE_METADATA_READONLY, Google\Service\Calendar::CALENDAR_READONLY]);
    
    // Enable incremental authorization. Recommended as a best practice.
    $client->setIncludeGrantedScopes(true);
    
    // Recommended, offline access will give you both an access and refresh token so that
    // your app can refresh the access token without user interaction.
    $client->setAccessType("offline");
    
    // Generate a URL for authorization as it doesn't contain code and error
    if (!isset($_GET['code']) && !isset($_GET['error']))
    {
      // Generate and set state value
      $state = bin2hex(random_bytes(16));
      $client->setState($state);
      $_SESSION['state'] = $state;
    
      // Generate a url that asks permissions.
      $auth_url = $client->createAuthUrl();
      header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
    }
    
    // User authorized the request and authorization code is returned to exchange access and
    // refresh tokens.
    if (isset($_GET['code']))
    {
      // Check the state value
      if (!isset($_GET['state']) || $_GET['state'] !== $_SESSION['state']) {
        die('State mismatch. Possible CSRF attack.');
      }
    
      // Get access and refresh tokens (if access_type is offline)
      $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
    
      /** Save access and refresh token to the session variables.
        * ACTION ITEM: In a production app, you likely want to save the
        *              refresh token in a secure persistent storage instead. */
      $_SESSION['access_token'] = $token;
      $_SESSION['refresh_token'] = $client->getRefreshToken();
      
      // Space-separated string of granted scopes if it exists, otherwise null.
      $granted_scopes = $client->getOAuth2Service()->getGrantedScope();
    
      // Determine which scopes user granted and build a dictionary
      $granted_scopes_dict = [
        'Drive' => str_contains($granted_scopes, Google\Service\Drive::DRIVE_METADATA_READONLY),
        'Calendar' => str_contains($granted_scopes, Google\Service\Calendar::CALENDAR_READONLY)
      ];
      $_SESSION['granted_scopes_dict'] = $granted_scopes_dict;
      
      $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/';
      header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
    }
    
    // An error response e.g. error=access_denied
    if (isset($_GET['error']))
    {
      echo "Error: ". $_GET['error'];
    }
    ?>

    Питон

    This example uses the Flask framework. It runs a web application at http://localhost:8080 that lets you test the OAuth 2.0 flow. If you go to that URL, you should see five links:

    • Call Drive API: This link points to a page that tries to execute a sample API request if users granted the permission. If necessary, it starts the authorization flow. If successful, the page displays the API response.
    • Mock page to call Calendar API: This link points to a maockpage that tries to execute a sample Calendar API request if users granted the permission. If necessary, it starts the authorization flow. If successful, the page displays the API response.
    • Test the auth flow directly: This link points to a page that tries to send the user through the authorization flow . The app requests permission to submit authorized API requests on the user's behalf.
    • Revoke current credentials: This link points to a page that revokes permissions that the user has already granted to the application.
    • Clear Flask session credentials: This link clears authorization credentials that are stored in the Flask session. This lets you see what would happen if a user who had already granted permission to your app tried to execute an API request in a new session. It also lets you see the API response your app would get if a user had revoked permissions granted to your app, and your app still tried to authorize a request with a revoked access token.
    # -*- coding: utf-8 -*-
    
    import os
    import flask
    import requests
    
    import google.oauth2.credentials
    import google_auth_oauthlib.flow
    import googleapiclient.discovery
    
    # This variable specifies the name of a file that contains the OAuth 2.0
    # information for this application, including its client_id and client_secret.
    CLIENT_SECRETS_FILE = "client_secret.json"
    
    # The OAuth 2.0 access scope allows for access to the
    # authenticated user's account and requires requests to use an SSL connection.
    SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly',
              'https://www.googleapis.com/auth/calendar.readonly']
    API_SERVICE_NAME = 'drive'
    API_VERSION = 'v2'
    
    app = flask.Flask(__name__)
    # Note: A secret key is included in the sample so that it works.
    # If you use this code in your application, replace this with a truly secret
    # key. See https://flask.palletsprojects.com/quickstart/#sessions.
    app.secret_key = 'REPLACE ME - this value is here as a placeholder.'
    
    @app.route('/')
    def index():
      return print_index_table()
    
    @app.route('/drive')
    def drive_api_request():
      if 'credentials' not in flask.session:
        return flask.redirect('authorize')
    
      features = flask.session['features']
    
      if features['drive']:
        # Load credentials from the session.
        credentials = google.oauth2.credentials.Credentials(
            **flask.session['credentials'])
    
        drive = googleapiclient.discovery.build(
            API_SERVICE_NAME, API_VERSION, credentials=credentials)
    
        files = drive.files().list().execute()
    
        # Save credentials back to session in case access token was refreshed.
        # ACTION ITEM: In a production app, you likely want to save these
        #              credentials in a persistent database instead.
        flask.session['credentials'] = credentials_to_dict(credentials)
    
        return flask.jsonify(**files)
      else:
        # User didn't authorize read-only Drive activity permission.
        # Update UX and application accordingly
        return '<p>Drive feature is not enabled.</p>'
    
    @app.route('/calendar')
        def calendar_api_request():
          if 'credentials' not in flask.session:
            return flask.redirect('authorize')
    
          features = flask.session['features']
    
          if features['calendar']:
            # User authorized Calendar read permission.
            # Calling the APIs, etc.
            return ('<p>User granted the Google Calendar read permission. '+
                    'This sample code does not include code to call Calendar</p>')
          else:
            # User didn't authorize Calendar read permission.
            # Update UX and application accordingly
            return '<p>Calendar feature is not enabled.</p>'
    
    @app.route('/authorize')
    def authorize():
      # Create flow instance to manage the OAuth 2.0 Authorization Grant Flow steps.
      flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
          CLIENT_SECRETS_FILE, scopes=SCOPES)
    
      # The URI created here must exactly match one of the authorized redirect URIs
      # for the OAuth 2.0 client, which you configured in the API Console. If this
      # value doesn't match an authorized URI, you will get a 'redirect_uri_mismatch'
      # error.
      flow.redirect_uri = flask.url_for('oauth2callback', _external=True)
    
      authorization_url, state = flow.authorization_url(
          # Enable offline access so that you can refresh an access token without
          # re-prompting the user for permission. Recommended for web server apps.
          access_type='offline',
          # Enable incremental authorization. Recommended as a best practice.
          include_granted_scopes='true')
    
      # Store the state so the callback can verify the auth server response.
      flask.session['state'] = state
    
      return flask.redirect(authorization_url)
    
    @app.route('/oauth2callback')
    def oauth2callback():
      # Specify the state when creating the flow in the callback so that it can
      # verified in the authorization server response.
      state = flask.session['state']
    
      flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
          CLIENT_SECRETS_FILE, scopes=SCOPES, state=state)
      flow.redirect_uri = flask.url_for('oauth2callback', _external=True)
    
      # Use the authorization server's response to fetch the OAuth 2.0 tokens.
      authorization_response = flask.request.url
      flow.fetch_token(authorization_response=authorization_response)
    
      # Store credentials in the session.
      # ACTION ITEM: In a production app, you likely want to save these
      #              credentials in a persistent database instead.
      credentials = flow.credentials
      
      credentials = credentials_to_dict(credentials)
      flask.session['credentials'] = credentials
    
      # Check which scopes user granted
      features = check_granted_scopes(credentials)
      flask.session['features'] = features
      return flask.redirect('/')
      
    
    @app.route('/revoke')
    def revoke():
      if 'credentials' not in flask.session:
        return ('You need to <a href="/authorize">authorize</a> before ' +
                'testing the code to revoke credentials.')
    
      credentials = google.oauth2.credentials.Credentials(
        **flask.session['credentials'])
    
      revoke = requests.post('https://oauth2.googleapis.com/revoke',
          params={'token': credentials.token},
          headers = {'content-type': 'application/x-www-form-urlencoded'})
    
      status_code = getattr(revoke, 'status_code')
      if status_code == 200:
        return('Credentials successfully revoked.' + print_index_table())
      else:
        return('An error occurred.' + print_index_table())
    
    @app.route('/clear')
    def clear_credentials():
      if 'credentials' in flask.session:
        del flask.session['credentials']
      return ('Credentials have been cleared.<br><br>' +
              print_index_table())
    
    def credentials_to_dict(credentials):
      return {'token': credentials.token,
              'refresh_token': credentials.refresh_token,
              'token_uri': credentials.token_uri,
              'client_id': credentials.client_id,
              'client_secret': credentials.client_secret,
              'granted_scopes': credentials.granted_scopes}
    
    def check_granted_scopes(credentials):
      features = {}
      if 'https://www.googleapis.com/auth/drive.metadata.readonly' in credentials['granted_scopes']:
        features['drive'] = True
      else:
        features['drive'] = False
    
      if 'https://www.googleapis.com/auth/calendar.readonly' in credentials['granted_scopes']:
        features['calendar'] = True
      else:
        features['calendar'] = False
    
      return features
    
    def print_index_table():
      return ('<table>' +
              '<tr><td><a href="/test">Test an API request</a></td>' +
              '<td>Submit an API request and see a formatted JSON response. ' +
              '    Go through the authorization flow if there are no stored ' +
              '    credentials for the user.</td></tr>' +
              '<tr><td><a href="/authorize">Test the auth flow directly</a></td>' +
              '<td>Go directly to the authorization flow. If there are stored ' +
              '    credentials, you still might not be prompted to reauthorize ' +
              '    the application.</td></tr>' +
              '<tr><td><a href="/revoke">Revoke current credentials</a></td>' +
              '<td>Revoke the access token associated with the current user ' +
              '    session. After revoking credentials, if you go to the test ' +
              '    page, you should see an <code>invalid_grant</code> error.' +
              '</td></tr>' +
              '<tr><td><a href="/clear">Clear Flask session credentials</a></td>' +
              '<td>Clear the access token currently stored in the user session. ' +
              '    After clearing the token, if you <a href="/test">test the ' +
              '    API request</a> again, you should go back to the auth flow.' +
              '</td></tr></table>')
    
    if __name__ == '__main__':
      # When running locally, disable OAuthlib's HTTPs verification.
      # ACTION ITEM for developers:
      #     When running in production *do not* leave this option enabled.
      os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
    
      # This disables the requested scopes and granted scopes check.
      # If users only grant partial request, the warning would not be thrown.
      os.environ['OAUTHLIB_RELAX_TOKEN_SCOPE'] = '1'
    
      # Specify a hostname and port that are set as a valid redirect URI
      # for your API project in the Google API Console.
      app.run('localhost', 8080, debug=True)

    Руби

    This example uses the Sinatra framework.

    require 'googleauth'
    require 'googleauth/web_user_authorizer'
    require 'googleauth/stores/redis_token_store'
    
    require 'google/apis/drive_v3'
    require 'google/apis/calendar_v3'
    
    require 'sinatra'
    
    configure do
      enable :sessions
    
      # Required, call the from_file method to retrieve the client ID from a
      # client_secret.json file.
      set :client_id, Google::Auth::ClientId.from_file('/path/to/client_secret.json')
    
      # Required, scope value
      # Access scopes for two non-Sign-In scopes: Read-only Drive activity and Google Calendar.
      scope = ['Google::Apis::DriveV3::AUTH_DRIVE_METADATA_READONLY',
               'Google::Apis::CalendarV3::AUTH_CALENDAR_READONLY']
    
      # Required, Authorizers require a storage instance to manage long term persistence of
      # access and refresh tokens.
      set :token_store, Google::Auth::Stores::RedisTokenStore.new(redis: Redis.new)
    
      # Required, indicate where the API server will redirect the user after the user completes
      # the authorization flow. The redirect URI is required. The value must exactly
      # match one of the authorized redirect URIs for the OAuth 2.0 client, which you
      # configured in the API Console. If this value doesn't match an authorized URI,
      # you will get a 'redirect_uri_mismatch' error.
      set :callback_uri, '/oauth2callback'
    
      # To use OAuth2 authentication, we need access to a CLIENT_ID, CLIENT_SECRET, AND REDIRECT_URI
      # from the client_secret.json file. To get these credentials for your application, visit
      # https://console.cloud.google.com/apis/credentials.
      set :authorizer, Google::Auth::WebUserAuthorizer.new(settings.client_id, settings.scope,
                              settings.token_store, callback_uri: settings.callback_uri)
    end
    
    get '/' do
      # NOTE: Assumes the user is already authenticated to the app
      user_id = request.session['user_id']
    
      # Fetch stored credentials for the user from the given request session.
      # nil if none present
      credentials = settings.authorizer.get_credentials(user_id, request)
    
      if credentials.nil?
        # Generate a url that asks the user to authorize requested scope(s).
        # Then, redirect user to the url.
        redirect settings.authorizer.get_authorization_url(request: request)
      end
      
      # User authorized the request. Now, check which scopes were granted.
      if credentials.scope.include?(Google::Apis::DriveV3::AUTH_DRIVE_METADATA_READONLY)
        # User authorized read-only Drive activity permission.
        # Example of using Google Drive API to list filenames in user's Drive.
        drive = Google::Apis::DriveV3::DriveService.new
        files = drive.list_files(options: { authorization: credentials })
        "<pre>#{JSON.pretty_generate(files.to_h)}</pre>"
      else
        # User didn't authorize read-only Drive activity permission.
        # Update UX and application accordingly
      end
    
      # Check if user authorized Calendar read permission.
      if credentials.scope.include?(Google::Apis::CalendarV3::AUTH_CALENDAR_READONLY)
        # User authorized Calendar read permission.
        # Calling the APIs, etc.
      else
        # User didn't authorize Calendar read permission.
        # Update UX and application accordingly
      end
    end
    
    # Receive the callback from Google's OAuth 2.0 server.
    get '/oauth2callback' do
      # Handle the result of the oauth callback. Defers the exchange of the code by
      # temporarily stashing the results in the user's session.
      target_url = Google::Auth::WebUserAuthorizer.handle_auth_callback_deferred(request)
      redirect target_url
    end

    Node.js

    To run this example:

    1. В API Console, add the URL of the local machine to the list of redirect URLs. For example, add http://localhost .
    2. Make sure you have maintenance LTS, active LTS, or current release of Node.js installed.
    3. Create a new directory and change to it. Например:
      mkdir ~/nodejs-oauth2-example
      cd ~/nodejs-oauth2-example
    4. Install the Google API Client Library for Node.js using npm :
      npm install googleapis
    5. Create the files main.js with the following content.
    6. Run the example:
      node .\main.js

    main.js

    const http = require('http');
    const https = require('https');
    const url = require('url');
    const { google } = require('googleapis');
    const crypto = require('crypto');
    const express = require('express');
    const session = require('express-session');
    
    /**
     * To use OAuth2 authentication, we need access to a CLIENT_ID, CLIENT_SECRET, AND REDIRECT_URI.
     * To get these credentials for your application, visit
     * https://console.cloud.google.com/apis/credentials.
     */
    const oauth2Client = new google.auth.OAuth2(
      YOUR_CLIENT_ID,
      YOUR_CLIENT_SECRET,
      YOUR_REDIRECT_URL
    );
    
    // Access scopes for two non-Sign-In scopes: Read-only Drive activity and Google Calendar.
    const scopes = [
      'https://www.googleapis.com/auth/drive.metadata.readonly',
      'https://www.googleapis.com/auth/calendar.readonly'
    ];
    
    /* Global variable that stores user credential in this code example.
     * ACTION ITEM for developers:
     *   Store user's refresh token in your data store if
     *   incorporating this code into your real app.
     *   For more information on handling refresh tokens,
     *   see https://github.com/googleapis/google-api-nodejs-client#handling-refresh-tokens
     */
    let userCredential = null;
    
    async function main() {
      const app = express();
    
      app.use(session({
        secret: 'your_secure_secret_key', // Replace with a strong secret
        resave: false,
        saveUninitialized: false,
      }));
    
      // Example on redirecting user to Google's OAuth 2.0 server.
      app.get('/', async (req, res) => {
        // Generate a secure random state value.
        const state = crypto.randomBytes(32).toString('hex');
        // Store state in the session
        req.session.state = state;
    
        // Generate a url that asks permissions for the Drive activity and Google Calendar scope
        const authorizationUrl = oauth2Client.generateAuthUrl({
          // 'online' (default) or 'offline' (gets refresh_token)
          access_type: 'offline',
          /** Pass in the scopes array defined above.
            * Alternatively, if only one scope is needed, you can pass a scope URL as a string */
          scope: scopes,
          // Enable incremental authorization. Recommended as a best practice.
          include_granted_scopes: true,
          // Include the state parameter to reduce the risk of CSRF attacks.
          state: state
        });
    
        res.redirect(authorizationUrl);
      });
    
      // Receive the callback from Google's OAuth 2.0 server.
      app.get('/oauth2callback', async (req, res) => {
        // Handle the OAuth 2.0 server response
        let q = url.parse(req.url, true).query;
    
        if (q.error) { // An error response e.g. error=access_denied
          console.log('Error:' + q.error);
        } else if (q.state !== req.session.state) { //check state value
          console.log('State mismatch. Possible CSRF attack');
          res.end('State mismatch. Possible CSRF attack');
        } else { // Get access and refresh tokens (if access_type is offline)
          let { tokens } = await oauth2Client.getToken(q.code);
          oauth2Client.setCredentials(tokens);
    
          /** Save credential to the global variable in case access token was refreshed.
            * ACTION ITEM: In a production app, you likely want to save the refresh token
            *              in a secure persistent database instead. */
          userCredential = tokens;
          
          // User authorized the request. Now, check which scopes were granted.
          if (tokens.scope.includes('https://www.googleapis.com/auth/drive.metadata.readonly'))
          {
            // User authorized read-only Drive activity permission.
            // Example of using Google Drive API to list filenames in user's Drive.
            const drive = google.drive('v3');
            drive.files.list({
              auth: oauth2Client,
              pageSize: 10,
              fields: 'nextPageToken, files(id, name)',
            }, (err1, res1) => {
              if (err1) return console.log('The API returned an error: ' + err1);
              const files = res1.data.files;
              if (files.length) {
                console.log('Files:');
                files.map((file) => {
                  console.log(`${file.name} (${file.id})`);
                });
              } else {
                console.log('No files found.');
              }
            });
          }
          else
          {
            // User didn't authorize read-only Drive activity permission.
            // Update UX and application accordingly
          }
    
          // Check if user authorized Calendar read permission.
          if (tokens.scope.includes('https://www.googleapis.com/auth/calendar.readonly'))
          {
            // User authorized Calendar read permission.
            // Calling the APIs, etc.
          }
          else
          {
            // User didn't authorize Calendar read permission.
            // Update UX and application accordingly
          }
        }
      });
    
      // Example on revoking a token
      app.get('/revoke', async (req, res) => {
        // Build the string for the POST request
        let postData = "token=" + userCredential.access_token;
    
        // Options for POST request to Google's OAuth 2.0 server to revoke a token
        let postOptions = {
          host: 'oauth2.googleapis.com',
          port: '443',
          path: '/revoke',
          method: 'POST',
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': Buffer.byteLength(postData)
          }
        };
    
        // Set up the request
        const postReq = https.request(postOptions, function (res) {
          res.setEncoding('utf8');
          res.on('data', d => {
            console.log('Response: ' + d);
          });
        });
    
        postReq.on('error', error => {
          console.log(error)
        });
    
        // Post the request with data
        postReq.write(postData);
        postReq.end();
      });
    
    
      const server = http.createServer(app);
      server.listen(8080);
    }
    main().catch(console.error);

    HTTP/REST

    This Python example uses the Flask framework and the Requests library to demonstrate the OAuth 2.0 web flow. We recommend using the Google API Client Library for Python for this flow. (The example in the Python tab does use the client library.)

    import json
    import flask
    import requests
    
    app = flask.Flask(__name__)
    
    # To get these credentials (CLIENT_ID CLIENT_SECRET) and for your application, visit
    # https://console.cloud.google.com/apis/credentials.
    CLIENT_ID = '123456789.apps.googleusercontent.com'
    CLIENT_SECRET = 'abc123'  # Read from a file or environmental variable in a real app
    
    # Access scopes for two non-Sign-In scopes: Read-only Drive activity and Google Calendar.
    SCOPE = 'https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/calendar.readonly'
    
    # Indicate where the API server will redirect the user after the user completes
    # the authorization flow. The redirect URI is required. The value must exactly
    # match one of the authorized redirect URIs for the OAuth 2.0 client, which you
    # configured in the API Console. If this value doesn't match an authorized URI,
    # you will get a 'redirect_uri_mismatch' error.
    REDIRECT_URI = 'http://example.com/oauth2callback'
    
    @app.route('/')
    def index():
      if 'credentials' not in flask.session:
        return flask.redirect(flask.url_for('oauth2callback'))
    
      credentials = json.loads(flask.session['credentials'])
    
      if credentials['expires_in'] <= 0:
        return flask.redirect(flask.url_for('oauth2callback'))
      else: 
        # User authorized the request. Now, check which scopes were granted.
        if 'https://www.googleapis.com/auth/drive.metadata.readonly' in credentials['scope']:
          # User authorized read-only Drive activity permission.
          # Example of using Google Drive API to list filenames in user's Drive.
          headers = {'Authorization': 'Bearer {}'.format(credentials['access_token'])}
          req_uri = 'https://www.googleapis.com/drive/v2/files'
          r = requests.get(req_uri, headers=headers).text
        else:
          # User didn't authorize read-only Drive activity permission.
          # Update UX and application accordingly
          r = 'User did not authorize Drive permission.'
    
        # Check if user authorized Calendar read permission.
        if 'https://www.googleapis.com/auth/calendar.readonly' in credentials['scope']:
          # User authorized Calendar read permission.
          # Calling the APIs, etc.
          r += 'User authorized Calendar permission.'
        else:
          # User didn't authorize Calendar read permission.
          # Update UX and application accordingly
          r += 'User did not authorize Calendar permission.'
    
      return r
    
    @app.route('/oauth2callback')
    def oauth2callback():
      if 'code' not in flask.request.args:
        state = str(uuid.uuid4())
        flask.session['state'] = state
        # Generate a url that asks permissions for the Drive activity
        # and Google Calendar scope. Then, redirect user to the url.
        auth_uri = ('https://accounts.google.com/o/oauth2/v2/auth?response_type=code'
                    '&client_id={}&redirect_uri={}&scope={}&state={}').format(CLIENT_ID, REDIRECT_URI,
                                                                              SCOPE, state)
        return flask.redirect(auth_uri)
      else:
        if 'state' not in flask.request.args or flask.request.args['state'] != flask.session['state']:
          return 'State mismatch. Possible CSRF attack.', 400
    
        auth_code = flask.request.args.get('code')
        data = {'code': auth_code,
                'client_id': CLIENT_ID,
                'client_secret': CLIENT_SECRET,
                'redirect_uri': REDIRECT_URI,
                'grant_type': 'authorization_code'}
    
        # Exchange authorization code for access and refresh tokens (if access_type is offline)
        r = requests.post('https://oauth2.googleapis.com/token', data=data)
        flask.session['credentials'] = r.text
        return flask.redirect(flask.url_for('index'))
    
    if __name__ == '__main__':
      import uuid
      app.secret_key = str(uuid.uuid4())
      app.debug = False
      app.run()

    Redirect URI validation rules

    Google applies the following validation rules to redirect URIs in order to help developers keep their applications secure. Your redirect URIs must adhere to these rules. See RFC 3986 section 3 for the definition of domain, host, path, query, scheme and userinfo, mentioned below.

    Validation rules
    Схема

    Redirect URIs must use the HTTPS scheme, not plain HTTP. Localhost URIs (including localhost IP address URIs) are exempt from this rule.

    Хозяин

    Hosts cannot be raw IP addresses. Localhost IP addresses are exempted from this rule.

    Домен
  • Host TLDs ( Top Level Domains ) must belong to the public suffix list .
  • Host domains cannot be “googleusercontent.com” .
  • Redirect URIs cannot contain URL shortener domains (eg goo.gl ) unless the app owns the domain. Furthermore, if an app that owns a shortener domain chooses to redirect to that domain, that redirect URI must either contain “/google-callback/” in its path or end with “/google-callback” .
  • Userinfo

    Redirect URIs cannot contain the userinfo subcomponent.

    Путь

    Redirect URIs cannot contain a path traversal (also called directory backtracking), which is represented by an “/..” or “\..” or their URL encoding.

    Запрос

    Redirect URIs cannot contain open redirects .

    Фрагмент

    Redirect URIs cannot contain the fragment component.

    Персонажи Redirect URIs cannot contain certain characters including:
    • Wildcard characters ( '*' )
    • Non-printable ASCII characters
    • Invalid percent encodings (any percent encoding that does not follow URL-encoding form of a percent sign followed by two hexadecimal digits)
    • Null characters (an encoded NULL character, eg, %00 , %C0%80 )

    Incremental authorization

    In the OAuth 2.0 protocol, your app requests authorization to access resources, which are identified by scopes. It is considered a best user-experience practice to request authorization for resources at the time you need them. To enable that practice, Google's authorization server supports incremental authorization. This feature lets you request scopes as they are needed and, if the user grants permission for the new scope, returns an authorization code that may be exchanged for a token containing all scopes the user has granted the project.

    For example, an app that lets people sample music tracks and create mixes might need very few resources at sign-in time, perhaps nothing more than the name of the person signing in. However, saving a completed mix would require access to their Google Drive. Most people would find it natural if they only were asked for access to their Google Drive at the time the app actually needed it.

    In this case, at sign-in time the app might request the openid and profile scopes to perform basic sign-in, and then later request the https://www.googleapis.com/auth/drive.file scope at the time of the first request to save a mix.

    To implement incremental authorization, you complete the normal flow for requesting an access token but make sure that the authorization request includes previously granted scopes. This approach allows your app to avoid having to manage multiple access tokens.

    The following rules apply to an access token obtained from an incremental authorization:

    • The token can be used to access resources corresponding to any of the scopes rolled into the new, combined authorization.
    • When you use the refresh token for the combined authorization to obtain an access token, the access token represents the combined authorization and can be used for any of the scope values included in the response.
    • The combined authorization includes all scopes that the user granted to the API project even if the grants were requested from different clients. For example, if a user granted access to one scope using an application's desktop client and then granted another scope to the same application via a mobile client, the combined authorization would include both scopes.
    • If you revoke a token that represents a combined authorization, access to all of that authorization's scopes on behalf of the associated user are revoked simultaneously.

    The language-specific code samples in Step 1: Set authorization parameters and the sample HTTP/REST redirect URL in Step 2: Redirect to Google's OAuth 2.0 server all use incremental authorization. The code samples below also show the code that you need to add to use incremental authorization.

    PHP

    $client->setIncludeGrantedScopes(true);

    Питон

    In Python, set the include_granted_scopes keyword argument to true to ensure that an authorization request includes previously granted scopes. It is very possible that include_granted_scopes will not be the only keyword argument that you set, as shown in the example below.

    authorization_url, state = flow.authorization_url(
        # Enable offline access so that you can refresh an access token without
        # re-prompting the user for permission. Recommended for web server apps.
        access_type='offline',
        # Enable incremental authorization. Recommended as a best practice.
        include_granted_scopes='true')

    Руби

    auth_client.update!(
      :additional_parameters => {"include_granted_scopes" => "true"}
    )

    Node.js

    const authorizationUrl = oauth2Client.generateAuthUrl({
      // 'online' (default) or 'offline' (gets refresh_token)
      access_type: 'offline',
      /** Pass in the scopes array defined above.
        * Alternatively, if only one scope is needed, you can pass a scope URL as a string */
      scope: scopes,
      // Enable incremental authorization. Recommended as a best practice.
      include_granted_scopes: true
    });

    HTTP/REST

    GET https://accounts.google.com/o/oauth2/v2/auth?
      client_id=your_client_id&
      response_type=code&
      state=state_parameter_passthrough_value&
      scope=https%3A//www.googleapis.com/auth/drive.metadata.readonly%20https%3A//www.googleapis.com/auth/calendar.readonly&
      redirect_uri=https%3A//oauth2.example.com/code&
      prompt=consent&
      include_granted_scopes=true

    Refreshing an access token (offline access)

    Access tokens periodically expire and become invalid credentials for a related API request. You can refresh an access token without prompting the user for permission (including when the user is not present) if you requested offline access to the scopes associated with the token.

    • If you use a Google API Client Library, the client object refreshes the access token as needed as long as you configure that object for offline access.
    • If you are not using a client library, you need to set the access_type HTTP query parameter to offline when redirecting the user to Google's OAuth 2.0 server . In that case, Google's authorization server returns a refresh token when you exchange an authorization code for an access token. Then, if the access token expires (or at any other time), you can use a refresh token to obtain a new access token.

    Requesting offline access is a requirement for any application that needs to access a Google API when the user is not present. For example, an app that performs backup services or executes actions at predetermined times needs to be able to refresh its access token when the user is not present. The default style of access is called online .

    Server-side web applications, installed applications, and devices all obtain refresh tokens during the authorization process. Refresh tokens are not typically used in client-side (JavaScript) web applications.

    PHP

    If your application needs offline access to a Google API, set the API client's access type to offline :

    $client->setAccessType("offline");

    After a user grants offline access to the requested scopes, you can continue to use the API client to access Google APIs on the user's behalf when the user is offline. The client object will refresh the access token as needed.

    Питон

    In Python, set the access_type keyword argument to offline to ensure that you will be able to refresh the access token without having to re-prompt the user for permission. It is very possible that access_type will not be the only keyword argument that you set, as shown in the example below.

    authorization_url, state = flow.authorization_url(
        # Enable offline access so that you can refresh an access token without
        # re-prompting the user for permission. Recommended for web server apps.
        access_type='offline',
        # Enable incremental authorization. Recommended as a best practice.
        include_granted_scopes='true')

    After a user grants offline access to the requested scopes, you can continue to use the API client to access Google APIs on the user's behalf when the user is offline. The client object will refresh the access token as needed.

    Руби

    If your application needs offline access to a Google API, set the API client's access type to offline :

    auth_client.update!(
      :additional_parameters => {"access_type" => "offline"}
    )

    After a user grants offline access to the requested scopes, you can continue to use the API client to access Google APIs on the user's behalf when the user is offline. The client object will refresh the access token as needed.

    Node.js

    If your application needs offline access to a Google API, set the API client's access type to offline :

    const authorizationUrl = oauth2Client.generateAuthUrl({
      // 'online' (default) or 'offline' (gets refresh_token)
      access_type: 'offline',
      /** Pass in the scopes array defined above.
        * Alternatively, if only one scope is needed, you can pass a scope URL as a string */
      scope: scopes,
      // Enable incremental authorization. Recommended as a best practice.
      include_granted_scopes: true
    });

    After a user grants offline access to the requested scopes, you can continue to use the API client to access Google APIs on the user's behalf when the user is offline. The client object will refresh the access token as needed.

    Access tokens expire. This library will automatically use a refresh token to obtain a new access token if it is about to expire. An easy way to make sure you always store the most recent tokens is to use the tokens event:

    oauth2Client.on('tokens', (tokens) => {
      if (tokens.refresh_token) {
        // store the refresh_token in your secure persistent database
        console.log(tokens.refresh_token);
      }
      console.log(tokens.access_token);
    });

    This tokens event only occurs in the first authorization, and you need to have set your access_type to offline when calling the generateAuthUrl method to receive the refresh token. If you have already given your app the requisiste permissions without setting the appropriate constraints for receiving a refresh token, you will need to re-authorize the application to receive a fresh refresh token.

    To set the refresh_token at a later time, you can use the setCredentials method:

    oauth2Client.setCredentials({
      refresh_token: `STORED_REFRESH_TOKEN`
    });

    Once the client has a refresh token, access tokens will be acquired and refreshed automatically in the next call to the API.

    HTTP/REST

    To refresh an access token, your application sends an HTTPS POST request to Google's authorization server ( https://oauth2.googleapis.com/token ) that includes the following parameters:

    Поля
    client_id The client ID obtained from the API Console.
    client_secret The client secret obtained from the API Console.
    grant_type As defined in the OAuth 2.0 specification , this field's value must be set to refresh_token .
    refresh_token The refresh token returned from the authorization code exchange.

    The following snippet shows a sample request:

    POST /token HTTP/1.1
    Host: oauth2.googleapis.com
    Content-Type: application/x-www-form-urlencoded
    
    client_id=your_client_id&
    client_secret=your_client_secret&
    refresh_token=refresh_token&
    grant_type=refresh_token

    As long as the user has not revoked the access granted to the application, the token server returns a JSON object that contains a new access token. The following snippet shows a sample response:

    {
      "access_token": "1/fFAGRNJru1FTz70BzhT3Zg",
      "expires_in": 3920,
      "scope": "https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/calendar.readonly",
      "token_type": "Bearer"
    }

    Note that there are limits on the number of refresh tokens that will be issued; one limit per client/user combination, and another per user across all clients. You should save refresh tokens in long-term storage and continue to use them as long as they remain valid. If your application requests too many refresh tokens, it may run into these limits, in which case older refresh tokens will stop working.

    Revoking a token

    In some cases a user may wish to revoke access given to an application. A user can revoke access by visiting Account Settings . See the Remove site or app access section of the Third-party sites & apps with access to your account support document for more information.

    It is also possible for an application to programmatically revoke the access given to it. Programmatic revocation is important in instances where a user unsubscribes, removes an application, or the API resources required by an app have significantly changed. In other words, part of the removal process can include an API request to ensure the permissions previously granted to the application are removed.

    PHP

    To programmatically revoke a token, call revokeToken() :

    $client->revokeToken();

    Питон

    To programmatically revoke a token, make a request to https://oauth2.googleapis.com/revoke that includes the token as a parameter and sets the Content-Type header:

    requests.post('https://oauth2.googleapis.com/revoke',
        params={'token': credentials.token},
        headers = {'content-type': 'application/x-www-form-urlencoded'})

    Руби

    To programmatically revoke a token, make an HTTP request to the oauth2.revoke endpoint:

    uri = URI('https://oauth2.googleapis.com/revoke')
    response = Net::HTTP.post_form(uri, 'token' => auth_client.access_token)

    The token can be an access token or a refresh token. If the token is an access token and it has a corresponding refresh token, the refresh token will also be revoked.

    If the revocation is successfully processed, then the status code of the response is 200 . For error conditions, a status code 400 is returned along with an error code.

    Node.js

    To programmatically revoke a token, make an HTTPS POST request to /revoke endpoint:

    const https = require('https');
    
    // Build the string for the POST request
    let postData = "token=" + userCredential.access_token;
    
    // Options for POST request to Google's OAuth 2.0 server to revoke a token
    let postOptions = {
      host: 'oauth2.googleapis.com',
      port: '443',
      path: '/revoke',
      method: 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': Buffer.byteLength(postData)
      }
    };
    
    // Set up the request
    const postReq = https.request(postOptions, function (res) {
      res.setEncoding('utf8');
      res.on('data', d => {
        console.log('Response: ' + d);
      });
    });
    
    postReq.on('error', error => {
      console.log(error)
    });
    
    // Post the request with data
    postReq.write(postData);
    postReq.end();

    The token parameter can be an access token or a refresh token. If the token is an access token and it has a corresponding refresh token, the refresh token will also be revoked.

    If the revocation is successfully processed, then the status code of the response is 200 . For error conditions, a status code 400 is returned along with an error code.

    HTTP/REST

    To programmatically revoke a token, your application makes a request to https://oauth2.googleapis.com/revoke and includes the token as a parameter:

    curl -d -X -POST --header "Content-type:application/x-www-form-urlencoded" \
            https://oauth2.googleapis.com/revoke?token={token}

    The token can be an access token or a refresh token. If the token is an access token and it has a corresponding refresh token, the refresh token will also be revoked.

    If the revocation is successfully processed, then the HTTP status code of the response is 200 . For error conditions, an HTTP status code 400 is returned along with an error code.

    Time-based access

    Time-based access allows a user to grant your app access to their data for a limited duration to complete an action. Time-based access is available in select Google products during the consent flow, giving users the option to grant access for a limited period of time. An example is the Data Portability API which enables a one-time transfer of data.

    When a user grants your application time-based access, the refresh token will expire after the specified duration. Note that refresh tokens may be invalidated earlier under specific circumstances; see these cases for details. The refresh_token_expires_in field returned in the authorization code exchange response represents the time remaining until the refresh token expires in such cases.

    Implementing Cross-Account Protection

    An additional step you should take to protect your users' accounts is implementing Cross-Account Protection by utilizing Google's Cross-Account Protection Service. This service lets you subscribe to security event notifications which provide information to your application about major changes to the user account. You can then use the information to take action depending on how you decide to respond to events.

    Some examples of the event types sent to your app by Google's Cross-Account Protection Service are:

    • https://schemas.openid.net/secevent/risc/event-type/sessions-revoked
    • https://schemas.openid.net/secevent/oauth/event-type/token-revoked
    • https://schemas.openid.net/secevent/risc/event-type/account-disabled

    See the Protect user accounts with Cross-Account Protection page for more information on how to implement Cross Account Protection and for the full list of available events.