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.