簡體   English   中英

如何使用 Python 的密碼學 package 簽署 AMP 更新緩存請求?

[英]How do I sign an AMP update-cache request with Python's cryptography package?

如何使用 Python 的密碼學 package 為 AMP 的更新緩存 API簽署 URL?

這是確定更新緩存請求的路徑和查詢參數的核心簽名邏輯。 在這里,域是您網站的實際域,而不是特殊的 AMP 子域。

from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives import serialization

def sign_amp_update_cache_url(private_key: bytes, domain: str, url: str) -> str:
    private_key = serialization.load_pem_private_key(private_key, password=None)  # or whatever your key's password is
    message = f"/update-cache/c/s/{domain}{url}?amp_action=flush&amp_ts={int(time.time())}"
    binary_signature = private_key.sign(
        message.encode("UTF-8"),
        padding.PKCS1v15(),
        hashes.SHA256()
    )
    encoded_signature = base64.b64encode(binary_signature, altchars=b"-_").replace(b"=", b"").decode("UTF-8")
    signed_url = f"{message}&amp_url_signature={encoded_signature}"
    return signed_url

To finish preparing the request, you must compute the AMP cache subdomain for your domain , get the updateCacheApiDomainSuffix from caches.json , and concatenate the AMP cache subdomain, updateCacheApiDomainSuffix, and signed AMP update cache URL from the function above. 以下是 Google 的有關該過程的文檔,其中包含緩存 URL 應該是什么樣子的示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM