Reputation: 10312
I am trying to integrate a software written in java to be able to fetch some data from Salesforce sites of multiple organizations.
I am new to Salesforce and this is what I have done so far. I have created a connected app in my developer Salesforce account and wrote this piece of code based on what's available in blogs :
private static final String CLIENT_ID = "my-connected-app-client-id";
private static final String CLIENT_SECRET = "my-connected-app-client-secret";
private static final String REDIRECT_URI = "https://localhost:8443/callback";
private static final String ENVIRONMENT = "https://login.salesforce.com";
private static String tokenUrl = null;
private static final String username = "my-developer-sf-login";
private static final String password = "my-developer-sf-password";
private String accessToken = null;
private String instanceUrl = null;
private List<BasicNameValuePair> getParams(){
ArrayList<BasicNameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("grant_type", "password"));
params.add(new BasicNameValuePair("client_id", CLIENT_ID));
params.add(new BasicNameValuePair("client_secret", CLIENT_SECRET));
params.add(new BasicNameValuePair("redirect_uri", REDIRECT_URI));
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
return params;
}
@Override
public boolean connect() throws ConnectException {
System.out.println("Getting a token");
tokenUrl = ENVIRONMENT + "/services/oauth2/token";
HttpClient httpclient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(tokenUrl);
try {
post.setEntity(new UrlEncodedFormEntity(getParams(), "UTF-8"));
HttpResponse response = httpclient.execute(post);
String responseStr = EntityUtils.toString(response.getEntity());
HashMap loginResponse = new ObjectMapper().readValue(responseStr, HashMap.class);
accessToken = loginResponse.get("access_token").toString();
instanceUrl = loginResponse.get("instance_url").toString();
System.out.println("We have an access token: " + accessToken + "\n" + "Using instance " + instanceUrl + "\n\n");
return accessToken != null && instanceUrl != null;
} catch (ClientProtocolException e) {
throw new ConnectException("Unable to connect to salesforce", e);
} catch (IOException e) {
throw new ConnectException("Unable to connect to salesforce", e);
} finally {
post.releaseConnection();
}
}
With the access tokens got in the above program, I am able to hit the REST end points and get some Account details json using SOSQL query API.
However, I have some very basic questions. Since, I supplied the username and password of my developer salesforce account with my own connected app, Would this approach work for fetching from other organizations Salesforce accounts? Do they have to supply their username/password information to my application? Will there be some kind of configuration in the organization's Salesforce application to be able to connect with my newly created application?
Upvotes: 0
Views: 94
Reputation: 1
To answer your question, let me ask you one thing.. If u have your own Account on google and you created an app to login into it using OAUTH 2.0, so do you think you could able to login into the google account of other user.? I believe No. Unless you get the admin access you can't login. Rather you can ask user to enter their username and password with privacy policies. Hope this may help you.. Thanks
Upvotes: 0