在Python中进行身份验证时,Google AnalyticsAPI会强制浏览器打开

时间:2013-01-10 05:03:23

标签: python api google-analytics ubuntu-12.04

我正在尝试从Google AnalyticsAPI查询数据。我已经设置了我的凭据,并且我能够在Windows本地计算机上运行以下代码。但是,当我从Ubuntu命令行运行时,它强制在以下步骤打开SSL浏览器窗口:

service = initialize_service()

所有代码如下:

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()

0 个答案:

没有答案