【问题标题】:How do I fix the 403:insufficient authentication scopes error from Google Analytics User Deletion API如何修复来自 Google Analytics 用户删除 API 的 403:insufficient authentication scopes 错误
【发布时间】:2020-01-31 13:04:54
【问题描述】:

调用 Google Analytics user deletion API 时出现以下错误

错误

HttpError:https://www.googleapis.com/analytics/v3/management/accounts/5795821/entityUserLinks/%27785972698.1540375322%27?返回“权限不足:请求的身份验证范围不足。”>

代码:

import csv
from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials


def get_service(api_name, api_version, scope, key_file_location):
    credentials = ServiceAccountCredentials.from_json_keyfile_name(key_file_location,scope)      
    #Build the service object.
    service = build(api_name, api_version, credentials=credentials)
    return service


# open a list of client ids
with open('File_name.csv', 'rt') as csvfile:
    reader = csv.reader(csvfile, dialect='excel')
    reader_list = list(reader)

def delete_users(service):
    for row in reader_list:
        for row in reader_list:
            service.management().accountUserLinks().delete(
            accountId ='XXXXXX', 
            linkId = row[0]
            ).execute()

def main():
    # Define the auth scopes to request.
    scope = ['https://www.googleapis.com/auth/analytics.user.deletion']
    key_file_location = 'credentials.json'

    # Authenticate and construct service.
    service = get_service('analytics', 'v3', scope, key_file_location)
    delete_users(service)


if __name__ == '__main__':
    main()

我已经从 GCP 控制台 > 服务帐户下载了凭据文件并使用了该文件。无法理解确切的原因是什么以及如何解决它。我在哪里可以查看/定义身份验证范围?任何文档或帮助将不胜感激。

我什至尝试按照this post 发送 ViewId 代替 accountId 但同样的错误。

对同样的谷歌文档真的很失望。

【问题讨论】:

  • 您正在从 VM 实例运行此代码?
  • 我的本地机器上没有

标签: python api google-cloud-platform google-analytics google-analytics-api


【解决方案1】:

权限不足:请求的身份验证范围不足。

意味着用户无权执行您正在尝试执行的操作。

转到谷歌分析网站找到您要删除的视图所在的帐户。在帐户级别将服务帐户添加为用户。

然后它将有权访问该帐户。

【讨论】:

    【解决方案2】:

    错误提示您拥有的服务帐户凭据没有执行删除操作的正确权限。 这个article 有关于创建和应用权限到服务帐户的详细步骤和截图。此外,它还介绍了如何为您的服务帐户所在的项目启用 Google Analytics API。

    【讨论】:

      【解决方案3】:

      你打电话给Account User Links: delete

      如果这是您想要的呼叫,您需要使用“https://www.googleapis.com/auth/analytics.manage.users”范围。

      请参阅帐户用户链接删除的文档: https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtReference/management/accountUserLinks/delete#auth

      其中包含范围代码的行应该是:

      scope = ['https://www.googleapis.com/auth/analytics.manage.users']
      

      如果您想使用用户删除 API,那么您使用的是正确的范围,但您应该调用:

      service.userDeletion().userDeletionRequest().upsert(...).execute()
      

      快速注意,此方法采用用户 ID 和属性 ID 或项目 ID 和用户 ID。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-30
        • 2013-10-15
        • 2012-01-17
        • 1970-01-01
        • 2018-05-11
        • 2019-01-24
        • 2018-03-16
        • 2013-10-25
        相关资源
        最近更新 更多