【问题标题】:How to get access to user's google calendar from React Native (expo)?如何从 React Native (expo) 访问用户的谷歌日历?
【发布时间】:2019-06-11 13:18:03
【问题描述】:

我使用 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 帐户或仅从应用程序帐户使用accessToken 获取有关用户日历的信息吗?

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    我不是 React 开发人员,因此不确定如何修复您的标题。看起来不错。

    【讨论】:

    • 如何检查它是否在 react native 之外工作?我尝试从浏览器执行此 URL(使用真实令牌),但得到“未找到”。
    • 那么你的访问令牌有问题,它可能会过期一个小时。你可以在这里测试它,但你不能提供你的访问令牌developers.google.com/apis-explorer/#s/calendar/v3/…
    • @DalmTo 我的访问令牌如下所示:ya29.GluUBkIgKjUdNVAteJqDd3YMGj_pHztwosIK6dLnHgLYUWm_2SJy5ql_AyqaOHhE-Len_EsDt1wiORY25SdV4WjH5c0w4yl-zeT5tHcNpEaFO4tqwnY7uiTwjgya 它有什么问题?
    • 它可能已过期。它可能是一个刷新令牌,它可能是一个授权代码。尝试生成一个新的。
    猜你喜欢
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    • 2022-12-30
    • 1970-01-01
    • 1970-01-01
    • 2013-01-01
    • 1970-01-01
    • 2011-07-08
    相关资源
    最近更新 更多