我正在尝试从 Google Analytics API 查询数据。我已经设置了我的凭据,并且能够在我的 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()