我使用世博会,我想向拥有Google帐户的用户添加对我的应用程序的访问权限。然后,我需要获取有关登录我的应用程序的用户的Google日历的信息。
我使用Expo.Google.logInAsync(options)
(https://docs.expo.io/versions/latest/sdk/google)实现登录功能。我的范围如下所示:
scopes: ['https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/calendar.readonly']
当有人尝试登录我的应用程序时,它会询问查看日历列表的权限。作为回应,我得到:
Object {
"accessToken": "a",
"idToken": "b",
"refreshToken": "c",
"serverAuthCode": "d",
"type": "success",
"user": Object {
"email": "e",
"familyName": "f",
"givenName": "g",
"id": "h",
"name": "i",
"photoUrl": "j",
},
}
我收到了有关用户的数据,但我没有任何关于其日历的数据。我尝试使用此函数获取有关日历(https://developers.google.com/calendar/v3/reference/calendarList)的数据:
getUsersCalendarList = async (accessToken) => {
let calendarsList = await fetch('https://www.googleapis.com/calenda/v3/users/me/calendarList', {
headers: { Authorization: `Bearer ${accessToken}`},
});
return calendarsList;
}
作为回应,我得到了:
Response {
"_bodyBlob": Blob {
"_data": Object {
"blobId": "67b8b161-690f-4ff6-9cee-1dce12840ebd",
"offset": 0,
"size": 994,
},
},
"_bodyInit": Blob {
"_data": Object {
"blobId": "67b8b161-690f-4ff6-9cee-1dce12840ebd",
"offset": 0,
"size": 994,
},
},
"headers": Headers {
"map": Object {
"alt-svc": Array [
"quic=":443"; ma=2592000; v="44,43,39,35"",
],
"cache-control": Array [
"public, max-age=0",
],
"content-type": Array [
"application/json; charset=UTF-8",
],
"date": Array [
"Thu, 17 Jan 2019 11:30:32 GMT",
],
"expires": Array [
"Thu, 17 Jan 2019 11:30:32 GMT",
],
"server": Array [
"GSE",
],
"vary": Array [
"X-Origin",
],
"x-content-type-options": Array [
"nosniff",
],
"x-frame-options": Array [
"SAMEORIGIN",
],
"x-xss-protection": Array [
"1; mode=block",
],
},
},
"ok": false,
"status": 403,
"statusText": undefined,
"type": "default",
"url": "https://www.googleapis.com/calendar/v3/users/me/calendarList",
}
如何在世博会中获取用户的谷歌日历列表?
我在这里找到了解决方案:React-Native JSON fetch from URL。需要在返回的对象上使用json()
函数。getUsersCalendarList
schulde 看起来像这样:
getUsersCalendarList = async (accessToken) => {
let calendarsList = await fetch('https://www.googleapis.com/calenda/v3/users/me/calendarList', {
headers: { Authorization: `Bearer ${accessToken}`},
});
return calendarsList.json();
}
您还可以将访问令牌添加为请求的参数。
https://www.googleapis.com/calenda/v3/users/me/calendarList?access_token={token}
我不是反应开发人员,所以不确定如何修复您的标头。看起来还行。