You need to sign in to do that
Don't have an account?
Shivakrishna Komma
Getting "An internal server error has occurred" for Oauth2 Password flow
Hello,
I'm new to salesforce. I was trying to login using OAuth2 password flow using a RESTClient stub (in java), and getting the Internal Server Error. the url which I am trying is eu6.salesforce.com.
Can anybody help, in resolvoing the issue.
Thanks.
I'm new to salesforce. I was trying to login using OAuth2 password flow using a RESTClient stub (in java), and getting the Internal Server Error. the url which I am trying is eu6.salesforce.com.
Can anybody help, in resolvoing the issue.
Thanks.
To make an http call and get a call back with the token , you need to make a Connected App in your instance and get CLIENTID and CLIENTSECRET from the connectd App.
Use this Code
Hope it will help.
public class RestApiTest {
static final String USERNAME = "xxxxxxxxxxxxxxxxxxx";
static final String PASSWORD = "xxxxxxxxxxxxxxxxxxx";
static final String LOGINURL = "https://login.salesforce.com";
static final String GRANTSERVICE = "/services/oauth2/token?grant_type=password";
static final String CLIENTID = "xxxxxxxxxxxxxxxxxxx";
static final String CLIENTSECRET = "xxxxxxxxxxxxxxxxxxx";
private static String REST_ENDPOINT = "/services/data";
private static String API_VERSION = "/v32.0";
private static String baseUri;
private static Header oauthHeader;
private static Header prettyPrintHeader = new BasicHeader("X-PrettyPrint", "1");
private static String leadId;
private static String leadFirstName;
private static String leadLastName;
private static String leadCompany;
public static void main(String[] args) {
HttpClient httpclient = HttpClientBuilder.create().build();
// Assemble the login request URL
String loginURL = LOGINURL +
GRANTSERVICE +
"&client_id=" + CLIENTID +
"&client_secret=" + CLIENTSECRET +
"&username=" + USERNAME +
"&password=" + PASSWORD;
// Login requests must be POSTs
HttpPost httpPost = new HttpPost(loginURL);
HttpResponse response = null;
try {
// Execute the login POST request
response = httpclient.execute(httpPost);
} catch (ClientProtocolException cpException) {
cpException.printStackTrace();
} catch (IOException ioException) {
ioException.printStackTrace();
}
// verify response is HTTP OK
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
System.out.println("Error authenticating to Force.com: " + statusCode);
// Error is in EntityUtils.toString(response.getEntity())
return;
}
String getResult = null;
try {
getResult = EntityUtils.toString(response.getEntity());
System.out.println("Result From Server: "+getResult);
} catch (IOException ioException) {
ioException.printStackTrace();
}
JSONObject jsonObject = null;
String loginAccessToken = null;
String loginInstanceUrl = null;
try {
jsonObject = (JSONObject) new JSONTokener(getResult).nextValue();
loginAccessToken = jsonObject.getString("access_token");
loginInstanceUrl = jsonObject.getString("instance_url");
System.out.println("loginAccessToken: "+loginAccessToken+" "+"loginInstanceUrl: "+loginInstanceUrl);
} catch (JSONException jsonException) {
jsonException.printStackTrace();
}
baseUri = loginInstanceUrl + REST_ENDPOINT + API_VERSION;
oauthHeader = new BasicHeader("Authorization", "OAuth " + loginAccessToken);
System.out.println("oauthHeader1: " + oauthHeader);
System.out.println(response.getStatusLine());
System.out.println("Successful login");
System.out.println(" instance URL: " + loginInstanceUrl);
System.out.println(" access token/session ID: " + loginAccessToken);
System.out.println("baseUri: " + baseUri);
// Run codes to query, insert, update and delete records in Salesforce using REST API
//queryLeads();
// createLeads();
//updateLeads();
// deleteLeads();
// release connection
httpPost.releaseConnection();
}