问题描述
我订阅了我的应用以收听Google推送通知。频道详细信息一直存在。
@PostMapping(/ google / calendars /)
public void watch(HttpServletRequest请求,
@RequestHeader(value =X-Goog-Channel-ID,required = false)string googleChannelId,
@RequestHeader(value =X-Goog-Channel-Token ,必需= false)字符串googleChannelToken,
@RequestHeader(value =X-Goog-Channel-Expiration,required = false)字符串googleChannelExpiration,
@RequestHeader(value =X-Goog- ID,required = false)字符串googleResourceId,
@RequestHeader(value =X-Goog-Resource-URI,required = false)字符串googleResourceUri,
@RequestHeader(value =X-Goog- Resource-State,required = false)字符串googleResourceState,
@RequestHeader(value =X-Goog-Message-Number,required = false)字符串googleMessageNumber,
收到使用信息后,我可以打开客户端
到Google服务,但我不知道如何
googleChannelId = 354a78aa-0cc0-4b5b-96c5-e54e03a51e68
googleChannelToken =< null>
googleChannelExpiration = 2017年3月18日17:01:56 GMT
googleResourceId = eVHbdUeaWhfQPpvJPU7VZRxYzUs
googleResourceUri = https://www.googleapis.com/calendar/v3/calendars/sergii_vlasiuk@ukr。 net / events?maxResults = 250& alt = json
googleResourceState =存在
googleMessageNumber = 403802722
存在
表示 googleResourceId
已被更改。
使用 googleChannelId
我加载我的频道详情并打开客户端。
- 如何使用
googleResourceId
在我的pull change请求中(请求示例也许)??
- 是否正确理解:
not_exists
表示资源被删除;
存在
表示资源已创建或编辑?
添加详情
我试过了
client.events().get(originalCalendarId,googleResourceId).execute()
Where :
-
客户端
是com.google.api。 services.calendar.Calendar
带有正确标记的对象。 -
ginalCalendarId
一个用于订阅。 -
googleResourceId
- 来自标题消息X-Goog-Resource -ID
值。
- >
未找到
{
code:404,
errors:[{
domain:global,
message:Not Found,
reason:notFound
}],
message: 未找到
} 如果Google不'没有一个方便的方法来区分哪些实体已经更新,我想我们应该考虑一个解决方法。在我看来,通过遍历和重写所有数据来获取所有事件并更新数据库状态会导致一些性能问题。为了解决这个问题,我会选择使用每个事件所具有的 etag 值。每次事件更新时,ETag都会改变(在 CalDav协议中使用)
如果您通过事件将etag保存到数据库中,那么一旦您从Google获取事件列表,就可以检查etag值并仅更新真正更改的元素。更新条目时,必须优化零件。
Google支持etag。
您可以在这里找到它:
{
kind:calendar#events,
etag:etag,
summary:string,
description:string,
updated:datetime,
timeZone:string,
accessRole:string,
defaultReminders:[
{
method:string,
minutes:integer
}
],
nextPageToken:string,
nextSyncToken:string ,
items:[
events Resource
]
}
已更新:
$ b 有执行同步的更方便的方法。
您可以使用增量同步方法。增量同步允许您检索自上次同步请求以来已修改的所有资源。
您每次更新时只需保存同步令牌数据并在请求中使用它来获取新数据。在这种情况下,您将只获取已修改的数据。
Calendar.Events.List request = client.events()。list(primary);
request.setSyncToken(syncToken);
如果您打电话给您:
events = request.execute();
列表< Event> items = events.getItems();
您将只获取已更新的数据。
I have subscribed my app to listen to Google push notifications. Channel details was persisted. I can receive notifications from google to my endpoint.
@PostMapping("/google/calendars/") public void watch(HttpServletRequest request, @RequestHeader(value = "X-Goog-Channel-ID", required = false) String googleChannelId, @RequestHeader(value = "X-Goog-Channel-Token", required = false) String googleChannelToken, @RequestHeader(value = "X-Goog-Channel-Expiration", required = false) String googleChannelExpiration, @RequestHeader(value = "X-Goog-Resource-ID", required = false) String googleResourceId, @RequestHeader(value = "X-Goog-Resource-URI", required = false) String googleResourceUri, @RequestHeader(value = "X-Goog-Resource-State", required = false) String googleResourceState, @RequestHeader(value = "X-Goog-Message-Number", required = false) String googleMessageNumber,
Using information was received I can open
client
to google service. But I do not know how to get changes.googleChannelId=354a78aa-0cc0-4b5b-96c5-e54e03a51e68 googleChannelToken=<null> googleChannelExpiration=Sat, 18 Mar 2017 17:01:56 GMT googleResourceId=eVHbdUeaWhfQPpvJPU7VZRxYzUs googleResourceUri=https://www.googleapis.com/calendar/v3/calendars/sergii_vlasiuk@ukr.net/events?maxResults=250&alt=json googleResourceState=exists googleMessageNumber=403802722
exists
meansgoogleResourceId
was changed.Using
googleChannelId
I load my channel details and open client.- How to use
googleResourceId
in my pull change request (request example maybe)? - Is it correct understanding:
not_exists
means resource was deleted;exists
means resource was created or edited?
Add details
I've tried
client.events().get(originalCalendarId, googleResourceId).execute()
Where:
client
iscom.google.api.services.calendar.Calendar
object with correct tokens.ginalCalendarId
- is good known google calendar Id, one was used in subscription.googleResourceId
- is from header messageX-Goog-Resource-ID
value.->404 Not Found{ "code" : 404, "errors" : [ { "domain" : "global", "message" : "Not Found", "reason" : "notFound" } ], "message" : "Not Found"}
解决方案If Google doesn't have a convenient approach to distinguish which exactly entities have been updated, I suppose we should think about a workaround. In my opinion, getting all the events and updating your database state by traversing and rewriting all the data will lead you to some performance issues.
To address that, I would choose using "etag" value that each event has. Every time an event is updated, the ETag changes(works in CalDav protocol)
http://cdn.nsoftware.com/help/IP9/cs/CalDAV_p_ETag.htm
If you save etag to your database with your event, then once you get the list of events from Google, you can just check etag value and update only the elements that were really changed. That must optimize the part when you update your entries.
Google supports etag.You can find it here:
https://developers.google.com/google-apps/calendar/v3/reference/events/list
{ "kind": "calendar#events", "etag": etag, "summary": string, "description": string, "updated": datetime, "timeZone": string, "accessRole": string, "defaultReminders": [ { "method": string, "minutes": integer } ], "nextPageToken": string, "nextSyncToken": string, "items": [ events Resource ] }
UPDATED:
There is a more convenient way to perform synchronization.You can use Incremental synchronization approach. Incremental sync allows you to retrieve all the resources that have been modified since the last sync request.
https://developers.google.com/google-apps/calendar/v3/sync#initial_full_sync
You just need to save sync token every time you update data and use it in the request to get new data. In this case you will get only data that was modified.
Calendar.Events.List request = client.events().list("primary"); request.setSyncToken(syncToken);
If you call then:
events = request.execute(); List<Event> items = events.getItems();
You will get only the data that was updated.
这篇关于Google推送通知Java处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
- How to use