Deprecated: Optional parameter $list declared before required parameter $is_script is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php on line 21

Deprecated: Optional parameter $register declared before required parameter $footer_or_media is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php on line 45

Deprecated: Optional parameter $register declared before required parameter $footer_or_media is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php on line 104

Deprecated: Optional parameter $expire declared before required parameter $path is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_functions.php on line 54

Deprecated: Optional parameter $depth declared before required parameter $output is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/themes/entaro/inc/classes/megamenu.php on line 155

Deprecated: Optional parameter $depth declared before required parameter $output is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/themes/entaro/inc/classes/mobilemenu.php on line 147

Deprecated: Optional parameter $args declared before required parameter $wp_customize is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/extensions/customizer/extension_customizer.php on line 583

Deprecated: Optional parameter $args declared before required parameter $wp_customize is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/extensions/customizer/extension_customizer.php on line 606

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1893
{"id":1251,"date":"2018-03-06T18:25:21","date_gmt":"2018-03-06T18:25:21","guid":{"rendered":"http:\/\/www.salesforcenextgen.com\/?p=1251"},"modified":"2020-12-28T19:36:29","modified_gmt":"2020-12-28T19:36:29","slug":"how-to-authenticate-user-in-salesforce-using-rest-api","status":"publish","type":"post","link":"https:\/\/salesforcenextgen.com\/how-to-authenticate-user-in-salesforce-using-rest-api\/","title":{"rendered":"How to Authenticate user in Salesforce Using REST Api?"},"content":{"rendered":"

How to Authenticate user in Salesforce Using REST Api?<\/strong><\/p>\n

Salesforce uses oAuth protocol to allow application users to access the data in salesforce securely without exposing Username and password of a particular user.<\/p>\n

\"How<\/p>\n

But before we could make a REST Api call, we need to authenticate our app with salesforce, by making it a connected app. To make an App as connected app follow the steps given in this post<\/a>.<\/p>\n

There are several OAuth endpoints provided by Salesforce, depending upon our requirement and resources we need to choose an authentication flow from the below<\/p>\n

    \n
  1. Web server Flow<\/strong><\/li>\n
  2. User-Agent Flow<\/strong><\/li>\n
  3. Username-Password Flow<\/strong><\/li>\n<\/ol>\n

    Once we have successfully authenticated our connected app user, salesforce provide us with an access token, which are further utilised to make authenticated REST Api calls.<\/p>\n

    In this post we are going to discuss the Web Server Authentication Flow<\/strong>.<\/p>\n

    This authentication flow is used by the application which are hosted over a secure server. In this flow client application redirects the user to another web server i.e. an authorization server where the user get generates an authorize code, now this authorise code is used to fetch the access token from the Salesforce, see the flow diagram below.\"How<\/p>\n

    Step wise breakdown of the authentication flow:<\/p>\n

      \n
    1. The endpoint for the web server authentication flow is : https:\/\/login.salesforce.com\/services\/oauth2\/authorize<\/li>\n<\/ol>\n

      Parameter\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Description<\/p>\n

      response_type Must be code for this authentication flow.<\/p>\n

      client_id\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 The Consumer Key from the connected app definition.<\/p>\n

      redirect_uri\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 The Callback URL from the connected app definition.<\/p>\n

      Example authorization url will look something like this:<\/p>\n

      https:\/\/login.salesforce.com\/services\/oauth2\/authorize?response_type=code<\/p>\n

      &client_id=3MVG9lKcPoNINVBIPJjdw1J9LLM82HnFVVX19KY1uA5mu0QqEWhqKpoW3svG3X<\/p>\n

      HrXDiCQjK1mdgAvhCscA9GE&redirect_uri=https%3A%2F%2Fwww.mysite.com%2F<\/p>\n

      code_callback.jsp&state=mystate<\/p>\n

        \n
      1. User login on this url with their credentials and will interact with the endpoint directly and therefore application never sees user\u2019s credentials. After successful log in, user would be asked to authorise the app and this step is skipped if already authorised before.<\/li>\n
      2. After confirmation from Salesforce the client application will get authorized and end-user\u2019s Web browser would be redirected to the callback URL specified by the\u00a0redirect_uri\u00a0parameter. The authorization information is appended by Salesforce to the redirect URL with the following values:<\/li>\n<\/ol>\n

        Parameters\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Description<\/p>\n

        code\u00a0\u00a0\u00a0\u00a0\u00a0 Authorization code the consumer must use to obtain the access and refresh tokens.<\/p>\n

        state\u00a0\u00a0\u00a0\u00a0\u00a0 The state value that was passed in as part of the initial request, if applicable.<\/p>\n

        E.g.<\/p>\n

        https:\/\/www.mysite.com\/authcode_callback?code=aWekysIEeqM9PiT<\/p>\n

        hEfm0Cnr6MoLIfwWyRJcqOqHdF8f9INokharAS09ia7UNP6RiVScerfhc4w%3D%3D<\/p>\n

          \n
        1. Application is able to extract the aut code from the return url and then use this code to further fetch the access token and refresh token from the salesforce token request end point.<\/li>\n<\/ol>\n

          End point: https:\/\/login.salesforce.com\/services\/oauth2\/token<\/p>\n

          Parameter\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Description<\/p>\n

          grant_type\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Value must be authorization_code for this flow.<\/p>\n

          client_secret\u00a0\u00a0\u00a0\u00a0\u00a0 The Consumer Secret from the connected app definition.<\/p>\n

          client_id\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 The Consumer Key from the connected app definition.<\/p>\n

          redirect_uri\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 The Callback URL from the connected app definition.<\/p>\n

          code\u00a0\u00a0\u00a0\u00a0\u00a0 Authorization code the consumer must use to obtain the access and refresh tokens<\/p>\n

          E.g.<\/p>\n

          POST \/services\/oauth2\/token HTTP\/1.1<\/p>\n

          Host: login.salesforce.com<\/p>\n

          grant_type=authorization_code&code=aPrxsmIEeqM9PiQroGEWx1UiMQd95_5JUZ<\/p>\n

          VEhsOFhS8EVvbfYBBJli2W5fn3zbo.8hojaNW_1g%3D%3D&client_id=3MVG9lKcPoNI<\/p>\n

          NVBIPJjdw1J9LLM82HnFVVX19KY1uA5mu0QqEWhqKpoW3svG3XHrXDiCQjK1mdgAvhCs<\/p>\n

          cA9GE&client_secret=1955279925675241571&<\/p>\n

          redirect_uri=https%3A%2F%2Fwww.mysite.com%2Fcode_callback.jsp<\/p>\n

            \n
          1. Successful request will return the following information from the salesforce.<\/li>\n<\/ol>\n

            Parameters\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Description<\/p>\n

            access_token\u00a0\u00a0\u00a0 Access token that acts as a session ID that the application uses for making requests. This token should be protected as though it were user credentials.<\/p>\n

            refresh_token\u00a0 Token that can be used in the future to obtain new access tokens.<\/p>\n

            instance_url\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Identifies the Salesforce instance to which API calls should be sent.<\/p>\n

            id\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Identity URL that can be used to both identify the user as well as query for more information about the user. Can be used in an HTTP request to get more information about the end user.<\/p>\n

            E.g. of the Raw JSON response<\/p>\n

            {“id”:”https:\/\/login.salesforce.com\/id\/00Dx0000000BV7z\/005x00000012Q9P”,<\/p>\n

            “issued_at”:”1278448101416″,<\/p>\n

            “refresh_token”:”5Aep8614iLM.Dq661ePDmPEgaAW9Oh_L3JKkDpB4xReb54_<\/p>\n

            pZebnUG0h6Sb4KUVDpNtWEofWM39yg==”,<\/p>\n

            “instance_url”:”https:\/\/***yourInstance***.salesforce.com\/”,<\/p>\n

            “signature”:”CMJ4l+CCaPQiKjoOEwEig9H4wqhpuLSk4J2urAe+fVg=”,<\/p>\n

            “access_token”:”00Dx0000000BV7z!AR8AQP0jITN80ESEsj5EbaZTFG0R<\/p>\n

            NBaT1cyWk7TrqoDjoNIWQ2ME_sTZzBjfmOE6zMHq6y8PIW4eWze9JksNEkWUl.Cju7m4″}<\/p>\n

              \n
            1. The information received in the previous step is further used to make the authenticated REST Api calls.<\/li>\n<\/ol>\n

              This is the web server Authentication Flow used by Salesforce<\/p>\n

              Further we will discuss about the other two authentication flow.<\/p>\n

              Also, Have a look at the below resources:<\/strong><\/h1>\n
                \n
              1. \n

                Best Salesforce Interview Questions book with Apex and Visualforce concept explained<\/a><\/h3>\n<\/li>\n<\/ol>\n

                Also, Have a look at the below learning resources:<\/strong><\/h1>\n
                  \n
                1. \n

                  SOQL (Salesforce Object Query Language)<\/a><\/strong><\/em><\/h3>\n<\/li>\n
                2. \n

                  Apex Trigger Best Practices and the Trigger Framework<\/a><\/strong><\/em><\/h3>\n<\/li>\n
                3. \n

                  Salesforce Interview Question and Answers Part 2<\/a><\/strong><\/em><\/h3>\n<\/li>\n
                4. \n

                  Salesforce Interview Questions on Test Class<\/a><\/strong><\/em><\/h3>\n<\/li>\n
                5. \n

                  Salesforce-lightning-interview-questions-2018<\/cite><\/span><\/a><\/strong><\/em><\/h3>\n<\/li>\n<\/ol>\n

                  \u00a0 \u00a0 \u00a06.\u00a0Salesforce Interview Questions Batch Class\u00a0<\/a><\/strong><\/em><\/h3>\n","protected":false},"excerpt":{"rendered":"

                  How to Authenticate user in Salesforce Using REST Api? Salesforce uses oAuth protocol to allow application users to access the data in salesforce securely without exposing Username and password of a particular user. But before we could make a REST Api call, we need to authenticate our app with salesforce, by making it a connected …
                  Continue reading How to Authenticate user in Salesforce Using REST Api?<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":1257,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[178],"tags":[195,196,198,211,210,212,201,203,202,204,206,207,205,208,209,200,199,197],"class_list":["post-1251","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-how-to-do-in-salesforce","tag-how-to-authenticate-user-in-salesforce-using-rest-api","tag-how-to-create-authenticated-website-user-in-salesforce","tag-oauth-web-server-flow-salesforce","tag-salesforce-authorization-bearer-token","tag-salesforce-authorization-endpoint","tag-salesforce-authorization-url","tag-salesforce-oauth-authentication","tag-salesforce-oauth-authentication-example","tag-salesforce-oauth-authentication-failure","tag-salesforce-oauth-callback-url","tag-salesforce-oauth-redirect-uri","tag-salesforce-oauth-redirect-url","tag-salesforce-oauth-redirect_uri_mismatch","tag-salesforce-oauth-rest-api","tag-salesforce-rest-oauth-token","tag-salesforce-trusted-ip-range","tag-salesforce-trusted-ip-range-for-oauth-web-server-flow","tag-web-server-flow-salesforce"],"_links":{"self":[{"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/posts\/1251","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/comments?post=1251"}],"version-history":[{"count":3,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/posts\/1251\/revisions"}],"predecessor-version":[{"id":2184,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/posts\/1251\/revisions\/2184"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/media\/1257"}],"wp:attachment":[{"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/media?parent=1251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/categories?post=1251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/tags?post=1251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}