2013-08-19 3 views
2

내 캘린더에서 내 파이썬 프로그램으로 이벤트 목록을 가져오고 싶습니다. 내 Google이 제공 한 oauth의 방대한 문서를 이해할 수 없습니다.Google 캘린더 API를 파이썬과 함께 사용하는 방법

이것은 내가 시도한 코드입니다. 그 절반 비트와 peices. 그 누구도 날이 하나

import gflags 
import httplib2 

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


flow = flow_from_clientsecrets('client_secrets.json', 
           scope='https://www.googleapis.com/auth/calendar', 
           redirect_uri='http://example.com/auth_return') 

http = httplib2.Http() 
http = credentials.authorize(http) 

service = build(serviceName='calendar', version='v3', http=http, 
     developerKey='AI6456456456456456456456456456yKhI') 

events = service.events().list(calendarId='[email protected]').execute() 
print events 

건을 해결하는 데 도움 수있는 것은 내가 내 흐름 자격 증명이 내가 API 콘솔 프로젝트를 만들고 그것에 캘린더 API를 추가 한

개체를 얻을 수있는 방법이다. 그것들을 클라이언트에 넣으십시오.

나는 브라우저 리다이렉션과 내가 원하는 내 캘린더를 원하지 않는다. 사용자가 캘린더에 액세스하는 것을 원하지 않지만 캘린더에 액세스하여 웹 사이트에 내 이벤트를 게시 할 수 있습니다. 내가 틀리지 않는 경우, OAuth를하지 않고 로그인 할 수 있습니다 (사용되지 않음) V2 API를 사용할 수 있습니다 내 캘린더

답변

2

을 acessing에 대한의 Oauth를 사용해야 할 이유

모르겠어요. v3의 (주, 나는 flow_from_clientsecrets를 사용하지 않는)이 내가 인증 흐름이 무엇이며, 그것을 작동 : 다음

import gflags 
from oauth2client.file import Storage 
from oauth2client.client import OAuth2WebServerFlow 

flags = gflags.FLAGS 
flow = OAuth2WebServerFlow(
     client_id = 'some_id.apps.googleusercontent.com', 
     client_secret = 'some_secret-32ijfsnfkj2jf', 
     scope='https://www.googleapis.com/auth/calendar', 
     user_agent='Python/2.7') 

# to get a link for authentication in a terminal, 
# which needs to be opened in a browser anyway 
flags.auth_local_webserver = False 

# store auth token in a file 'calendar.dat' if it doesn't exist, 
# otherwise just use it for authentication 
base = os.path.dirname(__file__) 
storage = Storage(os.path.join(base, 'calendar.dat')) 
credentials = storage.get() 
if credentials is None or credentials.invalid == True: 
    credentials = run(FLOW, storage) 

http = httplib2.Http() 
http = credentials.authorize(http) 
service = build(serviceName='calendar', version='v3', http=http, 
    developerKey='AI6456456456456456456456456456yKhI') 

와 통신 할 수 service를 사용합니다. 이 코드는 일부 가져 오기가 누락되었을 수 있습니다. 그리고 올바르게 기억하면 기본 캘린더에 액세스하는 경우 캘린더 ID를 제공 할 필요가 없습니다.

나는 그것이 도움이되기를 바랍니다.

+1

감사합니다. 1.) 경고를받습니다. 경고 : root :이 함수 oauth2client.tools.run()과 gflags 라이브러리의 사용은 더 이상 사용되지 않으며 li brary의 차후 버전에서 제거 될 것입니다. 밖에있는 새로운 방법. 2) 명령 줄에서 파이썬 프로그램을 실행하고 인증 코드를 묻습니다. 어떻게 그 코드를 얻을 수 있습니까? 인증 코드없이 로그인 할 수 없습니다. 어떻게 1을 위해서 – user26

+0

을 사용하고 계십니까?) 같은 툴 패키지에서'run_flow'를 사용할 수 있습니다. 그리고 2.) 나는 브라우저 상호 작용을 완전히 무시할 수 있다고 생각하지 않는다. 브라우저를 통해 적어도 앱에 대한 액세스 권한을 부여해야하며 액세스 코드를 제공합니다 (기본값을 변경하지 않은 경우 브라우저에서 'redirect_uri = oob'을 사용하여 가져 오기). –

+1

TypeError : run_flow()가 3 이상 인수 (주어진 2) – elexhobby

관련 문제