Skip to content

Instantly share code, notes, and snippets.

@Tanver-Hasan
Last active November 13, 2020 16:57
Show Gist options
  • Save Tanver-Hasan/939276926bade328ef177762eebe8535 to your computer and use it in GitHub Desktop.
Save Tanver-Hasan/939276926bade328ef177762eebe8535 to your computer and use it in GitHub Desktop.
SLACK OIDC connection with v2 Oauth2.0 API
@Tanver-Hasan
Copy link
Author

User scope is defined directly in the Authorization URL : Ex: user_scope=identity.basic

@Tanver-Hasan
Copy link
Author

Calling user API with nested accesToken in authed_user object:


function fetchUserProfile(accessToken, context, callback) {
  request.get(
    {
      url: 'https://slack.com/api/users.identity',
      qs: {
        token: context.authed_user.access_token,
      },
    },
    (err, resp, body) => {
      if (err) {
        return callback(err);
      }

      if (resp.statusCode !== 200) {
        return callback(new Error(`[Response code: ${resp.statusCode}] ${body}`));
      }

      let bodyParsed;
      try {
        bodyParsed = JSON.parse(body);
      } catch (jsonError) {
        return callback(new Error(body));
      }

      if (!bodyParsed.ok) {
        return callback(new Error(body));
      }

      const profile = {
        ...bodyParsed.user,
        user_id: [bodyParsed.team.id, bodyParsed.user.id].join('-'),
        team: bodyParsed.team,
      };

      callback(null, profile);
    }
  );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment