如何从React Native(expo)访问用户的Google日历?

时间:2019-01-17 10:49:54

标签: react-native google-calendar-api expo

我使用的是Expo,我想向拥有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",
     }

如何获取Expo中的用户Google日历列表?

2 个答案:

答案 0 :(得分:0)

您还可以将访问令牌作为参数添加到请求中。

https://www.googleapis.com/calenda/v3/users/me/calendarList?access_token={token}

我不是React开发人员,因此不确定如何解决标头。看起来还可以。

答案 1 :(得分:0)

我在这里找到解决方案: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();
}