如何为在Meteor应用程序中使用Google帐户注册的用户处理Google日历数据?
例如,我要插入事件或至少发送事件邀请。我的用户使用Google帐户在我的应用程序中注册,但是即使他们注销了,我也想插入事件。下面是我到目前为止拥有的服务器代码,但是它给了我“权限不足”错误。
if (user && user.services && user.services.google &&
user.services.google.accessToken) {
var startTimeUTC = moment.utc(event.startTime, "YYYY-MM-DD HH:mm:ss").format();
var endTimeUTC = moment.utc(event.endTime, "YYYY-MM-DD HH:mm:ss").format();
console.log("POSTINGGCAL");
if (moment.utc().unix() < moment.utc(event.endTime).unix()) {
console.log("ENTERGCAL");
var id = Meteor.http.post("https://www.googleapis.com/calendar/v3/calendars/primary/events", {
'headers' : {
'Authorization': "Bearer " + user.services.google.accessToken,
'Content-Type': 'application/json'
},
'data': {
"summary": event.name,
"location": event.location,
"start": {
"dateTime": startTimeUTC,
},
"end": {
"dateTime": endTimeUTC,
},
"attendees": [
{
"email": user.email,
},
]
}
});
}//end if moment
}//end if user
错误:
W20141207-16:53:31.883(7)? (STDERR)
W20141207-16:53:31.883(7)? (STDERR) /Users/.meteor/packages/meteor-tool/.1.0.35.lzatbc++os.osx.x86_64+web.browser+web.cordova/meteor-tool-os.osx.x86_64/dev_bundle/lib/node_modules/fibers/future.js:206
W20141207-16:53:31.884(7)? (STDERR) throw(ex);
W20141207-16:53:31.884(7)? (STDERR) ^
W20141207-16:53:31.909(7)? (STDERR) Error: failed [403] { "error": { "errors": [ { "domain": "global", "reason": "insufficientPermissions", "message": "Insufficient Permission" } ], "code": 403, "message": "Insufficient Permission" } }
W20141207-16:53:31.909(7)? (STDERR) at Object.Future.wait (/Users/.meteor/packages/meteor-tool/.1.0.35.lzatbc++os.osx.x86_64+web.browser+web.cordova/meteor-tool-os.osx.x86_64/dev_bundle/lib/node_modules/fibers/future.js:326:15)
W20141207-16:53:31.910(7)? (STDERR) at Object.call (packages/meteor/helpers.js:118)
W20141207-16:53:31.910(7)? (STDERR) at Object.HTTP.post (packages/http/httpcall_common.js:56)
W20141207-16:53:31.910(7)? (STDERR) at scheduleIt (app/server/scheduler.js:243:28)
W20141207-16:53:31.910(7)? (STDERR) at app/server/startup.js:16:8
W20141207-16:53:31.911(7)? (STDERR) at _.extend.forEach (packages/mongo/mongo_driver.js:894)
W20141207-16:53:31.911(7)? (STDERR) at Cursor.(anonymous function) [as forEach] (packages/mongo/mongo_driver.js:741)
W20141207-16:53:31.911(7)? (STDERR) at app/server/startup.js:14:20
W20141207-16:53:31.912(7)? (STDERR) at _.extend.forEach (packages/mongo/mongo_driver.js:894)
W20141207-16:53:31.912(7)? (STDERR) at Cursor.(anonymous function) [as forEach] (packages/mongo/mongo_driver.js:741)
W20141207-16:53:31.912(7)? (STDERR) - - - - -
W20141207-16:53:31.913(7)? (STDERR) at makeErrorByStatus (packages/http/httpcall_common.js:12)
W20141207-16:53:31.913(7)? (STDERR) at Request._callback (packages/http/httpcall_server.js:95)
W20141207-16:53:31.913(7)? (STDERR) at Request.self.callback (/Users/.meteor/packages/http/.1.0.8.l3xm0k++os+web.browser+web.cordova/npm/node_modules/request/request.js:122:22)
W20141207-16:53:31.913(7)? (STDERR) at Request.emit (events.js:98:17)
W20141207-16:53:31.914(7)? (STDERR) at Request.<anonymous> (/Users/.meteor/packages/http/.1.0.8.l3xm0k++os+web.browser+web.cordova/npm/node_modules/request/request.js:888:14)
W20141207-16:53:31.914(7)? (STDERR) at Request.emit (events.js:117:20)
W20141207-16:53:31.914(7)? (STDERR) at IncomingMessage.<anonymous> (/Users/.meteor/packages/http/.1.0.8.l3xm0k++os+web.browser+web.cordova/npm/node_modules/request/request.js:839:12)
W20141207-16:53:31.914(7)? (STDERR) at IncomingMessage.emit (events.js:117:20)
W20141207-16:53:31.915(7)? (STDERR) at _stream_readable.js:929:16
W20141207-16:53:31.915(7)? (STDERR) at process._tickCallback (node.js:419:13)
=> Exited with code: 8
编辑:我在客户端上添加了以下内容,尽管我不再收到任何错误,但日历事件未显示在我的测试Google帐户上。有任何想法吗?
Accounts.ui.config({
requestPermissions: {
google:
['https://www.googleapis.com/auth/calendar',
'https://www.googleapis.com/auth/calendar.readonly',
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/tasks'],
// forceApprovalPrompt: {google: true}
},
forceApprovalPrompt: {google: true},
requestOfflineToken: {google: true},
passwordSignupFields: 'EMAIL_ONLY',
// extraSignupFields: []
});
以下流星文档无济于事,因为出现此错误:未捕获的错误:Accounts.ui.config:无效的密钥:forceApprovalPrompt
查看来自Google API的错误:
insufficientPermissions
这意味着您使用的令牌是通过要求用户仅登录而不设置日历访问权限来实现的。
因此,当您让用户登录并使用Meteor.loginWithGoogle
或accounts-ui
打包时,您需要询问Calendar API范围:
Meteor.loginWithGoogle({requestPermissions: ['https://www.googleapis.com/auth/calendar'], forceApprovalPrompt: true})
或与 accounts-ui
Accounts.ui.config({requestPermissions:{google:['https://www.googleapis.com/auth/calendar']}, forceApprovalPrompt: {google: true}})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句