Guest User

Untitled

a guest
Aug 7th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. var result = {};
  2. var application = "";
  3. var vendor = "";
  4. var businessunit = "";
  5. var user = "";
  6. var pass = "";
  7. var authCode = window.btoa(application + "@" + vendor + ":" + businessunit);
  8. $.ajax({
  9. "url": 'https://',
  10. "type": 'post',
  11. "contentType": 'application/json',
  12. "dataType": 'json',
  13. "headers": {
  14. 'Authorization': 'basic ' + authCode
  15. },
  16. "data": JSON.stringify({
  17. "grant_type": 'password',
  18. "username": user,
  19. "password" : pass,
  20. "scope": 'AdminApi AgentApi AuthenticationApi PatronApi RealTimeApi'
  21. }),
  22. "success": function (resp) {
  23. result.access_token = resp.access_token;
  24. result.token_type = resp.token_type;
  25. result.resource_server_base_uri = resp.resource_server_base_uri;
  26. result.expires_in = resp.expires_in;
  27. result.refresh_token = resp.refresh_token;
  28. result.scope = resp.scope;
  29. result.refresh_token_server_uri = resp.refresh_token_server_uri;
  30. },
  31. "error": function (XMLHttpRequest, textStatus, errorThrown) {
  32. alert("Failed to retrieve token.n" + XMLHttpRequest.status + ' '
  33. + XMLHttpRequest.statusText);
  34. }
  35. });
  36.  
  37. public class AuthCallout {
  38. public void basicAccessTokenGeneration() {
  39. HttpRequest req = new HttpRequest();
  40. req.setEndpoint('https://');
  41. req.setMethod('POST');
  42.  
  43. // Specify the required username and password to access the endpoint
  44. // As well as the header and header information
  45.  
  46. String username = '';
  47. String password = '';
  48. String vendor = '';
  49. String businessunit ='';
  50. String application ='';
  51.  
  52. Blob headerValue = Blob.valueOf(application + '@' + vendor + ':' + businessunit);
  53. String authorizationHeader = 'BASIC ' +
  54. EncodingUtil.base64Encode(headerValue);
  55. req.setHeader('Authorization', authorizationHeader);
  56.  
  57.  
  58. JSONGenerator gen = JSON.createGenerator(true);
  59.  
  60. gen.writeStartObject();
  61. gen.writeStringField('grant_type ', 'password');
  62. gen.writeStringField('username', username);
  63. gen.writeStringField('password',password);
  64. gen.writeStringField('scope','');
  65. gen.writeEndObject();
  66. String jsonS = gen.getAsString();
  67. system.debug('DAta in '+str);
  68. system.debug('Data in gen'+jsonS);
  69. req.setBody(jsonS);
  70. // Create a new http object to send the request object
  71. // A response object is generated as a result of the request
  72.  
  73. Http http = new Http();
  74. HTTPResponse res = http.send(req);
  75. System.debug(res.getBody());
  76. System.debug(res.getStatusCode());
  77. System.debug(res.getBodyDocument());
  78. }
  79. }
Add Comment
Please, Sign In to add comment