谷歌日历API - 通过服务帐户访问自己的日历 [英] Google Calendar API - Access own calendar via Service account

查看:289
本文介绍了谷歌日历API - 通过服务帐户访问自己的日历的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要访问的谷歌日历API来使用Python插入项。我创建了一个服务帐户的谷歌API控制台上,增加了一个私钥,下载了它。

I want to access the Google Calendar API to insert entries with Python. I created a Service account on the Google API console, added an private key, downloaded it.

但是,当我尝试修改我的日历点儿,它是在同一个帐户,我得到了以下错误消息。阅读的作品。

But when I try to modify anyting of my calendar, it is on the same account, I get the following error message. Reading works.

code是

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='423291807109-XXXXXXXXXXXXXXXXxx@developer.gserviceaccount.com',
                                                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='XXXXXXXXXXXXXXXXXX@group.calendar.google.com', body=event)

response = request.execute()

print(response)

错误消息是:

apiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/calendar/v3/calendars/XXXXXXXXXXXXXXXXXXX@group.calendar.google.com/events?alt=json returned "Forbidden">

我本来以为我可以用这个服务帐户访问自己的数据,但似乎并非如此。

I would have thought that I can access my own data with this service account, but it seems not to be.

谷歌称,

后,您还将有机会
  与私钥相关联的客户端ID。您将需要两个
  编码应用程序时。 -
  <一href=\"https://developers.google.com/accounts/docs/OAuth2?hl=de#scenarios\">https://developers.google.com/accounts/docs/OAuth2?hl=de#scenarios

我GOOGLE了约2个小时,但它似乎被记录非常糟糕。有没有一种方法,我可以将通过谷歌日历API的新事件,而无需用户交互(又名三足式OAuth),或者是有办法解决它?

I googled about 2 hours, but it seems to be documented very bad. Is there a way I can insert new events via the Google Calendar API without user interaction (aka 3-legged OAuth) or is there a way to fix it?

我刚刚发现德precated ClientLoging。为什么谷歌使得它很难?

I just found deprecated ClientLoging. Why does Google makes it that difficult?

亲切的问候

推荐答案

我意识到,3阶梯OAuth的工作,如果一个转储产生的凭据JSON和在每一个你需要他们的时间读取它们。

I realized that 3-stepped OAuth works if one dumps the resulting credentials to JSON and reads them in every time you need them.

所以流动是:
添加您的的client_secrets.json 在同一文件夹作为此脚本说明在谷歌API 。抓住从提示中给出的网址的关键。进入它请求提示。党!11。我希望这些凭据永远持续下去。

So the flow is: Add your client_secrets.json as stated on Google API in the same folder as this script. Grab the key from the url given in the prompt. Enter it on request to the prompt. Party!11. I hope these credentials last forever.

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

现在,在应用程序本身:

Now, in the application itself:

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)

要获得一个服务对象,人们现在可以调用

To get a service object, one can now call

service = __create_service()

和使用A​​PI​​就可以了。

and use the API on it.

这篇关于谷歌日历API - 通过服务帐户访问自己的日历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆