Google Analytics API forces browser open when authenticating in Python

末鹿安然 提交于 2020-01-03 17:10:09

问题


I'm attempting to query data from the Google Analytics API. I've setup my credentials, and I am able to get the following code to run on my Windows local machine. However, when I run from the Ubuntu command line, it forces an SSL browser window to open at the following step:

service = initialize_service()

All code is below:

import httplib2
from apiclient.discovery import build
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import run
import sys
from apiclient.errors import HttpError
from oauth2client.client import AccessTokenRefreshError

CLIENT_SECRETS = 'C:\Users\mds78\Documents\code\google\client_secrets.json'
MISSING_CLIENT_SECRETS_MESSAGE = '%s is missing' % CLIENT_SECRETS

FLOW = flow_from_clientsecrets(CLIENT_SECRETS, scope='https://www.googleapis.com/auth/analytics.readonly', message=MISSING_CLIENT_SECRETS_MESSAGE)

TOKEN_FILE_NAME = 'analytics.dat'

def prepare_credentials():
    storage = Storage(TOKEN_FILE_NAME)
    credentials = storage.get()
    if credentials is None or credentials.invalid:
        credentials = run(FLOW, storage)
    return credentials

def initialize_service():
    http = httplib2.Http()
    credentials = prepare_credentials()
    http = credentials.authorize(http)
    return build('analytics', 'v3', http=http)

service = initialize_service()

来源:https://stackoverflow.com/questions/14251138/google-analytics-api-forces-browser-open-when-authenticating-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!