Data acquisition from analytics API with Google API Client for python Part 2 Web application

Last time it was CLI, so this time it's a web application

reference https://developers.google.com/api-client-library/python/auth/web-app

Credentials require authentication of approved redirect URL スクリーンショット 2016-11-28 午後5.36.55.png

Download JSON data after creation スクリーンショット 2016-11-28 午後5.39.12.png

sample I'm using django, so I use HttpResponse Redirect

from oauth2client import client
from django.http import HttpResponseRedirect

flow = client.flow_from_clientsecrets(
    'client_secrets.json',
    scope='https://www.googleapis.com/auth/drive.metadata.readonly',
    redirect_uri='http://www.example.com/oauth2callback')

auth_uri = flow.step1_get_authorize_url()
return HttpResponseRedirect(auth_uri)

Account authentication is performed, so select any account スクリーンショット 2016-11-28 午後5.42.16.png

Since the authentication code can be obtained with the approved redirect URL, use it to obtain analytics data.

auth_code = request.GET['code']
flow = client.flow_from_clientsecrets(
    'client_secrets.json',
    scope='https://www.googleapis.com/auth/drive.metadata.readonly',
    redirect_uri='http://www.example.com/oauth2callback')
credentials = flow.step2_exchange(auth_code)
http_auth = credentials.authorize(httplib2.Http())
analytics = build('analytics', 'v4', http=http_auth, discoveryServiceUrl=self.DISCOVERY_URI)
reports = analytics.reports()
reports.batchGet(
                body={
                    'reportRequests': [
                        {
                            'viewId': self.VIEW_ID,
                            'dateRanges': [{'startDate': self.target_date, 'endDate': 'today'}],
                            "dimensions": [
                                {
                                    "name": "ga:productSku",  #The product code of the item sold.
                                }],
                            'metrics': [
                                {'expression': 'ga:itemQuantity'}  #The number of items sold in an e-commerce transaction.
                            ],
                            'pageSize': 50000,
                            'pageToken': "nextpage",
                            "orderBys":
                                [
                                    {"fieldName": "ga:itemQuantity", "sortOrder": "DESCENDING"},
                                ]
                        }]
                }
        ).execute()

Recommended Posts

Data acquisition from analytics API with Google API Client for python Part 2 Web application
Get data from analytics API with Google API Client for python
Use Google Analytics API from Python
Play with YouTube Data API v3 using Google API Python Client
[Python] Web application from 0! Hands-on (3) -API implementation-
Python: Reading JSON data from web API
[Python] Web application from 0! Hands-on (4) -Data molding-
Extract data from a web page with Python
How to use Service Account OAuth and API with Google API Client for python
Data analysis for improving POG 1 ~ Web scraping with Python ~
Web application made with Python3.4 + Django (Part.1 Environment construction)
Web API with Python + Falcon
Web application with Python + Flask ② ③
Web application with Python + Flask ④
Python: Extract file information from shared drive with Google Drive API
Python Application: Data Cleansing Part 1: Python Notation
Data acquisition using python googlemap api
Python Application: Data Handling Part 3: Data Format
[Data science basics] Data acquisition from API
Python application: data visualization part 1: basic
Python application: Data cleansing # 2: Data cleansing with DataFrame
Let's make a WEB application for phone book with flask Part 2
Let's make a WEB application for phone book with flask Part 3
Get US stock price from Python with Web API with Raspberry Pi
Let's make a WEB application for phone book with flask Part 4
Collecting information from Twitter with Python (Twitter API)
[Python] Web application from 0! Hands-on (2) -Hello World-
Google Cloud Vision API sample for python
[google-oauth] [python] Google APIs Client Library for Python
Receive textual data from mysql with python
[Spotify API] Looking back on 2020 with playlists --Part.1 Acquisition of playlist data
[Note] Get data from PostgreSQL with Python
WEB scraping with Python (for personal notes)
Python Application: Data Visualization Part 3: Various Graphs
Process Pubmed .xml data with python [Part 2]
Simple Slack API client made with Python
Retrieving food data with Amazon API (Python)
Get Google Fit API data in Python
Working with Azure CosmosDB from Python Part.2
Use Google Cloud Vision API from Python
Image acquisition from camera with Python + OpenCV
[Python] A quick web application with Bottle!
Sensor data acquisition and visualization for plant growth with Intel Edison and Python
[GCP] Procedure for creating a web application with Cloud Functions (Python + Flask)
[Python] Web application design for machine learning
Run a Python web application with Docker
Edit Slide (PowerPoint for Google) with Python (Low-cost RPA case with Google API and Python)
[Python] Web application from 0! Hands-on (0) -Environmental construction-
Copy data from Amazon S3 to Google Cloud Storage with Python (boto)
Creating Google Spreadsheet using Python / Google Data API
[For beginners] Try web scraping with Python
Publish a web application for viewing data created with Streamlit on heroku
Web application production course learned with Flask of Python Part 2 Chapter 1 ~ JSON exchange ~
Csv output from Google search with [Python]! 【Easy】
Create test data like that with Python (Part 1)
Run Google Analytics API (core v3) in python
Get stock price data with Quandl API [Python]
Wrap C with Cython for use from Python
Tweet (API 1.1) on Google App Engine for Python
~ Tips for Python beginners from Pythonista with love ① ~
[Python] Web application from 0! Hands-on (1) -Design, DB construction-