简体   繁体   English

用于取消订阅 AWS Organizations 成员账户中的营销电子邮件的 SDK

[英]SDK to unsubscribe from marketing emails in AWS Organizations member accounts

I have an AWS Organization and I create member accounts for every new project I make.我有一个 AWS 组织,并为我创建的每个新项目创建成员账户。 Since I have control over all of the accounts I use the same email account for all of those, using the account-name+project-name@gmail.com pattern.由于我可以控制所有帐户,因此我对所有这些帐户使用相同的电子邮件帐户,使用 account-name+project-name@gmail.com 模式。 This means that I get the same marketing email for every new account I create.这意味着我为我创建的每个新帐户都会收到相同的营销电子邮件。 I know I can unsubscribe manually, but since I create the member accounts through the CLI I was wondering if there is a way to automatically unsubscribe (or avoid being subscribed) through the SDK.我知道我可以手动取消订阅,但由于我通过 CLI 创建了成员帐户,我想知道是否有办法通过 SDK 自动取消订阅(或避免被订阅)。

I've looked in the AWS Organizations SDK documentation , particularly around create-account but haven't found anything relevant.我查看了AWS Organizations SDK 文档,尤其是在create-account方面,但没有发现任何相关内容。

apparently there is no solution from AWS for this.显然AWS对此没有解决方案。 The only place I found is this .我找到的唯一地方就是这个 Which involves manual intervention.其中涉及人工干预。

Doing this on Organization ID would be a good option.在组织 ID 上执行此操作将是一个不错的选择。

meanwhile, I wrote this as a workaround.同时,我写了这个作为一种解决方法。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
GET_URL = 'https://pages.awscloud.com/communication-preferences'
POST_URL = 'https://pages.awscloud.com/index.php/leadCapture/save2'
s = requests.Session()
def fetch(url, data=None):
    if data is None:
        return s.get(url).content
    return s.post(url, data=data).content
def get_form_id():
    forms = BeautifulSoup(fetch(GET_URL), 'html.parser').findAll('form')
    for form in forms:
        fields = form.findAll('input')
        for field in fields:
            if field.get('name') == 'formid':
                return field['value']
email_id = 'testing@example.com'
formid = get_form_id()
form_data = {'Email': email_id, 'Unsubscribed': 'yes', 'formid': formid, 'formVid': formid}
r = fetch(POST_URL, data=form_data)
print(r)

AWS recently changed this web form again breaking things for the existing unsubscribe function I had written. AWS 最近更改了此 Web 表单,再次破坏了我编写的现有取消订阅功能。 They added 2 new required form data fields: checksum and checksumFields .他们添加了 2 个新的必填表单数据字段: checksumchecksumFields和字段。 The checksum field is a sha256 hash of a concatenation of all checksumFields values (single string joined with a , ). checksum字段是所有checksumFields和字段值串联的 sha256 散列(单个字符串与,连接)。

Below is my unsubscribe function written in python.下面是我用python编写的取消订阅函数。
NOTE: I like @samtoddler's example using beautiful soup to dynamically lookup the formid vs hard-coding it in as a function input var like I have.注意:我喜欢@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)

While I was looking into this problem, I stumbled across a different endpoint that was buried in the HTML of the unsubscribe page.在我研究这个问题时,我偶然发现了一个不同的端点,它隐藏在取消订阅页面的 HTML 中。 It seems like it was once used for browsers with JavaScript disabled (although the page just redirects in that situation, so it's never actually used).似乎它曾经用于禁用 JavaScript 的浏览器(尽管页面只是在这种情况下重定向,所以它从未真正使用过)。

This endpoint works exactly the same way as it does in @samtoddler's answer, but the endpoint is https://pages.awscloud.com/index.php/leadCapture/save3 .该端点的工作方式与@samtoddler 的答案完全相同,但端点是https://pages.awscloud.com/index.php/leadCapture/save3 I've tested submitting a POST request to that endpoint with the following form data, and it seemed to work:我已经测试了使用以下表单数据向该端点提交 POST 请求,它似乎有效:

{
  "email": "test@gmail.com",
  "Unsubscribed": "yes",
  "formid": "34006",
  "formVid": "34006",
  "munchkinId": "112-TZM-766"
}

Wouldn't be a bad idea to use beautiful soup or something similar to get the form ID and Munchkin ID dynamically, but they haven't changed in 2 years so I feel fairly safe hard-coding them.使用漂亮的汤或类似的东西来动态获取表单 ID 和 Munchkin ID 并不是一个坏主意,但它们在 2 年内没有改变,所以我觉得对它们进行硬编码相当安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在使用 AWS Organizations 从 aws-cli 创建的账户之间切换 - How to switch between accounts created using AWS Organizations from aws-cli 从 AWS 组织中的主账户中运行的 AWS lambda function 检索子账户的角色 ARN - Retrieve Role ARNs of sub accounts from an AWS lambda function running in the master account, in AWS organizations 如何使用PHP SDK在AWS SES发送的电子邮件中实现列表取消订阅标头 - How to implement List-Unsubscribe header in emails sent by AWS SES with the PHP SDK 您可以从成员账户创建 AWS 账户吗? - Can you create AWS accounts from member accounts? 支持AWS会员账户 - Support for AWS member accounts 使用 AWS Organizations API 绕过暂停的 AWS 账户 - To by pass the SUSPENDED AWS accounts using AWS Organizations API AWS Organizations“您已超出允许的 AWS 账户数量” - AWS Organizations "You have exceeded the allowed number of AWS accounts" 在网站的多个 AWS 账户(在不同的组织中)之间轻松切换 - Easily switch between multiple AWS accounts (in separate organizations) in the website 使用适用于Java的AWS开发工具包在AWS Organizations中创建帐户 - Creating Account in the AWS Organizations using AWS SDK for Java 节点:来自不同账户的AWS S3 SDK同步 - Node: AWS S3 sdk sync from different accounts
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM