Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var result = {};
- var application = "";
- var vendor = "";
- var businessunit = "";
- var user = "";
- var pass = "";
- var authCode = window.btoa(application + "@" + vendor + ":" + businessunit);
- $.ajax({
- "url": 'https://',
- "type": 'post',
- "contentType": 'application/json',
- "dataType": 'json',
- "headers": {
- 'Authorization': 'basic ' + authCode
- },
- "data": JSON.stringify({
- "grant_type": 'password',
- "username": user,
- "password" : pass,
- "scope": 'AdminApi AgentApi AuthenticationApi PatronApi RealTimeApi'
- }),
- "success": function (resp) {
- result.access_token = resp.access_token;
- result.token_type = resp.token_type;
- result.resource_server_base_uri = resp.resource_server_base_uri;
- result.expires_in = resp.expires_in;
- result.refresh_token = resp.refresh_token;
- result.scope = resp.scope;
- result.refresh_token_server_uri = resp.refresh_token_server_uri;
- },
- "error": function (XMLHttpRequest, textStatus, errorThrown) {
- alert("Failed to retrieve token.n" + XMLHttpRequest.status + ' '
- + XMLHttpRequest.statusText);
- }
- });
- public class AuthCallout {
- public void basicAccessTokenGeneration() {
- HttpRequest req = new HttpRequest();
- req.setEndpoint('https://');
- req.setMethod('POST');
- // Specify the required username and password to access the endpoint
- // As well as the header and header information
- String username = '';
- String password = '';
- String vendor = '';
- String businessunit ='';
- String application ='';
- Blob headerValue = Blob.valueOf(application + '@' + vendor + ':' + businessunit);
- String authorizationHeader = 'BASIC ' +
- EncodingUtil.base64Encode(headerValue);
- req.setHeader('Authorization', authorizationHeader);
- JSONGenerator gen = JSON.createGenerator(true);
- gen.writeStartObject();
- gen.writeStringField('grant_type ', 'password');
- gen.writeStringField('username', username);
- gen.writeStringField('password',password);
- gen.writeStringField('scope','');
- gen.writeEndObject();
- String jsonS = gen.getAsString();
- system.debug('DAta in '+str);
- system.debug('Data in gen'+jsonS);
- req.setBody(jsonS);
- // Create a new http object to send the request object
- // A response object is generated as a result of the request
- Http http = new Http();
- HTTPResponse res = http.send(req);
- System.debug(res.getBody());
- System.debug(res.getStatusCode());
- System.debug(res.getBodyDocument());
- }
- }
Add Comment
Please, Sign In to add comment