[英]How do I access the components of a private RSA key in Python's cryptography module?
[英]How do I sign an AMP update-cache request with Python's cryptography package?
這是確定更新緩存請求的路徑和查詢參數的核心簽名邏輯。 在這里,域是您網站的實際域,而不是特殊的 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&_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}&_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.