0
votes

Getting the below error while calling the Google Analytics user deletion API

Error

HttpError: https://www.googleapis.com/analytics/v3/management/accounts/5795821/entityUserLinks/%27785972698.1540375322%27? returned "Insufficient Permission: Request had insufficient authentication scopes.">

Code:

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()

I have downloaded the credentials file from GCP console > Service accounts and using the same. Not able to understand what the exact cause is and how to resolve it. Where can I see/define the authentication scopes ? Any documentation or help would be appreciated.

I even tried sending ViewId in-place of accountId as per this post but same error.

Really disappointed with the google documentation for the same.

3
you are running this code from VM Instance?pradeep
no from my local machineanalyst045

3 Answers

1
votes

Insufficient Permission: Request had insufficient authentication scopes.

Means exactly that the user does not have permission to do what you are trying to do.

Go to the google analytics website find the account where the view you are looking to delete resides. Add the service account as a user at the account level.

It will then have access to the account.

1
votes

Error suggest that the service account credentials you have does not have the correct permission to perform delete operation. This article has detailed steps on creating and applying permissions to the service account with screenshot. Also, it describes how to enable Google Analytics API for the project where your service account lives.

0
votes

You are calling Account User Links: delete

You need to use "https://www.googleapis.com/auth/analytics.manage.users" scope if this is the call you are intending.

See the documentation for account user link delete: https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtReference/management/accountUserLinks/delete#auth

The line with the scope code in it should be:

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

If you wanted to use the User Deletion API, you are using the correct scope however you should call:

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

Quick note that the this method takes the user ID and a property ID or project id and a User ID.