├── .gitignore ├── Procfile ├── requirements.txt └── api.py /.gitignore: -------------------------------------------------------------------------------- 1 | json-key.json 2 | -------------------------------------------------------------------------------- /Procfile: -------------------------------------------------------------------------------- 1 | web: python api.py 2 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | google-api-python-client 2 | Flask 3 | -------------------------------------------------------------------------------- /api.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import os 3 | import base64 4 | from flask import Flask, jsonify, make_response 5 | from oauth2client.service_account import ServiceAccountCredentials 6 | 7 | app = Flask(__name__) 8 | 9 | # The scope for the OAuth2 request. 10 | SCOPE = 'https://www.googleapis.com/auth/analytics.readonly' 11 | 12 | # Defines a method to get an access token from the ServiceAccount object. 13 | def get_access_token(): 14 | return ServiceAccountCredentials.from_json_keyfile_name( 15 | "json-key.json", SCOPE).get_access_token().access_token 16 | 17 | @app.route("/token") 18 | def token(): 19 | resp = make_response(jsonify(**{"token": get_access_token()})) 20 | resp.headers['Access-Control-Allow-Origin'] = '*' 21 | return resp 22 | 23 | if __name__ == "__main__": 24 | with open("json-key.json", "w") as f: 25 | f.write(base64.b64decode(os.environ.get("JSON_KEY")).decode('UTF-8')) 26 | port = int(os.environ.get("PORT", 5000)) 27 | app.run(host='0.0.0.0', port=port) 28 | --------------------------------------------------------------------------------