I'm writing a Ruby app that accesses the Google Analytics API to pull down some experiment information.
The app connects and authenticates using a Google Service Account via the following function:
def connect ... @@client = Google::APIClient.new(:application_name => 'My Service App', :application_version => '1.0.0') key_file = Rails.root.join('config', 'privatekey.p12').to_s key_secret = 'somesecret' key = Google::APIClient::PKCS12.load_key(key_file, key_secret) asserter = Google::APIClient::JWTAsserter.new( SECRETS[:google_service_account_email], ['https://www.googleapis.com/auth/yt-analytics.readonly', 'https://www.googleapis.com/auth/analytics.readonly' ], key ) @@client.authorization = asserter.authorize() ... end
...which authenticates and discovers both APIs without issue.
Using the client against the YouTube Analytics API works without issue. Using the same exact account to access the Analytics API via...
response = @@client.execute({ # 'analytics is the API object retrieved via discover_api() :api_method => analytics.management.experiments.list, :parameters => { 'accountId' => 'AAAAAAAA', 'profileId' => 'PPPPPPPP', 'webPropertyId' => 'UA-WWWWWWWW-#' } })
Results in a 403 error response:
{"domain":"global","reason":"insufficientPermissions","message":"User does not have sufficient permissions for this account."}
In regards to authorization, I have double-checked the account [email protected]:
Given that the service account can access at least one API (YouTube Analytics), and the associated account ([email protected]) can access the Analytics web interface, there seems to be something wrong with the service account accessing the Analytics API in particular.
Any ideas?
Similar topics:
Sign in to Google Analytics. Click Admin, and navigate to the desired account/property/view. In the Account, Property, or View column (depending upon whether you want to add users at the account, property, or view level), click Access Management. In the Account permissions list, click +, then click Add users.
To get started, you'll need to sign into your Google Analytics account. Then, click the Admin option at the bottom of your Analytics panel on the left. Now, you'll see the 3 levels of access (i.e. Account, Property, and View). And, you'll also find the User Management option below all 3 access levels.
Longer answer/explanation — Apparently this error seems to be caused by a bug in Google Analytics website (as of Nov 2020) which occurs whenever you are logged in to Google Analytics with a different Google account than the default, first account, and the default account not having access to your Google Analytics ...
The Google Analytics Reporting API v4 provides programmatic methods to access report data in Google Analytics (Universal Analytics properties only). With the Google Analytics Reporting API, you can: Build custom dashboards to display Google Analytics data. Automate complex reporting tasks to save time.
Make sure you give the service account email (something like [email protected]) permissions to read/write from your GA view.
Admin > View > User Management > "Add permissions for:"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With