gpt4 book ai didi

javascript - 有没有办法从应用程序脚本中的 graphql API 获取数据?

转载 作者:行者123 更新时间:2023-12-01 00:56:34 25 4
gpt4 key购买 nike

我正在尝试通过应用脚本从 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")"

我需要做什么才能让我的代码正常工作?

最佳答案

解决方案是在获取之前对 url 进行编码:

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);
}

关于javascript - 有没有办法从应用程序脚本中的 graphql API 获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56535772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com