I'm creating an script, based on Google Analytics step-by-step guide from this page:
https://developers.google.com/analytics/resources/tutorials/hello-analytics-api
Authorization is done without problems, until it tries to access data. The return code is 403, and error message is:
User does not have any Google Analytics account
This message has no sense: my account has google analytics data, tracking multiple websites, and I can access it from web browser without problem. I've allowed Analytics API through Google APIs console, and API access is giving me right data.
Just add you given email (format of
71667655853644-o653rrdkq5hthsgo0otbpojoo@developer.gserviceaccount.com
)to User Managers:
Wish it helps you
The problem happens since we dont provide a "sub" argument. Unless we provide this, the call happens on behalf of that long service account email.
So just provide a sub argument, with an email which you already have given access in the report and things should work well!
I had this problem too. I fixed it by adding the email address for my service account to the Google Analytics profile I wanted it to access.
I got the email address (something like 1234567890@developer.gserviceaccount.com) for the service account by looking under the "API Access" tab in the Google APIs console.
Then, I followed Google's instructions for adding an email address to an Analytics profile. Now everything's working as expected.
Good luck!
You will also get this error if you have never logged in with the google account youre trying to authenticate with.
I had this problem too, and I found that the problem was that I had asked for too many permissions. The Developer Console says to ask for both
http://www.googleapis.com/auth/analytics
andhttp://www.googleapis.com/auth/analytics.readonly
permissions. This did not work when I was also using thesub
claim. Asub
claim instructs Google to issue an access token that operates on behalf of another user — in my case the Google account that owns the service account. I removed theanalytics
permission and stuck withanalytics.readonly
with the sub claim:The Bearer token issued allows me to make (at least some) Google Analytics queries to profiles that are owned by completely different Google accounts, but that have been shared (read-only) with my gmail user (me.example@gmail.com).
I was getting the 403 error until I changed the permissions of the email account from inside Google Analytics from 'Read & Analyze' to something else, saved it, and then changed the permissions back to 'Read & Analyze' and it worked.