Search code examples
javaslackslack-api

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


I am trying 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 I am trying the following code excerpts:

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

and:

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

and finally I run:

@Test
public void testSlackChatMessage() {

    SlackClient client = BasicRuntimeConfig.getClient();

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

}

Previously I created a Slack App and used its authentication token to run this example. I was able to have it working via postman, but not using the code above, due to the following error:

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)

Does anyone know what I am missing? Thank you for your attention.


Solution

  • I figured out the problem. The API states:

    enter image description here

    So I started to suspect that the post message operation from HubSpot is not the same as the one performed by the postman example provided in Slack's website, which only required chat:write:user scope. It turns out that HubSpot post message requires scope chat:write:bot instead.