"Error: Cannot read property 'id' of null" with OAuth enabled Bolt app
Description
After successfully going through the oauth flow and storing the token, when any events are emitted the following error is produced:
[WARN] bolt-app Authorization of incoming event did not succeed. No listeners will be called.
[ERROR] bolt-app Error: Cannot read property 'id' of null
Any guidance would be great as I have been pulling my hair out because I store the object exactly as it presented so I do not understand what is causing this.
This is the installation object being returned:
{
"_id":"61f245ddb181bc207d0980e3",
"team":{
"id":"T030G1xxxx",
"name":"DevTest"
},
"enterprise":null,
"user":{
"token":null,
"scopes":null,
"id":"U030SBxxxx"
},
"tokenType":"bot",
"isEnterpriseInstall":false,
"appId":"xxxxxxx",
"authVersion":"v2",
"bot":{
"scopes":[
"app_mentions:read",
],
"token":"xoxb-xxxxx",
"userId":"xxxxx",
"id":"xxxxx"
},
"incomingWebhook":{
"url":"https://hooks.slack.com/services/xxxx/xxxx/xxxx",
"channel":"@russ",
"channelId":"D02VD48T5ST",
"configurationUrl":"https://devtest-ui83276.slack.com/services/B030BCE3QAZ"
}
}
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:
What you expected to happen
Actual result:
What actually happened
Attachments:
Logs, screenshots, screencast, sample project, funny gif, etc.
@seratch would you be able to take a look at this issue over here? <3 Its 2am my time and I'm dying to figure this out before I go to bed haha
@6lick I'm pretty sure that this issue is the same with https://github.com/slackapi/bolt-js/issues/1265#issuecomment-1010625799 If you upgrade @slack/bolt and its underlying @slack/oauth to the latest versions, this issue should be resolved.
Hmmm that changed my error to Error: An API error occurred: not_authed
output of npm list
@slack/[email protected]
│ ├─┬ @slack/[email protected]
│ │ └── @types/[email protected] deduped
│ ├─┬ @slack/[email protected]
The library versions look good.
The "not_authed" error means that your API call does not have any token in the request.
It's supposed to have a bot token as installation.bot.token in the returned installation data. Your data has the token as top-level token, which is not compatible with the latest OAuth module.
If you're unable to change the data structure in database, you can convert the data to a compatible one in fetchInstallation method. Refer to https://github.com/slackapi/node-slack-sdk/blob/%40slack/oauth%402.4.0/packages/oauth/src/index.ts#L172-L174 to know the conversation under the hood.
I will be away from keyboard for a while. I hope you'll resolve your issue soon!
Ok- could you just sanity check me cause it looks like it is in x.bot.token route
{
_id: new ObjectId("61f245ddb181bc207d0980e3"),
team: { id: 'T030G1Y84E8', name: 'DevTest' },
enterprise: null,
user: { token: null, scopes: null, id: 'xxx' },
tokenType: 'bot',
isEnterpriseInstall: false,
appId: 'xxx',
authVersion: 'v2',
bot: {
scopes: [
'app_mentions:read',
'channels:history',
],
token: 'xoxb-xxxx',
userId: 'U0313FG1V24',
id: 'B02VD4ZFC6B'
},
incomingWebhook: {
url: 'https://hooks.slack.com/services/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',
channel: '@russ',
channelId: 'D02VD48T5ST',
configurationUrl: 'https://devtest-ui83276.slack.com/services/B030BCE3QAZ'
}
}
Ah, yes, you're right. My above suggestion was wrong. Sorry about that.
I cannot tell anything further with given information. The situation can depend on how you implement fetchInstallation in InstallationStore. The triage-bot example app also uses MongoDB to store installations and its fetchInstallation method returns object._doc property. Are you doing the same? If not, changing your code may help.
If you need more help from others, please share a minimal working example to reproduce your issue 👋
Ok cool thanks for sharing that I made it pretty similar- am still seeing the installation in the logs but am still getting the same error
Not exactly the cleanest code but it seems like it should work
const app = new App({
signingSecret: process.env.SLACK_SIGNING_SECRET,
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
stateSecret: 'my-secret',
socketMode: true,
appToken: process.env.SLACK_APP_TOKEN,
scopes: ['app_mentions:read','channels:history','channels:manage','channels:read','chat:write', 'commands','groups:read', 'groups:write', 'im:read', 'im:write', 'incoming-webhook', 'mpim:read', 'mpim:write', 'pins:write', 'users:read', 'users:read.email', 'workflow.steps:execute' ],
installationStore: {
storeInstallation: async (installation) => {
console.log("^^^^^^^^^" + JSON.stringify(installation))
// change the lines below so they save to your database
if (installation.isEnterpriseInstall && installation.enterprise !== undefined) {
// support for org-wide app installation
try {
await client.connect();
const database = client.db('slack_store');
const install_col = database.collection('install_info');
const resp = await install_col.insertOne(installation);
console.log(resp);
return resp
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
// return await database.set(installation.enterprise.id, installation);
}
if (installation.team !== undefined) {
// single team app installation
try {
await client.connect();
const database = client.db('slack_store');
const install_col = database.collection('install_info');
let teamData = installation.team
teamData = Object.assign(teamData, installation)
delete teamData.team // we already have this information from the assign above
delete teamData.user.token // we dont want a user token, if the scopes are requested
// Do an upsert so that we always have just one document per team ID
let resp = await install_col.findOneAndUpdate({ id: teamData.id }, {$set: teamData}, { upsert: true })
// const resp = await install_col.insertOne(installation);
console.log("$$$$$" + JSON.stringify(resp));
return resp
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
// return await database.set(installation.team.id, installation);
}
throw new Error('Failed saving installation data to installationStore');
},
fetchInstallation: async (installQuery) => {
// change the lines below so they fetch from your database
if (installQuery.isEnterpriseInstall && installQuery.enterpriseId !== undefined) {
// org wide app installation lookup
try {
await client.connect();
const database = client.db('slack_store');
const install_col = database.collection('install_info');
const resp = await install_col.findOne({"enterprise.id": installQuery.enterpriseId});
console.log(resp);
return resp
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
// return await database.get(installQuery.enterpriseId);
}
if (installQuery.teamId !== undefined) {
// single team app installation lookup
console.log("fetching token" + JSON.stringify(installQuery))
try {
await client.connect();
const database = client.db('slack_store');
const install_col = database.collection('install_info');
const resp = await install_col.findOne({ id: installQuery.teamId })
console.log(resp);
return resp
// const resp = await install_col.findOne({"team.id": installQuery.teamId});
// return resp
} catch(err){
console.log(err)
}finally {
// Ensures that the client will close when you finish/error
await client.close();
}
// return await database.get(installQuery.teamId);
}
throw new Error('Failed fetching installation');
},
deleteInstallation: async (installQuery) => {
// change the lines below so they delete from your database
if (installQuery.isEnterpriseInstall && installQuery.enterpriseId !== undefined) {
// org wide app installation deletion
try {
await client.connect();
const database = client.db('slack_store');
const install_col = database.collection('install_info');
const resp = await install_col.deleteOne({"enterprise.id": installQuery.enterpriseId});
console.log(resp);
return resp
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
// return await database.delete(installQuery.enterpriseId);
}
if (installQuery.teamId !== undefined) {
// single team app installation deletion
try {
await client.connect();
const database = client.db('slack_store');
const install_col = database.collection('install_info');
const resp = await install_col.deleteOne({"team.id": installQuery.teamId});
console.log(resp);
return resp
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
// return await database.delete(installQuery.teamId);
}
// throw new Error('Failed to delete installation');
},
},
})
I have made some progress by including context and adding token: context.botToken to the client calls but there are still issues like app.say() not sure how the token is supposed to get there.
👋 It looks like this issue has been open for 30 days with no activity. We'll mark this as stale for now, and wait 10 days for an update or for further comment before closing this issue out. If you think this issue needs to be prioritized, please comment to get the thread going again! Maintainers also review issues marked as stale on a regular basis and comment or adjust status if the issue needs to be reprioritized.
As this issue has been inactive for more than one month, we will be closing it. Thank you to all the participants! If you would like to raise a related issue, please create a new issue which includes your specific details and references this issue number.
facing a similar issue