How to get a list of most popular pages from Google Analytics in Python (Django)? -
I am trying to use the Google Analytics API using the code provided by my documents:
import httplib2 import running OS import apiclient.discovery import construction oauth2client.client import oauth2client.file import storage oauth2client.tools flow_from_clientsecrets CLIENT_SECRETS = os.path.join (os.path.dirname (os .path.abspath (__ file__)), 'client_secrets.json') MISSING_CLIENT_SECRETS_MESSAGE = '% s missing'% CLIENT_SECRETS flow = flow_from_clientsecrets ( '% s'% CLIENT_SECRETS, scope =' https: //www.googleapis. Com / auth / analytics.readonly ', message = MISSING_CLI ENT_SECRETS_MESSAGE,) TOKEN_FILE_NAME = os.path.join (os.path.dirname (os.path.abspath (__ file__)), 'analytics.dat') def prepare_credentials (): Storage = Storage (TOKEN_FILE_NAME) credential = storage. Get () if credentials have no or credentials.invalid: credentials = run (flow, storage) return credentials def initialize_service (): http = httplib2.Http () Credit = prepare_credentials () http = credentials.authorize (http) return Built ( 'analytics', 'v3', http = http) def get_analytics (): initialize_service () but the problem is that this code opens a browser and user analytics service Asks to allow access. Does anyone know how to get Google Analytics API (= to get that token) without Oath 2?
The methods of authorizing Google APIs are you using the mode that you can use on Google Allows access to the services, but what you really want to use in this case.
First of all ensure that the Analytics API service is enabled for the cloud project for you.
Then go and create a new service account client ID. This will give you a certificate file and service account email. This is the reason that all you need is
Here's an example of how to authenticate and illustrate the Analytics API.
import findplib2 oauth2client.client import SignedJwtAssertionCredentials # apiclient.discovery import service service email email SERVICE_ACCOUNT_EMAIL = '& lt; Some-id & gt; @ Developer.gserviceaccount.com 'service account # path to the private key file SERVICE_ACCOUNT_PKCS12_FILE_PATH =' /path/to/<publicatekey.pdf 'key, scope =' https: //www.googleapis.com/auth/analytics. Readonly ') http = httplib2.Http () http = credentials.authorize (http) creation of return (' analytics', 'v3', http = http) This is your Google Analytics account Will be accessed by the user as SERVICE_ACCOUNT_EMAIL , so you have to go into Google Analytics and this user has to provide access to their analytics data. / P> Optimized from:
Comments
Post a Comment