AWS 最近更改了此 Web 表单,再次破坏了我编写的现有取消订阅功能。他们添加了 2 个新的必填表单数据字段:checksum
和checksumFields
. 该checksum
字段是所有checksumFields
值串联的 sha256 哈希(与 a 连接的单个字符串,
)。
下面是我用python编写的取消订阅函数。
注意:我喜欢@samtoddler 的示例,它使用漂亮的汤来动态查找formid vs 硬编码它作为函数输入变量,就像我拥有的一样。
def unsubscribe_aws_mkt_emails(email,
url='https://pages.awscloud.com/index.php/leadCapture/save2',
form_id=34006,
lp_id=127906,
sub_id=6,
munchkin_id='112-TZM-766'):
'''
Unsubscribes email from AWS marketing emails via HTTPS POST
'''
sha256_hash = hashlib.sha256()
# Data fields used to calculate the payload SHA256 checksum value
checksum_fields = [
'FirstName',
'LastName',
'Email',
'Company',
'Phone',
'Country',
'preferenceCenterCategory',
'preferenceCenterGettingStarted',
'preferenceCenterOnlineInPersonEvents',
'preferenceCenterMonthlyAWSNewsletter',
'preferenceCenterTrainingandBestPracticeContent',
'preferenceCenterProductandServiceAnnoucements',
'preferenceCenterSurveys',
'PreferenceCenter_AWS_Partner_Events_Co__c',
'preferenceCenterOtherAWSCommunications',
'PreferenceCenter_Language_Preference__c',
'Title',
'Job_Role__c',
'Industry',
'Level_of_AWS_Usage__c',
]
headers = {
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
'user-agent': 'Mozilla/5.0',
}
data_dict = {
'Email': email,
'preferenceCenterCategory': 'no',
'preferenceCenterGettingStarted': 'no',
'preferenceCenterOnlineInPersonEvents': 'no',
'preferenceCenterMonthlyAWSNewsletter': 'no',
'preferenceCenterTrainingandBestPracticeContent': 'no',
'preferenceCenterProductandServiceAnnoucements': 'no',
'preferenceCenterSurveys': 'no',
'PreferenceCenter_AWS_Partner_Events_Co__c': 'no',
'preferenceCenterOtherAWSCommunications': 'no',
'Unsubscribed': 'yes',
'UnsubscribedReason': 'I already get email from another account',
'unsubscribedReasonOther': 'I already get email from another account',
'zOPEmailValidationHygiene': 'validate',
'formid': form_id,
'formVid': form_id,
'lpId': lp_id,
'subId': sub_id,
'munchkinId': munchkin_id,
'lpurl': '//pages.awscloud.com/communication-preferences.html?cr={creative}&kw={keyword}',
'_mkt_trk': f'id:{munchkin_id}&token:_mch-pages.awscloud.com-1644428507420-99548',
'_mktoReferrer': 'https://pages.awscloud.com/communication-preferences',
'checksumFields': ','.join(checksum_fields),
}
# calculated via js: f.checksum=v("sha256").update(s.join("|")).digest("hex")
# src = https://pages.awscloud.com/js/forms2/js/forms2.min.js
sha256_hash.update('|'.join([data_dict.get(v, '') for v in checksum_fields]).encode())
data_dict['checksum'] = sha256_hash.hexdigest()
data = parse.urlencode(data_dict).encode()
req = request.Request(url, data=data, headers=headers, method='POST')
resp = request.urlopen(req)