0

我设法使用下面的类似代码使用 Google App Script 查询 GraphQL。但是,当我更改为使用突变时,它不起作用(下面的代码)。

错误代码为 400,文本为:{"errors":[{"message":"GraphQL 操作必须包含非空querypersistedQuery扩展名。","extensions":{"code":"INTERNAL_SERVER_ERROR"}}]}

我刚开始学习 Google App Script & Javascript,在网上搜索了几个小时后不知道如何解决这个问题。一些帮助将不胜感激。提前致谢。

function get_auth_headers(){
  email = "test@mail.com"
  password ="test"

  var query = "mutation={ \
                userLogin(input:{email:\"" + email + "\",password:\"" + password + "\"}){ \
                  token \
                } \
              }";

  Logger.log(query)
  var url1 = "https://server.matters.news/graphql?";
  url = encodeURI(url1 + query)
  Logger.log(url)
  var response = UrlFetchApp.fetch(url, {muteHttpExceptions: true });
  Logger.log("code: " + response.getResponseCode());
  Logger.log("text: " + response.getContentText());
  var response = UrlFetchApp.fetch(url,{method: 'GET', headers: {'Content-Type': 'application/json'}});
  var lists = JSON.parse((response.getContentText()));
  Logger.log(lists);
}

4

1 回答 1

0

设法从https://speckle.guide/dev/js-app-script.html获得答案 如果您有同样的问题,请参考下面的代码供您参考。

function get_auth_headers() {
  //email = "test@gmail.com"
  //password ="test"

  let url = "https://server.matters.news/graphql";
  let graphql = JSON.stringify({
    query: `mutation($input: UserLoginInput!) {userLogin(input: $input) {token}}`,
    variables: {
      input: {
        email: email,
        password: password,
      },
    },
  });

  let params = {
    method: "POST",
    payload: graphql,
    headers: {
      "Content-Type": "application/json",
      Authorization: "Bearer YOUR_PERSONAL_TOKEN",
    },
  };

  var response = UrlFetchApp.fetch(url, params);
  var lists = JSON.parse((response.getContentText()));
  //Logger.log(lists['data']['userLogin']['token']);
  return lists['data']['userLogin']['token'];
}

于 2022-02-26T15:48:57.273 回答