Skip to content

Instantly share code, notes, and snippets.

@anmolgkv
Last active August 19, 2020 13:50
Show Gist options
  • Save anmolgkv/29031cedfe0823d252ff82e49ad0411c to your computer and use it in GitHub Desktop.
Save anmolgkv/29031cedfe0823d252ff82e49ad0411c to your computer and use it in GitHub Desktop.
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.URI;
import java.net.URLEncoder;
import java.io.UnsupportedEncodingException;
public class Sample {
public static void main(String ...args) {
try {
HttpClient client = HttpClient.newHttpClient();
// Note: non utf-8 characters needs to be URL encoded
// Ref: https://help.salesforce.com/articleView?id=000316466&language=en_US&type=1&mode=1
String password = URLEncoder.encode("g#$6-8YHVj", "UTF-8" );
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://test.salesforce.com/services/oauth2/token"))
.header("Content-Type", "application/x-www-form-urlencoded")
.POST(HttpRequest.BodyPublishers.ofString("grant_type=password" + "&client_id=3MVG95AcBeaB55lUkwfHx7LMDAyKq1KBRiIG2ZcmRhtXFdRWRqUpjbYVqgbtK7l.Y8yVX3IOvnlx89P03uYRY" + "&client_secret=7F7819F37750B0F4B7C5C37C0194BD2757491775EE1CA39F1AF7CD970C763487" + "&username=test-0ad7fasoqwwc@example.com" + "&password="+password))
.build();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.join();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment