Google Analytics and Python - Authorization wall - python

I have a simple need. Query the GA data of my company, wrangle then and seed them in a DB.
I wanted to write a python script to do so an and found out google
Hello Analytics API: Python quickstart for service accounts.
I follow every step but the google console seems pretty confusing for me.
I ended up having a .json file as asked but I'm getting this error with google script : googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/analytics/v3/management/accounts?alt=json returned "User does not have any Google Analytics account.">
Can someone lead me how to get the right authorization to make it work ?
EDIT : Just find out this tutorial... what's the differents between the 2 ?

User does not have any Google Analytics account.
Means that the user you are using the authenticate your application with does not have access to the google analytics account you are trying to request data from. Make sure that you are requesting a profile_id that the user has access to.
As you are using a service account i suspect that you forgot this step in the tutorial
You ned to give your service account access before its going to be able to access the data.

Related

How to connect Google Console Project with Google Analytics account

I want to create Python project to get data from Google Analytics from a certain page.
I have created new project in Google Console (console.google.developers.com) and got my OAuth 2.0 credentians in JSON format. I'm loosely following this tutorial: Python QuickStart.
I already got redirected to OAuth and selected my account but the script is getting
"User does not have sufficient permissions for this profile".
Will adding access to my user in Google Analytics be enough?
And how to run this project from shell on a remote server? I will not have the ability to just open browser and select Google account in CLI...
"User does not have sufficient permissions for this profile".
The user you are authencating with does not have access to the google analytics account you are trying to access. this would be the profile id that you are using in your code.
make sure that you are authencting your appliction using the same user you are using to log into google analytics
double check the profile id that you are using in your code.

Automate report generation using Python, what kind of Credentials do I need

I have an Google Analytics Account that I want to automate some custom reports from, but I have some problems understanding what kind of Credentials I need. Most of the tutorials I have seen says I need to use OAuth client ID but the google developers console site says I need a Servide Account key.
What is the difference between the two? Using another Analytics Account I tried to setup a OAuth connection, and it worked, but I now got unsure about what kind of key I should use.
What I want to do is to just have a Python script set up to run at some times, and then to get the data I want to query for. The data is just the same as the one I can get from logging into the Google Analytics UI, so there is no need for any users to consent to giving me access to any personal data or what ever else the Consent form should be used for.
Can someone explain what the difference is between the two Credentials and what one would be the correct one to use for my project?
Both Service accounts and OAuth2 are used to access private user data. Private data is data that is accessible only by logging in. My posts on Google+ are public anyone can see them. The information in my Google Analytics is private owned by me only I can see it and those I grant access to it.
With Oauth2 access is granted at run time. The first time an application is run the user will be asked if your application can access their data. If the user accepts and grants your application access you will be given a refresh token. This refresh token can be then used to get an access token which is used to access the private user data. Access tokens are only good for about an hour. After the hour is up you use the refresh token to get access again. That's why I say access is granted at runtime. You only have to ask the user for access once to get the refresh token though.
Service accounts on the other hand are pre authenticated. Service accounts are like dummy users they have their own google drive account and google calendar account. Because if this it is possible to shire data with them like you would any other user. You take the service account email address and add it as a user under the admin section of google analytics at the ACCOUNT level it must be the ACCOUNT level. Then using the service account in your code, you will be able to access the data for that Google Analytics account without requesting authentication from a user the first time.
Service accounts are most often used by developers to grant others access to the data owned by the developer. Oauth2 on the other hand would be used to access data of your customers for whos accounts you the developer does not personally have access to.
Technically speaking you can use either for your project as long as you store the refresh token you could technically use Oauth2 for your project. However I would not recommend it refresh tokens can expire under certain circumstances, which I will not go into.
I would recommend using a service account in your case it will be much easer for you to administrate as you will only need to set it up once.
My tutorials on the subject:
Google Developer console service account
Google Developer Console Oauth2 credentials

How do I access my google plus content and the things that have been shared with me via the API

I wish to get a listing of all my posts as well as of any and all shares sent to me on my Google+ page, I want to do that from a python script that has no html or other front end.However I am confused as to how I can get access to the content.
According to the google developers site I can not get an OAuth token for google plus if there isn't a graphical front end,, but I just want to get to my own stuff and do a bit of parsing.
Surely there must be a way to do that?
There is a Github repo that can help you
Using the Google+ Sign-In button to get an OAuth 2.0 refresh token.
Exchanging the refresh token for an access token.
Making Google+ API requests with the access token, including getting
a list of people that the user has circled.
Disconnecting the app from the user's Google account and revoking
tokens.

Simple Model for Accessing Multiple Google Analytics Accounts

I am looking for some clarification on how the Google OAuth 2.0 architecture handles the management of multiple Google Analytics accounts.
The scenario is straightforward: I am developing an analytics tool that hooks into a Google Analytics account and generates a report. I have a service account set up and verified and working with my own analytics account. My question is how to elegantly expand this so that incoming users can simply share permissions to their Google Analytics accounts with me, I can collect their data and pull up a report for them?
The confusion comes from the service account/OAuth 2.0 documentation. It seems that simplest thing I can do is create a client id and credentials, have users click a button, collect an authorization code and use it to generate an access and refresh token, then store the refresh token for later use. However, there also appears to be a means of using a service account that has permissions granted to is and simply changing the account id that I embed in the analytics query. There's also documentation of using Flow and Credential objects to perform authentication and access procedures.
I am mixing all of these up and can't get my bearings on what actually does what and with what limitations. Any help outlining the use cases and proper usages of these systems would be greatly appreciated. Thanks!

Use oauth to connect GAE python application to google's drive/docs/spreadsheet

I'd like to allow my Google App Engine application to connect to a clients Google Spreadsheet on their Google Drive. I've spent the last two and a half days trying, and I've gotten nowhere. Half of the GAE Python documentation seems to be out of date. For example some of the examples have webapp, and they don't work until I change them to webapp2, but that doesn't always work.
I created a OAuth2.0 thing (not really sure what to call it) at:
https://code.google.com/apis/console/
So now I have a Client ID and Client Secret, but one doc talked about a CONSUMER_KEY and CONSUMER_SECRET. So are they the same or?
I followed the following doc to use OAuth to read my tasks (I know it's a different API), but I couldn't figure out step/Task 3. I'm not sure if I have all of the files/librarys to connect using OAuth. I have the gdata-2.0.17 files, and I know how to connect to the drive and spreadsheets by hard coding the login credentials, but no user is going to give me their credentials.
I don't normally ask for code, or even help, but I'm completely lost with this whole OAuth API/Service.
If someone could post some sample code that uses OAuth 2.0 and webapp2, and that you have tested, that would be awesome.
If someone could link me to a sample GAE Python project that can authenticate with Google's servers and allow it to connect to the users spreadsheets using OAuth 2.0 and webapp2, I'd be over the moon.
A complete example application using Google Drive from GAE is explained in this article.
See Retrieving Authenticated Google Data Feeds with Google App Engine (Python) if you need to access the spreadsheet content.
The samples in this article is using Google Document List API but it could be easily adapted to use spreadsheets scope and spreadsheet client or service.
If you only need to list the files, I would recommend using Drive like #SebastionKreft suggested

Categories

Resources