7

Google 캘린더 API에 액세스하여 Python으로 항목을 삽입하고 싶습니다. Google API 콘솔에 서비스 계정을 만들고 비공개 키를 추가하여 다운로드했습니다.Google 캘린더 API - 서비스 계정을 통해 자신의 캘린더에 액세스하십시오.

하지만 캘린더를 수정하려고하면 같은 계정에 있는데 다음과 같은 오류 메시지가 표시됩니다. 독서가 작동합니다.

코드

import httplib2 

from oauth2client.client import SignedJwtAssertionCredentials 
from apiclient.discovery import build 

event = { 
     'summary' : 'Appointment', 
     'location' : 'Somewhere', 
     'start' : { 
        'dateTime' : '2012-09-03T10:00:00.000-07:00' 
        }, 
     'end' : { 
       'dateTime' : '2012-09-03T10:25:00.000-07:00' 
        } 
} 


f = file("key.p12", "rb") 
key = f.read() 
f.close() 

credentials = SignedJwtAssertionCredentials(
               service_account_name='[email protected]', 
               private_key=key, 

               scope='https://www.googleapis.com/auth/calendar'            
              ) 

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

service = build('calendar', 'v3', http=http) 
request = service.events().insert(calendarId='[email protected]', body=event) 

response = request.execute() 

print(response) 

오류 메시지입니다 :

apiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/calendar/v3/calendars/[email protected]/events?alt=json returned "Forbidden"> 

나는이 서비스 계정으로 내 자신의 데이터에 액세스 할 수 있다고 생각했을 것이다, 그러나 수없는 것 같다.

구글은 서비스 계정이 생성 된

후, 당신은 또한 개인 키와 연관된 클라이언트 ID에 액세스 할 수 있을 것이라는 점을 주장한다. 애플리케이션 코딩시 이 모두 필요합니다. - https://developers.google.com/accounts/docs/OAuth2?hl=de#scenarios

나는 약 2 시간을 인터넷 검색,하지만 아주 나쁜 기록 할 것으로 보인다. 사용자 상호 작용 (일명 3-legged OAuth)없이 Google 캘린더 API를 통해 새로운 이벤트를 삽입 할 수있는 방법이 있습니까? 아니면이를 수정하는 방법이 있습니까?

방금 ​​더 이상 사용되지 않는 ClientLoging이 발견되었습니다. 왜 Google은 그렇게 어렵게 만들었습니까?

종류 내가 하나가 JSON으로 결과 자격 증명을 덤프하고 필요할 때마다 그들을 읽는 경우 3 계단 OAuth를 작동하는지 깨달았다

답변

1

간주한다.

그래서 흐름은 다음과 같습니다이 스크립트와 같은 폴더에 Google API에 명시된대로 client_secrets.json 추가 . 프롬프트에서 주어진 URL에서 키를 가져옵니다. 프롬프트가 나타나면 그것을 입력하십시오. 파티! 11. 나는이 신임장이 영원히 계속되기를 바란다. 이제

from oauth2client.client import flow_from_clientsecrets 

flow = flow_from_clientsecrets('client_secrets.json', 
           scope='https://www.googleapis.com/auth/calendar', 
           redirect_uri='urn:ietf:wg:oauth:2.0:oob') 

auth_uri = flow.step1_get_authorize_url() 
print('Visit this site!') 
print(auth_uri) 
code = raw_input('Insert the given code!') 
credentials = flow.step2_exchange(code) 
print(credentials) 

with open('credentials', 'wr') as f: 
    f.write(credentials.to_json()) 

, 응용 프로그램 자체에 :

def __create_service(): 
    with open('credentials', 'rw') as f: 
     credentials = Credentials.new_from_json(f.read()) 

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

    return build('calendar', 'v3', http=http) 

는 서비스 개체를 얻으려면, 하나는 지금

service = __create_service() 

를 호출 할 수 있습니다 그 위에 API를 사용합니다.

+0

나는 ur 솔루션을 따라했으며 내 자격 증명은 파일에 없습니다. 그 파일에서 그 파일들을 어떻게 불러 와서'credentials.authorize()'를 사용할 수 있는지 말해 주시겠습니까? – user26

+0

오래 전 예를 썼습니다. https://github.com/Rentier/ReindeerIkenga에서 찾을 수 있습니다. – reindeer