5

Ich arbeite an meiner ersten App, Google Api für Kalender zu verwenden. Ich habe die Google-Beispiele gelesen unter: https://developers.google.com/google-apps/calendar/instantiateGoogle Api Auth Http Modul Fehler

Das erste Mal, als ich das Programm unten lief, war es erfolgreich. Ich habe meiner App erlaubt, auf mein Google-Konto zuzugreifen, und die Anwendung hat eine Datei "calendar.dat" mit den Authentifizierungsinformationen in meinem App-Verzeichnis erstellt. Nachdem ich das Feld umbenannt hatte, wurde der Code in der Auth nicht mehr funktionieren. Ich habe die Datei bereits vollständig gelöscht und neu erstellt, aber der Fehler bleibt bestehen.

Ich bekomme immer noch die Google-Authentifizierungsseite und kann immer noch den Zugriff bestätigen, nach dem ich eine Nachricht erhalten, dass der Authentifizierungsablauf abgeschlossen wurde.

Dies ist der Code (Standard-Google-Beispiel, das ich mit meinen app Details ausfüllen):

import gflags 
import httplib2 

from apiclient.discovery import build 
from oauth2client.file import Storage 
from oauth2client.client import OAuth2WebServerFlow 
from oauth2client.tools import run 

FLAGS = gflags.FLAGS 

# Set up a Flow object to be used if we need to authenticate. This 
# sample uses OAuth 2.0, and we set up the OAuth2WebServerFlow with 
# the information it needs to authenticate. Note that it is called 
# the Web Server Flow, but it can also handle the flow for native 
# applications 
# The client_id and client_secret are copied from the API Access tab on 
# the Google APIs Console 
FLOW = OAuth2WebServerFlow(
    client_id='YOUR_CLIENT_ID', 
    client_secret='YOUR_CLIENT_SECRET', 
    scope='https://www.googleapis.com/auth/calendar', 
    user_agent='YOUR_APPLICATION_NAME/YOUR_APPLICATION_VERSION') 

# To disable the local server feature, uncomment the following line: 
# FLAGS.auth_local_webserver = False 

# If the Credentials don't exist or are invalid, run through the native client 
# flow. The Storage object will ensure that if successful the good 
# Credentials will get written back to a file. 
storage = Storage('calendar.dat') 
credentials = storage.get() 
if credentials is None or credentials.invalid == True: 
    credentials = run(FLOW, storage) 

# Create an httplib2.Http object to handle our HTTP requests and authorize it 
# with our good Credentials. 
http = httplib2.Http() 
http = credentials.authorize(http) 

# Build a service object for interacting with the API. Visit 
# the Google APIs Console 
# to get a developerKey for your own application. 
service = build(serviceName='calendar', version='v3', http=http, 
     developerKey='YOUR_DEVELOPER_KEY') 

Und dies ist die Ausgabe:

Your browser has been opened to visit: 

    https://accounts.google.com/o/oauth2/auth? (auth url shortened) 

If your browser is on a different machine then exit and re-run this 
application with the command-line parameter 

    --noauth_local_webserver 

Traceback (most recent call last): 
    File "C:\Users\Desktop\Google Drive\Code\Python\Rooster\calendar.py", line 2, in <module> 
    import httplib2 
    File "C:\Python27\lib\site-packages\httplib2-0.7.6-py2.7.egg\httplib2\__init__.py", line 42, in <module> 
    import calendar 
    File "C:\Users\Desktop\Google Drive\Code\Python\Rooster\calendar.py", line 33, in <module> 
    credentials = run(FLOW, storage) 
    File "C:\Python27\lib\site-packages\google_api_python_client-1.0-py2.7.egg\oauth2client\util.py", line 120, in positional_wrapper 
    return wrapped(*args, **kwargs) 
    File "C:\Python27\lib\site-packages\google_api_python_client-1.0-py2.7.egg\oauth2client\tools.py", line 169, in run 
    credential = flow.step2_exchange(code, http=http) 
    File "C:\Python27\lib\site-packages\google_api_python_client-1.0-py2.7.egg\oauth2client\util.py", line 120, in positional_wrapper 
    return wrapped(*args, **kwargs) 
    File "C:\Python27\lib\site-packages\google_api_python_client-1.0-py2.7.egg\oauth2client\client.py", line 1128, in step2_exchange 
    http = httplib2.Http() 
AttributeError: 'module' object has no attribute 'Http' 

Antwort

4

Das Problem ist, dass in Ihrem Arbeitsverzeichnis Sie haben die Datei namens calendar.py. Wenn httplib2 von Google ein Standardkalendermodul importieren möchte, wird stattdessen das lokale Kalendermodul verwendet. In der lokalen führt es es aus, um den Import durchzuführen. Da httplib2 jedoch noch nicht vollständig importiert wurde, funktioniert der calendar.py-Code nicht richtig. Benennen Sie die Datei "calendar.py" in "myCalendar.py" um.

+0

Vielen Dank, ich hatte das schon ausprobiert, da ich vermutet, dass das das Problem ist, jedoch würde es nicht funktionieren. Ich kodiere in Aptana3 und habe die Datei im Programm umbenannt. Nach dem Lesen des Kommentars und dem Überprüfen des Verzeichnisses erschien es jedoch, dass Aptana auch kompilierte Python-Dateien erzeugte, so dass es noch ein zusätzliches calendar.py im Verzeichnis gab. – Difusio

+1

Das ist nicht Aptana, sondern die Python, nehme ich an. Der Python-Interpreter erstellt standardmäßig die kompilierten Versionen. –