Skip to main content
 首页 » 编程设计

Python Google Analytics OAuth 服务帐户和 2LO; "sub"参数和域范围委托(delegate)

2025年04月02日53rubylouvre

我在一些报告中阅读了一些关于使用 sub 参数的评论(这就是我的理解方式),当与凭证对象一起传递给带有服务的 2LO 的谷歌分析时accounts,可以用来代替直接通过管理控制台手动授权帐户,如下图所示。

Analytics Google API Error 403: "User does not have any Google Analytics Account"

我认为如果我通过 'sub': 'superuseremail@account.com' 它将取代在 GA 中手动设置帐户权限,我的理解是否错误?

更重要的是,我通过帐户权限获得了访问 Google Analytics 帐户的权限(这样我就没有 super 用户权限,也没有访问 super 用户电子邮件的权限),我通过了这个sub 参数使用该电子邮件。我在尝试这条路线时收到 403 错误。

简而言之:

访问我正在收集的服务帐户的唯一方法是通过手动添加服务帐户电子邮件访问 super 用户电子邮件和管理帐户权限,或者传递 sub 参数与 credentials 对象有一个 sub 电子邮件是正确的 super 用户电子邮件?

请您参考如下方法:

TLDR:不!,Google Analytics 不是 G-Suite产品,并且服务帐户无权在未经直接授权的情况下访问其他人的 G-Suite 帐户。

如果您仔细查看 source code for the method有问题:

def create_delegated(self, sub): 
    """Create credentials that act as domain-wide delegation of authority. 
    Use the ``sub`` parameter as the subject to delegate on behalf of 
    that user. 
    For example:: 
      >>> account_sub = 'foo@email.com' 
      >>> delegate_creds = creds.create_delegated(account_sub) 
    Args: 
        sub: string, An email address that this service account will 
             act on behalf of (via domain-wide delegation). 
    Returns: 
        ServiceAccountCredentials, a copy of the current service account 
        updated to act on behalf of ``sub``. 
    """ 
    new_kwargs = dict(self._kwargs) 
    ... 

您会看到该服务帐户将通过全域委托(delegate)代表用户。

那么你的问题的答案是“什么是 Domain wide delegation ?”。

在企业应用程序中,您可能希望以编程方式访问用户的数据,而无需用户手动授权。在 G-Suite 域中,域管理员可以授予第三方应用程序在域范围内访问其用户数据的权限——这称为域范围授权。