I want to query the Google Analytics API using Python to periodically download data from my Analytics account and store data in a local database. I am basically following the steps as given in the basic tutorial. I am using the Google client API library for Python in this process.
My script is working fine so far when I am running it on my local dev machine (Mac). When I start the script, my browser opens and I am prompted to grant access to my Analytics data from the app. Afterwards I can run my script as often as I want and get access to my data.
On my server (Ubuntu, only terminal available), the w3m
browser opens, but I cannot access my Google account from there. I can only quit w3m
and kill the program with Ctrl-C
. There is an error message like:
Your browser has been opened to visit:
https://accounts.google.com/o/oauth2/auth?scope=some_long_url&access_type=offline
If your browser is on a different machine then exit and re-run this application with the command-line parameter
--noauth_local_webserver
However when I run my script with the parameter --noauth_local_webserver
, I get the same results - w3m
opens and I cannot authenticate.
How can I get the --noauth_local_webserver
to work? I there another way to authenticate without a local browser on the same machine?
you can also use GA as a service API:https://developers.google.com/analytics/devguides/reporting/core/v3/quickstart/service-py this works perfectly fine. Just remmeber to convert the p12 to an unencryptet PEM file using openssl $openssl pkcs12 -in client_secrets.p12 -nodes -nocerts > client_secrets.pem the import password is printed out when you download the P12 from google developer's console
I ran into the same issue and managed to solve it by SSHing into my server. Example:
I then ran my script through SSH. When I was presented with the URL (https://accounts.google.com/o/oauth2/auth?scope=some_long_url&access_type=offline), I copied and pasted into the browser on my machine to complete the authentication flow.
When you use
FLAGS = gflags.FLAGS
, you actually need to pass the command-line arguments toFLAGS
(this may or may not have tripped me up as well :) ). See here for an Analytics-centric example of how to do it (code below as links tend to go away after a while). General idea is thatargv
arguments are passed into theFLAGS
variable, which then become available to other modules.Also, turns out that we aren't alone! You can track this bug to see when this will get added the documentation.