松弛的JSON post请求返回invalid_auth

时间:2018-06-02 17:07:07

标签: javascript json google-apps-script slack slack-api

我试图在用户按下按钮后通过谷歌应用程序脚本打开一个对话框但是我收到以下错误消息:

{"ok":false,"error":"invalid_auth","warning":"missing_charset","response_metadata":{"warnings":["missing_charset"]}}

这是我的代码:

function openDialog (range, triggerId, token) {
    var url = 'https://slack.com/api/dialog.open';
    var dialog = {
        trigger_id: triggerId,
        title: 'Submit a helpdesk ticket',
        callback_id: 'submit-ticket',
        submit_label: 'Submit',
        elements: [
            {
                label: 'Title',
                type: 'text',
                name: 'title',
                value: 'teste',
                hint: '30 second summary of the problem',
            },
            {
                label: 'Description',
                type: 'textarea',
                name: 'description',
                optional: true,
            },
            {
                label: 'Urgency',
                type: 'select',
                name: 'urgency',
                options: [
                    { label: 'Low', value: 'Low' },
                    { label: 'Medium', value: 'Medium' },
                    { label: 'High', value: 'High' },
                ],
            },
        ],
    };

    var options = {
        'method' : 'post',                  
        'contentType': 'application/json', 
        'headers': {
            'Authorization': 'Bearer ' +  token,
        },
        'payload' : JSON.stringify(dialog),
    };

    var urlFetch = UrlFetchApp.fetch(url, options);
    var message = ContentService.createTextOutput(urlFetch).setMimeType(ContentService.MimeType.JSON);

    return message; 
}

有人能发现我失踪的东西吗?

由于

这是doPost()函数触发openDialog()函数,它从callback_id = "gasolina"value = "update"if (payload.callback_id == "gasolina") { var selectedOption = actions.value; var operation = payload.callback_id; var triggerId = payload.trigger_id; var token = payload.token; var inputRow = actions.name; if (selectedOption == 'update') { var keyword = 'no+money'; var gastoExtra = ''; var operation = payload.callback_id; var gastoExtraRange = actions.name; return openDialog (gasRange, triggerId, token); 的松弛按钮读取有效负载:

{{1}}

1 个答案:

答案 0 :(得分:0)

我认为虽然你的脚本几乎是正确的,但还是需要稍加修改。你的脚本修改怎么样?从您的问题来看,我不确定您使用Slack的dialog.open的当前设置。因此,此修改后的脚本假设设置正确。

修改要点:

  • missing_charset的原因是由于JSON.stringify()使用了有效负载。
  • 来自the document of dialog.open,有效负载为tokendialogtrigger_id
  • token不会用于标题。
  • application/json不适用于contentType

当这些点反映到您的脚本时,修改后的脚本如下所示。

修改后的脚本:

function openDialog (range, triggerId, token) {
  var url = 'https://slack.com/api/dialog.open';
  var dialog = {
    title: 'Submit a helpdesk ticket',
    callback_id: 'submit-ticket',
    submit_label: 'Submit',
    elements: [
      {
        label: 'Title',
        type: 'text',
        name: 'title',
        value: 'teste',
        hint: '30 second summary of the problem',
      },
      {
        label: 'Description',
        type: 'textarea',
        name: 'description',
        optional: true,
      },
      {
        label: 'Urgency',
        type: 'select',
        name: 'urgency',
        options: [
          { label: 'Low', value: 'Low' },
          { label: 'Medium', value: 'Medium' },
          { label: 'High', value: 'High' },
        ],
      },
    ],
  };
  var options = {
    method: 'post',
    payload: {
      token: token,
      dialog: JSON.stringify(dialog),
      "trigger_id": triggerId,
    },
  };
  var urlFetch = UrlFetchApp.fetch(url, options);
  var message = ContentService.createTextOutput(urlFetch).setMimeType(ContentService.MimeType.JSON);
  return message; 
}

注意:

  • 在我的环境中,我使用了dialog.open和上面的脚本请求。但是,如果这不起作用,请检查错误消息并修改您的设置。