5

Pracuję nad moją pierwszą aplikacją, która kiedykolwiek używa Google Api dla Kalendarza. Czytałem przykłady Google pod adresem: https://developers.google.com/google-apps/calendar/instantiateBłąd modułu Google Api Auth Http

Po pierwszym uruchomieniu programu poniżej zakończyło się sukcesem. Pozwoliłem mojej aplikacji na dostęp do mojego konta Google, a aplikacja utworzyła plik calendar.dat z informacjami o autorze w moim katalogu aplikacji. Po zmianie nazwy pliku, w którym kod był w auth, przestał działać. Plik został już całkowicie usunięty i odtworzony od podstaw, ale błąd nadal występuje.

Nadal dostaję stronę autoryzacji Google i wciąż mogę potwierdzić dostęp, po czym otrzymuję komunikat, że proces uwierzytelniania został zakończony.

Jest to kod (standard przykład Google którego wypełnienie z moich danych aplikacji):

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

I to jest wyjście:

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' 

Odpowiedz

4

Problemem jest to, że w swoim katalogu roboczym masz plik o nazwie calendar.py. Kiedy moduł httplib2 Google chce zaimportować standardowy moduł kalendarza, zamiast niego otrzymuje lokalny. W lokalnym wykonuje go, aby wykonać import. Ale ponieważ httplib2 nie jest jeszcze w pełni zaimportowany, kod calendar.py nie działa poprawnie. Wystarczy zmienić nazwę pliku calendar.py na coś podobnego do myCalendar.py.

+0

Wielkie dzięki, już wypróbowałem to, ponieważ podejrzewałem, że to jest problem, jednak nie zadziałałoby. Koduję w Aptana3 i zmieniam nazwę pliku w programie. Jednak po przeczytaniu komentarza i sprawdzeniu katalogu okazało się, że Aptana generuje również skompilowane pliki Pythona, więc w katalogu nadal było dodatkowe pliki calendar.py. – Difusio

+1

To nie jest Aptana, ale przypuszczam pytona. Interpreter Pythona domyślnie tworzy skompilowane wersje. –

Powiązane problemy