2014-10-09 326 views
0

我試圖按照here給出的Google Analytics API教程。我一直在跟着它。下面是我的文件谷歌分析API - 錯誤:redirect_uri_mismatch

的client_secrets.json

{ 
    "installed": { 
    "client_id": "xxxxxxxxxxxxxxx.apps.googleusercontent.com", 
    "client_secret": "xxxxxxxxxxxxxxxxx", 
    "redirect_uris": ["http://127.0.0.1:8000/oauth2callback/"], 
    "auth_uri": "https://accounts.google.com/o/oauth2/auth", 
    "token_uri": "https://accounts.google.com/o/oauth2/token" 
    } 
} 

hello_analytics_api_v3_auth.py

#!/usr/bin/python 

import httplib2 

from apiclient.discovery import build 

from oauth2client.client import flow_from_clientsecrets 
from oauth2client.file import Storage 
from oauth2client.tools import run 

CLIENT_SECRETS = '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() 

    #Get stored credentials or run the Auth Flow if none are found 
    credentials = prepare_credentials() 
    http = credentials.authorize(http) 

    #Construct and return the authorized Analytics Service Object 
    return build('analytics', 'v3', http=http) 

hello_analytics_api_v3.py

#!/usr/bin/python 
# -*- coding: utf-8 -*- 

import sys 

# import the Auth Helper class 
import hello_analytics_api_v3_auth 

from apiclient.errors import HttpError 
from oauth2client.client import AccessTokenRefreshError 


def main(argv): 
    # Initialize the Analytics Service Object 
    service = hello_analytics_api_v3_auth.initialize_service() 

    try: 
    # Query APIs, print results 
    profile_id = get_first_profile_id(service) 

    if profile_id: 
     results = get_results(service, profile_id) 
     print_results(results) 

    except TypeError, error: 
    # Handle errors in constructing a query. 
    print ('There was an error in constructing your query : %s' % error) 

    except HttpError, error: 
    # Handle API errors. 
    print ('Arg, there was an API error : %s : %s' % 
      (error.resp.status, error._get_reason())) 

    except AccessTokenRefreshError: 
    # Handle Auth errors. 
    print ('The credentials have been revoked or expired, please re-run ' 
      'the application to re-authorize') 


def get_first_profile_id(service): 
    # Get a list of all Google Analytics accounts for this user 
    accounts = service.management().accounts().list().execute() 

    if accounts.get('items'): 
    # Get the first Google Analytics account 
    firstAccountId = accounts.get('items')[0].get('id') 

    # Get a list of all the Web Properties for the first account 
    webproperties = service.management().webproperties().list(accountId=firstAccountId).execute() 

    if webproperties.get('items'): 
     # Get the first Web Property ID 
     firstWebpropertyId = webproperties.get('items')[0].get('id') 

     # Get a list of all Views (Profiles) for the first Web Property of the first Account 
     profiles = service.management().profiles().list(
      accountId=firstAccountId, 
      webPropertyId=firstWebpropertyId).execute() 

     if profiles.get('items'): 
     # return the first View (Profile) ID 
     return profiles.get('items')[0].get('id') 

    return None 


def get_results(service, profile_id): 
    # Use the Analytics Service Object to query the Core Reporting API 
    return service.data().ga().get(
     ids='ga:' + profile_id, 
     start_date='2014-01-10', 
     end_date='2014-09-08', 
     metrics='ga:sessions').execute() 


def print_results(results): 
    # Print data nicely for the user. 
    if results: 
    print 'First View (Profile): %s' % results.get('profileInfo').get('profileName') 
    print 'Total Sessions: %s' % results.get('rows')[0][0] 

    else: 
    print 'No results found' 


if __name__ == '__main__': 
    main(sys.argv) 

要測試輸出,我正在用我的終端命令

python hello_analytics_api_v3.py 

運行此打開它要求我來驗證我的谷歌帳戶的瀏覽器之後,我得到一個400錯誤

Error: redirect_uri_mismatch

The redirect URI in the request: http://localhost:8080/ did not match a registered redirect URI.

Google是如何獲得http://localhost:8000/作爲重定向URI的?這是我在谷歌開發者控制檯應用程序

REDIRECT URIS http://127.0.0.1:8000/oauth2callback/

JAVASCRIPT ORIGINS http://127.0.0.1:8000/

+0

你輸入8000,你確定你不是說8080? – DaImTo 2014-10-09 11:15:49

+0

@DaImTo不,我在端口8000上運行我的本地服務器。 – 2014-10-09 11:17:09

+0

嘗試本教程可能更適合您。 http://www.marinamele.com/use-google-analytics-api-with-python同時我認爲你應該仔細檢查你的client_secret.json是否想知道它是從教程中讀取的,而不是你創建的那個。 – DaImTo 2014-10-09 11:18:49

回答