function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
harsha B R 9harsha B R 9 

How to get access token in username and Password authentication

Hello,

I am using the basic username and password authentication mechanism for integration.

Which is the endpoint URL that needs to set as endpointurl to get the access token and inturn data?

Please check the below code and suggest if any changes are required.

public class AccessToken {
    
    
       private Static final String username = 'test@test.com';
       private static final String password = 'XXXsecuritytoken';
    
      public class deserializeResponse
       {
         public String id;
          public String access_token;
       }
    
   public static void ReturnAccessToken ()
   {
        String reqbody = 'grant_type=password&username='+username+'&password='+password;
         Http h = new Http();
          HttpRequest req = new HttpRequest();
          req.setBody(reqbody);
          req.setMethod('GET');
        req.setEndpoint('https://test.salesforce.com/services/oauth2/token');
          HttpResponse res = h.send(req);
        system.debug('*** Response Body***'+res.getbody() );
         deserializeResponse resp1 = (deserializeResponse)JSON.deserialize(res.getbody(),deserializeResponse.class);
         system.debug('@@@@access_token@@'+resp1 );
          //return resp1.access_token;
   }
}

Any help is much appreciated.

Thanks & Regards,
Harsha BR
 
Best Answer chosen by harsha B R 9
GovindarajGovindaraj
This is the URL to get security token.

https://test.salesforce.com/services/oauth2/token

All Answers

GovindarajGovindaraj
Hi Harsha,

Hope you setup connected app and remote site settings and all :)

Above code looks fine but one suggestion. Did you hear about 'Named Credentials' ?

URL-1: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_named_credentials.htm
URL-2 : https://sfdcfanboy.com/2018/01/15/named-credentials-setup/

Using that we can share the Username and Password to an external system safely.

Thanks,
Govindaraj.S
harsha B R 9harsha B R 9
Hi Govindaraj,

Thanks for your response.

I am not using connected App.

The external system wants to send the data to Salesforce. We are not using OAuth authentication. We want to use the basic username and password authentication.

Having said that, my question is which is the endpoint to hit to the access token by passing username and password.

I need to give above endpoint to the external system.

Thanks & Regards,
Harsha BR
GovindarajGovindaraj
This is the URL to get security token.

https://test.salesforce.com/services/oauth2/token
This was selected as the best answer
harsha B R 9harsha B R 9
I am using the above URL in the code.But ,it is not working without clientId and client_secret.

We using only username and password.Could you please guide me through the code.
GovindarajGovindaraj
Hi Harsha,

As far as i know, We can't directly fetch information from Salesforce by passing username and password.

Also, shall i know the difficulty for using oAuth from your side ?

Thanks,
Govindaraj.S