bolt-js icon indicating copy to clipboard operation
bolt-js copied to clipboard

Working with user tokens

Open mrafiqi21 opened this issue 4 years ago • 10 comments

Description

Describe your issue here. I am intending to use the api files.sharedPublicURL but get an error 'not_allowed_token_type'. The api requires user token access. I have already added the required scopes for the api in OAuth and Permissions. What am I missing here?

client.files.sharedPublicURL({file: fileID})

This how I initialise the bolt app.

const app = new App({
    token: process.env.BOT_TOKEN,
    socketMode: true,
    appToken: process.env.APP_TOKEN,
    // logLevel: LogLevel.DEBUG
});

What type of issue is this? (place an x in one of the [ ])

  • [ ] bug
  • [ ] enhancement (feature request)
  • [x] question
  • [ ] documentation related
  • [ ] example code related
  • [ ] testing related
  • [ ] discussion

Requirements (place an x in each of the [ ])

  • [x] I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • [x] I've read and agree to the Code of Conduct.
  • [x] I've searched for any related issues and avoided creating a duplicate issue.

Bug Report

Filling out the following details about bugs will help us solve your issue sooner.

Reproducible in:

package version:

node version:

OS version(s):

Steps to reproduce:

1. 2. 3.

Expected result:

Expected to successfully make the file public

Actual result:

UnhandledPromiseRejectionWarning: Error: An API error occurred: not_allowed_token_type

Attachments:

Logs, screenshots, screencast, sample project, funny gif, etc.

mrafiqi21 avatar Feb 23 '22 07:02 mrafiqi21

Hey @mrafiqi21 👋🏼

As you've pointed out, this particular method requires a User token. Bolt defaults all method calls to use the token that is passed in during the App initialization (in this case, your BOT_TOKEN), which is why you're receiving the not_allowed_token_type error.

Add the required scope to the User token in your app configuration (files:write) and store it with the rest of your environment variables (process.env.USER_TOKEN).

When invoking sharedPublicURL, pass the User token as an override:

await client.files.sharedPublicURL({ token: process.env.USER_TOKEN, file: fileID });

Let us know if this sorts out the issue!

misscoded avatar Feb 24 '22 01:02 misscoded

Hi @misscoded

This does solve the error. Thank you. Is OAuth user token same for all users of the workspace? Me and a contributor to the app have different user tokens showing up in OAuth and Permission. How do I gain access to user tokens for a particular user?

This is what I am getting when a different user hits the server (I have checked the fileID, it exists for that user, I think the issue is because of wrong user token). Could you please provide some details on accessing user tokens?

data: { ok: false, error: 'file_not_found', response_metadata: { scopes: [Array], acceptedScopes: [Array] } }

P.s I am using socket mode without any redirect URLs.

mrafiqi21 avatar Feb 25 '22 06:02 mrafiqi21

Hi @mrafiqi21, to manage multiple user tokens in your app, the app needs to serve the URLs for the OAuth flow, plus all the tokens are issued by following the OAuth flow. With that, InstallationStore manages the tokens in database (the default implementation is local file system based one) and your Bolt app can access the token via context.userToken.

Refer to the document section for knowing how to configure your app for it: https://slack.dev/bolt-js/concepts#authenticating-oauth

Also, even when you use Socket Mode, the same app can serve the OAuth flow endpoints. For the OAuth flow, the app must serve internet-facing public URLs for interactions with installers in web browser while it receives event data from Slack API server through WebSocket connections (=Socket Mode).

seratch avatar Feb 25 '22 07:02 seratch

Hey @seratch Thank you for the quick reply. In case of an app running in socket mode, what is the install path of the application? How to set up an endpoint in bolt to serve public URL's?

mrafiqi21 avatar Feb 25 '22 08:02 mrafiqi21

@mrafiqi21 In localhost, you should be able to access http://localhost:3000/slack/install if you properly configure the OAuth settings (see the document link I've shared above). For serving public endpoints with your app, ngrok is a common easy way to forward public endpoint requests to your localhost app. Refer to this getting started guide and its links for more details: https://slack.dev/bolt-js/tutorial/getting-started-http

seratch avatar Feb 25 '22 08:02 seratch

@seratch I have used an ngrok url which points to my localhost app as the redirect URI. The path ../slack/install gives me this error:

App could not be installed. Invalid permissions requested invalid_scope

This is how I have initialised the app. All the scopes I have added here are from the manifest file of my app. Anything that I may be missing?

const app = new App({
    socketMode: true,
    appToken: process.env.APP_TOKEN,
    clientId: process.env.SLACK_CLIENT_ID,
    clientSecret: process.env.SLACK_CLIENT_SECRET,
    stateSecret: 'my-secret',
    scopes: ['channels:manage', 'channels:read', 'chat:write', 'commands', 'files:read', 'groups:write', 'im:write', 'mpim:write', 'files:write'],
    installationStore: new FileInstallationStore(),
    installerOptions: {
      userScopes: ['channels:write:',' chat:write:', 'groups:write:', 'im:write:', 'mpim:write:',' files:read:', 'files:write:'],
      directInstall: true
    }
});

mrafiqi21 avatar Feb 25 '22 08:02 mrafiqi21

@mrafiqi21

'groups:write:', 'im:write:', 'mpim:write:',' files:read:', 'files:write:'

These look invalid. The trailing : should be removed. Specifically, groups:write: needs to be groups:write. It's the same for others in the userScopes.

seratch avatar Feb 25 '22 09:02 seratch

@seratch Yes, missed those. Clicking allow to all permissions is throwing me off to a 'not found' page. The url params have 'code' and 'state'. Full path for /slack/oauth_redirect including domain name is added in the Redirect URI in OAuth and Permissions.

mrafiqi21 avatar Feb 25 '22 09:02 mrafiqi21

With given information, I don't have any suggestions. I hope that you'll figure the cause out soon!

seratch avatar Feb 25 '22 09:02 seratch

I was able to solve this by putting only one path with /slack/oauth_redirect in the Redirect URI in OAuth and Permissions and removing all other paths. Closing the issue.

mrafiqi21 avatar Mar 03 '22 10:03 mrafiqi21