【问题标题】:Google Analytics Management API - Insert method - Insufficient permissions HTTP 403Google Analytics Management API - 插入方法 - 权限不足 HTTP 403
【发布时间】:2015-10-15 18:23:30
【问题描述】:

我正在尝试通过 API 将用户添加到我的 Google Analytics(分析)帐户,但代码会产生以下错误:

googleapiclient.errors.HttpError:https://www.googleapis.com/analytics/v3/management/accounts/**accountID**/entityUserLinks?alt=json 返回“权限不足”>

我拥有此帐户的管理员权限 - 管理用户。我可以通过 Google Analytics 界面添加或删除用户,但不能通过 API。我还以用户身份将服务帐户电子邮件添加到 GA。范围设置为 analytics.manage.users

这是我在 add_user 函数中使用的代码 sn-p,它与 API 文档中提供的代码相同。

def add_user(服务):

尝试:
service.management().accountUserLinks().insert( accountId='XXXXX',
身体={
“权限”:{
“本地”:[
'编辑',
]
},
'userRef': {
“电子邮件”:“ABC.DEF@gmail.com”
}
}
).execute()

除了 TypeError,错误:
# 处理构造查询中的错误。
print '构造查询时出错:%s' % error

返回无

我们将不胜感激。谢谢!!

【问题讨论】:

  • 您使用的是服务帐号吗?您能否为您的问题添加完整的错误响应?
  • 嗨,马特。谢谢你。我能够通过它(终于!)。 - 问题是我应该使用已安装的应用程序时使用服务帐户。

标签: api python-2.7 google-analytics insert http-error


【解决方案1】:

问题是我使用的是服务帐户,而我本应使用已安装的应用程序。我不需要服务帐户,因为我可以使用自己的凭据进行访问。这对我有用!

【讨论】:

    【解决方案2】:

    还请记住,您必须指定要使用的范围,这里的示例(使用 Google 稍作改动的示例)默认定义了两个不允许插入用户的范围(因为它们都提供只读权限) 并会导致“错误 403 禁止”尝试这样做。

    所需的范围在下面的代码中给出:

    from apiclient.discovery import build
    from googleapiclient.errors import HttpError
    from oauth2client.service_account import ServiceAccountCredentials
    
    
    def get_service(api_name, api_version, scopes, key_file_location):
        """Get a service that communicates to a Google API.
    
        Args:
            api_name: The name of the api to connect to.
            api_version: The api version to connect to.
            scopes: A list auth scopes to authorize for the application.
            key_file_location: The path to a valid service account JSON key file.
    
        Returns:
            A service that is connected to the specified API.
        """
    
        credentials = ServiceAccountCredentials.from_json_keyfile_name(
                key_file_location, scopes=scopes)
    
        # Build the service object.
        service = build(api_name, api_version, credentials=credentials)
    
        return service
    
    
    def get_first_profile_id(service):
        # Use the Analytics service object to get the first profile id.
    
        # Get a list of all Google Analytics accounts for this user
        accounts = service.management().accounts().list().execute()
    
        if accounts.get('items'):
            # Get the first Google Analytics account.
            account = accounts.get('items')[0].get('id')
    
            # Do something, e.g. get account users & insert new ones 
            # ...
    
    
    def main():
        # Define the auth scopes to request.
    
        # Add here
        # https://www.googleapis.com/auth/analytics.manage.users
        # to be able to insert users as well:
        scopes = [
            'https://www.googleapis.com/auth/analytics.readonly',
            'https://www.googleapis.com/auth/analytics.manage.users.readonly',
        ]
        key_file_location = 'my_key_file.json'
    
        # Authenticate and construct service.
        service = get_service(
                api_name='analytics',
                api_version='v3',
                scopes=scopes,
                key_file_location=key_file_location)
    
        profile_id = get_first_profile_id(service)
        print_results(get_results(service, profile_id))
    
    
    if __name__ == '__main__':
        main()
    

    问候,

    HerrB92

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-01
      • 2021-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-04
      • 2018-11-08
      相关资源
      最近更新 更多