0

我有这里描述的代码。我需要在我的日历中创建很多事件。

我找不到让它工作的方法。使用 2 legged 似乎已被弃用

import sys
import httplib2
from  rfc3339 import rfc3339
from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import AccessTokenRefreshError
from oauth2client.client import flow_from_clientsecrets
from oauth2client.tools import run
import pytz
import datetime
import time
start_zone = pytz.timezone('Europe/London')
end_zone = pytz.timezone('Europe/Oslo')

start_time = datetime.datetime(2013,8,13,15,0, tzinfo=start_zone)
end_time = datetime.datetime(2013,8,16,19,0, tzinfo=end_zone)


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

storage = Storage('credentials.dat')
credentials = storage.get()
if credentials is None or credentials.invalid:
  credentials = run(flow, storage)

http = httplib2.Http()
http = credentials.authorize(http)
service = build('calendar', 'v3', http=http)

try:

  event = {
    'start': {
      'dateTime': start_time
      },
    'end': {
      'dateTime': end_time
      },
    "summary": "New event",
    "location": "Paris, FRANCE"
  }
  service.events().insert(calendarId='primary', body=event).execute()
  print "END"
except AccessTokenRefreshError:
  print ('Credentials have been revoked')

我已经以这种方式更新了我的代码(因为我没有重定向)

#!/usr/bin/python
import sys
import httplib2
from  rfc3339 import rfc3339
from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import AccessTokenRefreshError
from oauth2client.client import flow_from_clientsecrets
from oauth2client.tools import run
import pytz
import datetime
import time
start_zone = pytz.timezone('Europe/London')
end_zone = pytz.timezone('Europe/Oslo')

start_time = datetime.datetime(2013,8,13,15,0, tzinfo=start_zone)
end_time = datetime.datetime(2013,8,16,19,0, tzinfo=end_zone)

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.dat', 'wr') as f:
  f.write(credentials.to_json())

storage = Storage('credentials.dat')
credentials = storage.get()
if credentials is None or credentials.invalid:
  credentials = run(flow, storage)

http = httplib2.Http()
http = credentials.authorize(http)
service = build('calendar', 'v3', http=http)

try:
  event = {
    'start': {
      'dateTime': start_time
      },
    'end': {
      'dateTime': end_time
      },
    "summary": "New event",
    "location": "Paris, FRANCE"
  }
  service.events().insert(calendarId='primary', body=event).execute()
  print "END"
except AccessTokenRefreshError:
  print ('Credentials have been revoked')

所以当我运行我的脚本时,它会打开一个窗口广告,要求我登录。所以我输入了我的 gmail 用户名/密码,之后出现以下错误:

Error: redirect_uri_mismatch
The redirect URI in the request: urn:ietf:wg:oauth:2.0:oob did not match a registered redirect URI 
4

1 回答 1

1

错误消息表明您使用的 client_secrets.json 不适用于已安装的应用程序。尝试在 API 控制台中重新创建客户端 ID,并确保您指定它是已安装的应用程序。

于 2013-08-07T18:21:01.037 回答