Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class UsernamePasswordFlow {
- String username;
- String password;
- String clientId;
- String clientSecret;
- String tokenEndpoint = 'https://test.salesforce.com/services/oauth2/token';
- public UsernamePasswordFlow(String username, String password, String clientId, String clientSecret) {
- this.username = username;
- this.password = password;
- this.clientId = clientId;
- this.clientSecret = clientSecret;
- }
- public String requestAccessToken() {
- HttpRequest req = new HttpRequest();
- req.setEndpoint(tokenEndpoint);
- req.setMethod('POST');
- req.setBody(buildHttpQuery(new Map<String, String> {
- 'grant_type' => 'password',
- 'username' => username,
- 'password' => password,
- 'client_id' => clientId,
- 'client_secret' => clientSecret
- }));
- Http http = new Http();
- HttpResponse resp = http.send(req);
- Map<String, Object> m =
- (Map<String, Object>) JSON.deserializeUntyped(resp.getBody());
- return (String) m.get('access_token');
- }
- static String buildHttpQuery(Map<String, String> queryParams) {
- if (queryParams.isEmpty()) {
- return '';
- }
- String[] params = new String[] {};
- for (String k : queryParams.keySet()) {
- String v = EncodingUtil.urlEncode(queryParams.get(k), 'UTF-8');
- params.add(String.format('{0}={1}', new String[] { k, v }));
- }
- return String.join(params, '&');
- }
- }
- UsernamePasswordFlow upf = new UsernamePasswordFlow(
- 'my.name@example.com',
- 'mypassword',
- '3MdPGzpc3Iwg5lcwtc7HAprnDCVG9JNJ1CQBISiBA9bE1WXe_xlItLNdlxckCVyIo',
- '436267497522435674970'
- );
- String sessionId = upf.requestAccessToken();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement