0

I've created a Azure Web App Bot and added a OAuth Connection Setting which takes the user to Salesforce. Everything works well, I'm able to authenticate the user through my bot and also, I can get the access token from Salesforce.

Problem Can someone help me to get the user information from Salesforce? Because, I am able to get the access token alone and not sure, how to get the user id from Salesforce.

I've written the below code,

var salesforce = {};

salesforce.signin = (connector, session, callback) => {
    builder.OAuthCard.create(connector,
        session,
        connectionName,
        "Sign in to your Salesforce account",
        "Sign in",
        (createSignInErr, createSignInRes) => {
            if (createSignInErr) {
                callback({
                    status: 'failure',
                    data: createSignInErr.message
                });
                return;
            }

            callback({
                status: 'success',
                data: createSignInRes
            });
        });
};

salesforce.getUserToken = (connector, session, callback) => {
    connector.getUserToken(session.message.address,
        connectionName,
        undefined,
        (userTokenErr, userTokenResponse) => {
            if (userTokenErr) {
                callback({
                    status: 'failure',
                    data: userTokenErr.message
                });
                return;
            }

            callback({
                status: 'success',
                data: userTokenResponse
            });
        });
};

salesforce.accessToken = (connector, session, callback) => {
    salesforce.getUserToken(connector, session, (userTokenResponse) => {
        if (userTokenResponse.status == 'failure') {
            // If the user token is failed, then trigger the sign in card to the user.

            salesforce.signin(connector, session, (signinResponse) => {
                // If the sign in is failed, then let the user know about it.

                if (signinResponse.status == 'failure') {
                    session.send('Something went wrong, ', signinResponse.message);
                    return;
                }

                // If the sign in is success then get the user token and send it to the user.
                salesforce.getUserToken(connector, session, (newUserTokenResponse) => {
                    if (newUserTokenResponse.status == 'failure') {
                        session.send('Something went wrong, ', newUserTokenResponse.message);
                        return;
                    }

                    callback(newUserTokenResponse);
                    return;
                });
            });
        }

        callback(userTokenResponse);
    });
};

I can get the userTokenResponse here. But I need Salesforce user id so that I can start interacting with Salesforce behalf of the user.

moustacheman
  • 1,424
  • 4
  • 21
  • 47
  • The problem itself is only Salesforce related, right? The botframework aspect could be ignored for a while? – Ferdinand Fejskid Aug 30 '18 at 17:42
  • I don't think it is related to Salesforce. Generally, when we do a OAuth with Salesforce, it return `accessToken`, `userId`, `refreshToken`, and `instaceUrl`, but here I just get the accessToken and not sure how to get the userId who have been authenticated. I was trying to find out the methods here, https://github.com/Microsoft/BotBuilder/blob/botbuilder%403.15.0/Node/core/src/bots/ChatConnector.ts, but no luck. – moustacheman Aug 30 '18 at 17:48

2 Answers2

0

If you have only OAuth access token you may query details about the user by invoking http GET against:

https://login.salesforce.com/services/oauth2/userinfo for PROD or https://test.salesforce.com/services/oauth2/userinfo for sandbox

Add only Authorization: Bearer Y0UR0AUTHTOKEN to the header of the http GET request.

Based on my recent test the result returned from the server looks like:

{
    "sub": "https://test.salesforce.com/id/[organizationid]/[userId]",
    "user_id": "000",
    "organization_id": "000",
    "preferred_username": "me@mycompany.com",
    "nickname": "myNick",
    "name": "name lastname",
    "urls": {
        ...
    },
    "active": true,
    "user_type": "STANDARD",
    ...
}
  • I know what is the api is used to get the user information. My problem here is, I am not sure how to get the `[userId]` (which you have used in your code) from the `const connector = new builder.ChatConnector`. For eg., to get the userToken, we call `connector.getUserToken` – moustacheman Aug 30 '18 at 20:31
  • I did not use userid in my code. The userid is part of the result in my sample and in the request is used only the authorization token – Ferdinand Fejskid Aug 31 '18 at 08:15
0

You don't need a userId to get the user information where an accessToken is enough. I've installed jsforce and used the below code to get the identity information.

Solved by doing,

    const jsforce = require('jsforce');
    var connection = new jsforce.Connection({
            instanceUrl: instanceUrl,
            sessionId: accessToken
        });

        connection.identity((error, response) => {
            if(error) {
                callback({
                    status: 'failure',
                    message: error.message
                });
                return;
            }

            callback({
                staus: 'success',
                data: response
            });
        });
moustacheman
  • 1,424
  • 4
  • 21
  • 47