是否可以通过应用脚本中的graphql API获取数据?

时间:2019-06-11 02:41:40

标签: javascript google-apps-script google-sheets graphql fetch-api

我正在尝试通过app脚本从graphql API中获取数据。

使用POST提取graphql数据(Providing query string to fetch from a GraphQL API)时出现问题,但是我需要提取的url只允许GET。

我写的代码如下:

function main() {
  var ss = SpreadsheetApp.getActiveSheet();
  url = "https://coletum.com/api/graphql?query={answer(formId:7787){answer{nomeDaEmpresa107176,cnpj107177,inscricaoEstadual107178,telefone107179},metaData{userId,userName,source,friendlyId,createdAt,createdAtDevice,createdAtCoordinates,updatedAt,updatedAtCoordinates}}}&token=k4foipsju5ckosk4w8o0w084cc8oos0"

  var response = UrlFetchApp.fetch(url,{method: 'GET', headers: { 'Content-Type': 'application/json'} })

  var lists = JSON.parse((response.getContentText()));

  Logger.log(lists);
}

如果我将网址粘贴到浏览器中,则一切正常:

{
  data: {
    answer: [
      {
        answer: {
          nomeDaEmpresa107176: "LAVORO",
          cnpj107177: "26.693.198/0001-20",
          inscricaoEstadual107178: 12341234,
          telefone107179: "(66) 1234-1234"
        },
        metaData: {
          userId: 9938,
          userName: "Mendes",
          source: "web_private",
          friendlyId: "1.202279",
          createdAt: "2019-05-10T00:14:54+00:00",
          createdAtDevice: "2019-05-10T00:14:54+00:00",
          createdAtCoordinates: {
            longitude: -52.20797,
            latitude: -12.58536
          },
          updatedAt: null,
          updatedAtCoordinates: {
            longitude: null,
            latitude: null
          }
        }
      },
      {
        answer: {
          nomeDaEmpresa107176: "RURAL",
          cnpj107177: "26.693.198/0001-20",
          inscricaoEstadual107178: 1234,
          telefone107179: "(66) 1234-1234"
        },
        metaData: {
          userId: 9938,
          userName: "Mendes",
          source: "web_private",
          friendlyId: "1.202280",
          createdAt: "2019-05-10T00:15:15+00:00",
          createdAtDevice: "2019-05-10T00:15:15+00:00",
          createdAtCoordinates: {
            longitude: -52.20797,
            latitude: -12.58536
          },
          updatedAt: null,
          updatedAtCoordinates: {
            longitude: null,
            latitude: null
          }
        }
      },
      {
        answer: {
          nomeDaEmpresa107176: "PRIMAVERA MÁQUINAS",
          cnpj107177: "26.693.198/0001-20",
          inscricaoEstadual107178: 12341234,
          telefone107179: "(66) 1234-1234"
        },
        metaData: {
          userId: 9938,
          userName: "Mendes",
          source: "web_private",
          friendlyId: "1.202281",
          createdAt: "2019-05-10T00:15:51+00:00",
          createdAtDevice: "2019-05-10T00:15:51+00:00",
          createdAtCoordinates: {
            longitude: -52.20797,
            latitude: -12.58536
          },
          updatedAt: null,
          updatedAtCoordinates: {
            longitude: null,
            latitude: null
          }
        }
      },
      {
        answer: {
          nomeDaEmpresa107176: "SINAGRO",
          cnpj107177: "26.693.198/0001-20",
          inscricaoEstadual107178: 1234,
          telefone107179: "(66) 98412-1030"
        },
        metaData: {
          userId: 9938,
          userName: "Mendes",
          source: "web_private",
          friendlyId: "1.202633",
          createdAt: "2019-05-10T20:54:02+00:00",
          createdAtDevice: "2019-05-10T20:54:02+00:00",
          createdAtCoordinates: {
            longitude: -52.20629,
            latitude: -12.59418
          },
          updatedAt: null,
          updatedAtCoordinates: {
            longitude: null,
            latitude: null
          }
        }
      }
    ]
  }
}

但是在应用程序脚本中使用Url Fetch时出现错误:

"Invalid Argument: https://coletum.com/api/graphql?query={answer(formId:7787){answer{nomeDaEmpresa107176,cnpj107177,inscricaoEstadual107178,telefone107179},metaData{userId,userName,source,friendlyId,createdAt,createdAtDevice,createdAtCoordinates,updatedAt,updatedAtCoordinates}}}&token=k4foipsju5ckosk4w8o0w084cc8oos0 (line 5, file "Código")"

要使我的代码正常工作,我该怎么做?

1 个答案:

答案 0 :(得分:1)

解决方案是在提取之前对网址进行编码:

function main() {
  var ss = SpreadsheetApp.getActiveSheet();
  url_orcamentos = "https://coletum.com/api/graphql?query={answer(formId:7787){answer{nomeDaEmpresa107176,cnpj107177,inscricaoEstadual107178,telefone107179},metaData{userId,userName,source,friendlyId,createdAt,createdAtDevice,createdAtCoordinates,updatedAt,updatedAtCoordinates}}}&token=k4foipsju5ckosk4w8o0w084cc8oos0"
  url = encodeURI(url_orcamentos)
  var response = UrlFetchApp.fetch(url,{method: 'GET', headers: { 'Content-Type': 'application/json'} })

  var lists = JSON.parse((response.getContentText()));

  Logger.log(lists);
}
相关问题