Java 到 Slack API:SlackError{type=MISSING_SCOPE, error=missing_scope}

Java to Slack API: SlackError{type=MISSING_SCOPE, error=missing_scope}

我正在尝试 Slack API for the first time and I wanted to post a chat message from a Java client. For this purpose I am using the recommended HubSpot Java client and from its examples 我正在尝试以下代码摘录:

import com.hubspot.slack.client.SlackClient;
import com.hubspot.slack.client.SlackClientFactory;
import com.hubspot.slack.client.SlackClientRuntimeConfig;

    public class BasicRuntimeConfig {

        public static SlackClient getClient() {
            return SlackClientFactory.defaultFactory().build(get());
        }

        public static SlackClientRuntimeConfig get() {
            return SlackClientRuntimeConfig.builder()
                    .setTokenSupplier(() -> "the-token-from-my-slack-app")
                    .build();
        }
    }

和:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hubspot.algebra.Result;
import com.hubspot.slack.client.SlackClient;
import com.hubspot.slack.client.methods.params.chat.ChatPostMessageParams;
import com.hubspot.slack.client.models.response.SlackError;
import com.hubspot.slack.client.models.response.chat.ChatPostMessageResponse;

public class PostAMessage {
    private static final Logger LOG = LoggerFactory.getLogger(PostAMessage.class);

    public static ChatPostMessageResponse messageChannel(String channelToPostIn, SlackClient slackClient) {
        Result<ChatPostMessageResponse, SlackError> postResult = slackClient.postMessage(
                ChatPostMessageParams.builder()
                        .setText("Hello me! Here's a slack message!")
                        .setChannelId(channelToPostIn)
                        .build()
        ).join();

        return postResult.unwrapOrElseThrow(); // release failure here as a RTE
    }
}

最后我 运行:

@Test
public void testSlackChatMessage() {

    SlackClient client = BasicRuntimeConfig.getClient();

    ChatPostMessageResponse response = messageChannel("general", client);
    LOG.info("Got: {}", response);

}

之前我创建了一个 Slack 应用程序并将其身份验证令牌用于 运行 这个示例。由于以下错误,我能够通过 postman 让它工作,但没有使用上面的代码:

java.lang.IllegalStateException: SlackError{type=MISSING_SCOPE, error=missing_scope}

at com.hubspot.algebra.Result.lambda$unwrapOrElseThrow(Result.java:68)
at com.hubspot.algebra.Result.lambda$unwrapOrElseThrow[=17=](Result.java:64)
at java.base/java.util.Optional.orElseThrow(Optional.java:408)
at com.hubspot.algebra.Result.unwrapOrElseThrow(Result.java:60)
at com.hubspot.algebra.Result.unwrapOrElseThrow(Result.java:64)
at com.hubspot.algebra.Result.unwrapOrElseThrow(Result.java:68)
at io.vakt.messaging.poc.slack.PostAMessage.messageChannel(PostAMessage.java:30)
at io.vakt.messaging.poc.slack.PocSlackTest.testSlackChatMessage(PocSlackTest.java:22)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=17=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

有人知道我错过了什么吗?感谢您的关注。

我想通了。 API 状态:

所以我开始怀疑HubSpot的post消息操作和Slack网站上提供的postman例子不一样,只需要chat:write:user 范围。事实证明,HubSpot post 消息需要范围 chat:write:bot

我遇到了同样的问题。

我试图将 chat:write:bot 添加为建议的答案,但我在机器人令牌范围中找不到该范围。

所以我在 bot Bot 令牌范围

中添加了 chat:write 范围

由于忘记重新安装应用步骤,我仍然面临问题。

执行的步骤:

  1. 添加范围chat:write
  2. 重新安装应用
  3. 邀请机器人用户加入您发送消息的频道

我花了很长时间才弄明白这一点。后缀 :user:bot 在 UI.

的以下两部分中表示

如果您在 Bot Token Scopes 下添加 chat:write,那么您将拥有 chat:write:bot。如果您在 User Token Scopes 下添加 chat:write,那么您将拥有 chat:write:user.


更多相关信息:

如果您了解 Perspectival scopes,您会发现从现在开始您只需使用 chat:write

Perspectival scopes (for example, chat:write:user, chat:write:bot, files:write:user, files:write:bot) are disappearing. Just use chat:write and files:write.


如果出现以下错误,按照给定的步骤

{
   "ok": false,
   "error": "missing_scope",
   "needed": "chat:write:bot"
}
  1. Bot 令牌范围
  2. 下添加 chat:write 权限
  3. 重新安装应用程序
  4. 邀请机器人用户到您要向其发送消息的频道(来自@NIrav Modi 的回答)