gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:14:58 27 4
gpt4 key购买 nike

我正在尝试Slack API第一次,我想从 Java 客户端发布聊天消息。为此,我使用推荐的 HubSpot Java client和来自 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 应用程序并使用其身份验证 token 来运行此示例。我可以通过 postman 让它工作,但由于以下错误而没有使用上面的代码:

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

at com.hubspot.algebra.Result.lambda$unwrapOrElseThrow$1(Result.java:68)
at com.hubspot.algebra.Result.lambda$unwrapOrElseThrow$0(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$1.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$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.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)

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

最佳答案

我花了很长时间才弄清楚这一点。 Postfix :user:bot 在 UI 的以下两个部分中表示。

enter image description here

如果您在Bot Token Scopes下添加chat:write,那么您将拥有chat:write:bot。如果您在用户 token 范围下添加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 usechat:write and files:write.

<小时/>

如果出现以下错误,请执行给定的步骤

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

关于Java 到 Slack API : SlackError{type=MISSING_SCOPE, 错误=missing_scope},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59071887/

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