Mã thông báo của người mang được đặt trong tiêu đề Authorization của mọi yêu cầu HTTP cho hành động trong ứng dụng. Ví dụ:
POST /approve?expenseId=abc123 HTTP/1.1
Host: your-domain.com
Authorization: Bearer AbCdEf123456
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/1.0 (KHTML, like Gecko; Gmail Actions)
confirmed=Approved
Chuỗi "AbCdEf123456" trong ví dụ trước là mã thông báo uỷ quyền của người mang. Đây là mã thông báo mật mã do Google tạo. Tất cả mã thông báo của người mang được gửi cùng với các thao tác đều có trường azp (bên được uỷ quyền) được đặt thành gmail@system.gserviceaccount.com, với trường audience chỉ định miền của người gửi dưới dạng URL có dạng https://. Ví dụ: nếu email đến từ noreply@example.com, thì đối tượng là https://example.com.
Nếu bạn sử dụng mã thông báo của người mang, hãy xác minh rằng yêu cầu đó đến từ Google và dành cho miền của người gửi. Nếu không xác minh được mã thông báo, dịch vụ sẽ phản hồi yêu cầu bằng mã phản hồi HTTP 401 (Unauthorized).
Mã thông báo truy cập là một phần của tiêu chuẩn OAuth 2.0 và được áp dụng rộng rãi trên các API của Google.
Xác minh mã thông báo truy cập
Bạn nên dùng Thư viện ứng dụng API của Google nguồn mở để xác minh mã thông báo của người mang:
- Java: https://github.com/google/google-api-java-client
- Python: https://github.com/google/google-api-python-client
.NET: https://github.com/google/google-api-dotnet-client
Java
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Collections;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier;
import com.google.api.client.http.apache.ApacheHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
public class TokenVerifier {
// Bearer Tokens from Gmail Actions will always be issued to this authorized party.
private static final String GMAIL_AUTHORIZED_PARTY = "gmail@system.gserviceaccount.com";
// Intended audience of the token, based on the sender's domain
private static final String AUDIENCE = "https://example.com";
public static void main(String[] args) throws GeneralSecurityException, IOException {
// Get this value from the request's Authorization HTTP header.
// For example, for "Authorization: Bearer AbCdEf123456" use "AbCdEf123456"
String bearerToken = "AbCdEf123456";
GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(new ApacheHttpTransport(), new JacksonFactory())
.setAudience(Collections.singletonList(AUDIENCE))
.build();
GoogleIdToken idToken = verifier.verify(bearerToken);
if (idToken == null || !idToken.getPayload().getAuthorizedParty().equals(GMAIL_AUTHORIZED_PARTY)) {
System.out.println("Invalid token");
System.exit(-1);
}
// Token originates from Google and is targeted to a specific client.
System.out.println("The token is valid");
System.out.println("Token details:");
System.out.println(idToken.getPayload().toPrettyString());
}
}
Python
import sys
from oauth2client import client
# Bearer Tokens from Gmail Actions will always be issued to this authorized party.
GMAIL_AUTHORIZED_PARTY = 'gmail@system.gserviceaccount.com'
# Intended audience of the token, based on the sender's domain
AUDIENCE = 'https://example.com'
try:
# Get this value from the request's Authorization HTTP header.
# For example, for "Authorization: Bearer AbCdEf123456" use "AbCdEf123456"
bearer_token = 'AbCdEf123456'
# Verify valid token, signed by google.com, intended for a third party.
token = client.verify_id_token(bearer_token, AUDIENCE)
print('Token details: %s' % token)
if token['azp'] != GMAIL_AUTHORIZED_PARTY:
sys.exit('Invalid authorized party')
except:
sys.exit('Invalid token')
# Token originates from Google and is targeted to a specific client.
print('The token is valid')